?
Solved

ArrayAdapter list layout - Right Align

Posted on 2011-03-20
11
Medium Priority
?
1,010 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If your app took Google’s lash recently, here are the 5 most likely reasons.
These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

764 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