Solved

ArrayAdapter list layout - Right Align

Posted on 2011-03-20
11
954 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
Comment Utility

Use the gravity attribute:

setGravity(Gravity.RIGHT);

0
 
LVL 17

Expert Comment

by:Dejan Pažin
Comment Utility

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
Comment Utility

 I got: setGravity is undefined method

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

thanks in advanced



0
 
LVL 17

Expert Comment

by:Dejan Pažin
Comment Utility


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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:Dejan Pažin
Comment Utility


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
Comment Utility
public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;
0
 
LVL 17

Expert Comment

by:Dejan Pažin
Comment Utility

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

Accepted Solution

by:
Dejan Pažin earned 500 total points
Comment Utility
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
Comment Utility
it's work wonderful

 Thanks you
0
 
LVL 17

Expert Comment

by:Dejan Pažin
Comment Utility

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently Amazon announced their entry in the tablet device called the Amazon Kindle Fire. Some of you might have used an Ipad or have considered buying one. While they're similar in form and function in that they both have color displays with multit…
A short article about problems I had with the new location API and permissions in Marshmallow
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now