?
Solved

ArrayAdapter list layout - Right Align

Posted on 2011-03-20
11
Medium Priority
?
1,033 Views
Last Modified: 2012-05-11
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
Comment
Question by:madmare
  • 7
  • 4
11 Comments
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35178961

Use the gravity attribute:

setGravity(Gravity.RIGHT);

0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35178966

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
 

Author Comment

by:madmare
ID: 35178980

 I got: setGravity is undefined method

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

thanks in advanced



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35179019


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
 

Author Comment

by:madmare
ID: 35179030
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
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35179053


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
 

Author Comment

by:madmare
ID: 35179063
public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35179080

Ok, thx. I'll take a look later today.
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 total points
ID: 35179516
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
 

Author Comment

by:madmare
ID: 35179598
it's work wonderful

 Thanks you
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35179661

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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Ready to kick start your career in 2018? Add app developer skills to your resume. January’s Course of the Month features Android App Development training with hands-on learning.  Read on to learn why these skills are important.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question