How Do You Change The Background Color Of A TextView That Is Inside A ListView Programmatically?
I have a ListView that contains several TextView items. This list is created at runtime, and can vary in size. I would like to set the background of a TextView item based on a fl
Solution 1:
Solution 2:
Simply find the TextView as:
TextView myTextView = (TextView)findViewById(R.id.yourTextViewId);
And do what ever you want for example:
myTextView.setTextColor(color);
myTextView.setBackgroundColor(color);
EDIT:
Please find on this site how to implement "android custom adapter"?
Solution 3:
I assume you want to change color of list items selectively. For that you have to write your own custom adapter and override getView() method. Inside getView() you can change the color of any item depending on the position.
This link may help you write a custom adapter.
your getView() should look something like this -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
StockQuoteView sqView = null;
if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.stock_quote_list_item, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sqView = new StockQuoteView();
sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_symbol);
sqView.quote = (TextView) rowView.findViewById(R.id.ticker_price);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sqView);
} else {
sqView = (StockQuoteView) rowView.getTag();
}
if(position == 3) {
rowView.setBackgroundColor(#030303);
}
// Transfer the stock data from the data object
// to the view objects
StockQuote currentStock = stocks.get(position);
sqView.ticker.setText(currentStock.getTickerSymbol());
sqView.quote.setText(currentStock.getQuote().toString());
return rowView;
}
Hope that helps.
Solution 4:
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String>{
Context mContext;
String list[];
LayoutInflater mInflater;
public static HashMap<Integer, String> idList=new HashMap<Integer,String>();
public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId, objects);
mContext=context;
list=objects;
mInflater=LayoutInflater.from(context);
for(int i=0;i<list.length;i++){
idList.put(i,"false");
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_fruit,null);
holder=new ViewHolder();
holder.mTextView=(TextView)convertView.findViewById(R.id.mTextViewId);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
idList.put(position, "true");
if(idList.get(position)=="true")
holder.mTextView.setBackgroundColor(Color.GRAY);
else
holder.mTextView.setBackgroundColor(Color.WHITE);
return convertView;
}
class ViewHolder{
TextView mTextView;
}
}
list_fruit.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mTextViewId"
android:background="#fff"
android:textColor="#333"
android:padding="5dip"/>
Now,
setListAdapter(new CustomAdapter(this, R.layout.list_fruit,ratios));
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);
Now,whichever textview will be clicked,will get GRAY color,and others are WHITE colored.
Post a Comment for "How Do You Change The Background Color Of A TextView That Is Inside A ListView Programmatically?"