• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • Last Modified:

ArrayAdapter list layout - Right Align

Hi experts,
 I have an Android application wich displays RSS by ArrayAdapter list
 
 here is the code to display the Array Adapter:
          ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,
                      android.R.layout.simple_list_item_1,myRssFeed.getList());
          setListAdapter(adapter);  


How do I set the layoput be Rigth Alinged ?? ( by default it's left Aligned)

Thanks Alot
0
madmare
Asked:
madmare
  • 7
  • 4
1 Solution
 
Dejan PažinHead of SW DevelopmentCommented:

Use the gravity attribute:

setGravity(Gravity.RIGHT);

0
 
Dejan PažinHead of SW DevelopmentCommented:

madmare, I already answered to your similar question here:

http://www.experts-exchange.com/Programming/Languages/Java/Android/Q_26859493.html?sfQueryTermInfo=1+10+30+android+rtl

but you did not post any reply.

Please post the complete code, so I can see why you dont have it right alligned. The example I posted in the other question, works on my emulator.
0
 
madmareAuthor Commented:

 I got: setGravity is undefined method

 can you please be more specific , i'm new in Android development

thanks in advanced



0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Dejan PažinHead of SW DevelopmentCommented:


You have to use setGravity on the object that has that method defined. For example LinearLayout:

http://developer.android.com/reference/android/widget/LinearLayout.html


If you cant find it, post the complete code, otherways I cant see where is the problem.
0
 
madmareAuthor Commented:
This is my Code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout layout = new LinearLayout(this);

    try {

          URL rssUrl = new URL("http://www.aaaa.com/rssservice/V.aspx");          
          SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
          SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
          XMLReader myXMLReader = mySAXParser.getXMLReader();
          RSSHandler myRSSHandler = new RSSHandler();
          myXMLReader.setContentHandler(myRSSHandler);
          InputSource myInputSource = new InputSource(rssUrl.openStream());
          myXMLReader.parse(myInputSource);

          myRssFeed = myRSSHandler.getFeed();

    } catch (MalformedURLException e) {
      
          e.printStackTrace();
    } catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
          e.printStackTrace();
    } catch (SAXException e) {
// TODO Auto-generated catch block
          e.printStackTrace();
    } catch (IOException e) {
// TODO Auto-generated catch block
          e.printStackTrace();
            }

    if (myRssFeed!=null)
    {
          TextView feedTitle = (TextView)findViewById(R.id.feedtitle);          
          TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
          TextView feedPubdate = (TextView)findViewById(R.id.feedpubdate);
          TextView feedLink = (TextView)findViewById(R.id.feedlink);
          
          feedTitle.setText(myRssFeed.getTitle());
          feedTitle.setGravity(Gravity.RIGHT);
          
          feedDescribtion.setText(myRssFeed.getDescription());
          feedDescribtion.setGravity(Gravity.RIGHT);
          
          //feedPubdate.setText(myRssFeed.getPubdate());
          //feedPubdate.setGravity(Gravity.RIGHT);
          
          //feedLink.setText(myRssFeed.getLink());
          //feedLink.setGravity(Gravity.RIGHT);
          
          LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
          
          
          
          ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,
                      android.R.layout.simple_list_item_1,myRssFeed.getList());    
          setListAdapter(adapter);

            }
      }
}
0
 
Dejan PažinHead of SW DevelopmentCommented:


Please add the class definition, so I can see what you are extending here. I just need the line which starts with:

public class .... extends ....
0
 
madmareAuthor Commented:
public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;
0
 
Dejan PažinHead of SW DevelopmentCommented:

Ok, thx. I'll take a look later today.
0
 
Dejan PažinHead of SW DevelopmentCommented:
What you need is your own adapter. Here is everything you need:

1. Add the class below to your code
2. Change this line:

 ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this, android.R.layout.simple_list_item_1,myRssFeed.getList());    

into this:

 RightAlignedAdapter<RSSItem> adapter = new RightAlignedAdapter<RSSItem>(this, android.R.layout.simple_list_item_1,myRssFeed.getList());    


public class RightAlignedAdapter<K> extends  ArrayAdapter<K> {


        public RightAlignedAdapter(Context context, int resource, List<K> objects) {
            super(context, resource, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView)super.getView(position, convertView, parent);
            view.setGravity(Gravity.RIGHT);
            return view;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView)super.getView(position, convertView, parent);
            view.setGravity(Gravity.RIGHT);
            return view;
        }
    }

Open in new window

0
 
madmareAuthor Commented:
it's work wonderful

 Thanks you
0
 
Dejan PažinHead of SW DevelopmentCommented:

I'm glad it works for you.

This is also connected to your other questin about right alignement:

http://www.experts-exchange.com/Programming/Languages/Java/Android/Q_26859493.html?sfQueryTermInfo=1+10+30+android+rtl

Now would be a good time to check if the last answer on that question works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now