SoCmodder
SoCmodder

Reputation: 21

Multiple text colors in listview of android app

Alright guys, here is the deal. I have an RSS reader in my application that parses the XML and spits it into the Listview...Right now I have the color set to green, but I need the title to be green and the part of the description that I put in each row to be black.

Here is my code for the part that displays the feed:

public class CampusNews extends Activity implements OnItemClickListener
{
int checker = RSSHandler.checker;
public final String RSSFEEDOFCHOICE = "myurl";

private static final int SELECT = 0;
private static final int REFRESH = 1;

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.campus_news_layout);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        return null;
    }
}



private void UpdateDisplay()
{
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    {
        feedtitle.setText("No RSS Feed Available");
        return;
    }

    // feedtitle.setText(feed.getTitle());
    feedpubdate.setText(feed.getPubDate());




    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems()){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // View for custom Background color of feeds
            View view = super.getView(position, convertView, parent);

            if (position % 2 == 0){
                view.setBackgroundColor(0xffe4e4e4); // gray
                ((TextView) view).setTextColor(0xff00DD00); // text color
                ((TextView) view).setTextSize(15.0f);


            } else {
                view.setBackgroundColor(0xffffffff); // White
                ((TextView) view).setTextColor(0xff00DD00); // text color
                ((TextView) view).setTextSize(15.0f);
            }
            return view;
        }


    };




    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);

}


 @SuppressWarnings("rawtypes")
public void onItemClick(AdapterView parent, View v, int position, long id)
 {
     Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

     startActivity(itemintent);
 }

}

and this is where I have the code which tells it how to place the title and description piece inside of each row.

public class RSSItem 
{
private String _title = null;
private String _description = null;
private String _link = null;
private String _category = null;
private String _pubdate = null;


RSSItem()
{
}
void setTitle(String title)
{
    _title = title;
}
void setDescription(String description)
{
    _description = description;
}
void setLink(String link)
{
    _link = link;
}
void setCategory(String category)
{
    _category = category;
}
void setPubDate(String pubdate)
{
    _pubdate = pubdate;
}
String getTitle()
{
    return _title;
}
String getDescription()
{
    return _description;
}
String getLink()
{
    return _link;
}
String getCategory()
{
    return _category;
}
String getPubDate()
{
    return _pubdate;
}
public String toString()
{
    // limit how much text we display
    if (_title.length() > 42)
    {

        return _title.substring(0, 42) + "..." + "\n" +         _description.substring(3, 110) + "...";
    }
    return _title + "\n" + _description.substring(3, 110) + "...";

}
}

I searched around alot and thats how I was able to fix most of my problems, but I couldnt find a way to put multiple text colors in a single row for each item. Thanks in advance!

Upvotes: 2

Views: 1844

Answers (1)

Femi
Femi

Reputation: 64700

You will want to look at this: http://developer.android.com/resources/faq/commontasks.html#selectingtext . It covers styling portions of text.

EDIT: you can use instances of ForegroundColorSpan to change the color of bits and pieces of your text.

EDIT #2: Try something like this:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // View for custom Background color of feeds
        View view = super.getView(position, convertView, parent);

        if (position % 2 == 0){
            view.setBackgroundColor(0xffe4e4e4); // gray
            ((TextView) view).setTextColor(0xff00DD00); // text color
            ((TextView) view).setTextSize(15.0f);


        } else {
            view.setBackgroundColor(0xffffffff); // White
            ((TextView) view).setTextColor(0xff00DD00); // text color
            ((TextView) view).setTextSize(15.0f);
        }
        RSSItem item = (RSSItem)getItem(position);
        String title = item.getTitle(), base = item.toString();
        SpannableString str = new SpannableString(base);
        if(title.length() > 42){
           str.setSpan(new ForegroundColorSpan(0xFF00FF00), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           str.setSpan(new ForegroundColorSpan(0xFF000000), title.length()+1, base.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }else{
           str.setSpan(new ForegroundColorSpan(0xFF00FF00), 0, 45, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           str.setSpan(new ForegroundColorSpan(0xFF000000), 46, base.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }  
        ((TextView) view).setText(str);
        return view;
    }

Upvotes: 2

Related Questions