We known how to color ListView row android, so is there any way to set color
for each row different ?
Declare an arraylist.
ArrayList<String> fru
= new ArrayList<String>();
Add elements in to arraylist.
fru.add("Pear");
fru.add("Banana");
fru.add("Cashew");
fru.add("Orange");
fru.add("Water
melon");
fru.add("Peach");
fru.add("Grape");
fru.add("Mango");
fru.add("Plum");im
In folder layout, create a file name li.xml.
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42sp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/im"
android:layout_width="40sp"
android:layout_height="45sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:contentDescription="@null"
android:src="@drawable/cuoi" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:layout_toEndOf="@+id/im"
android:layout_toRightOf="@+id/im"
android:textColor="#0000aa"
android:textSize="18sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:contentDescription="@null"
android:src="@drawable/ten" />
</RelativeLayout>
Copy icon to folder drawable and change scr name if needs.
Create a new class name listp, source code below.
public class listp extends
ArrayAdapter<String> {
private final Activity context;
private final
ArrayList<String> item;
public listp(Activity
context, ArrayList<String> item) {
super(context,
R.layout.li, item);
// TODO
Auto-generated constructor stub
this.context=context;
this.item=item;
}
@SuppressLint({ "ViewHolder", "InflateParams" })
public View getView(int position,View
view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View
rowView=inflater.inflate(R.layout.li, null,true);
TextView txtTitle = (TextView)
rowView.findViewById(R.id.text);
txtTitle.setText(item.get(position));
if(position==0){
txtTitle.setTextColor(Color.parseColor("#FF0000"));
}
else if(position==1){
txtTitle.setTextColor(Color.parseColor("#FF7F50"));
}
else if(position==2){
txtTitle.setTextColor(Color.parseColor("#0000FF"));
}
else if(position==3){
txtTitle.setTextColor(Color.parseColor("#00ffff"));
}
else if(position==4){
txtTitle.setTextColor(Color.parseColor("#800080"));
}
else if(position==5){
txtTitle.setTextColor(Color.parseColor("#00FF00"));
}
else if(position==6){
txtTitle.setTextColor(Color.parseColor("#800000"));
}
else if(position==7){
txtTitle.setTextColor(Color.parseColor("#008000"));
}
else{
txtTitle.setTextColor(Color.parseColor("#FF00FF"));
}
return rowView;
};
}
We create custom adapter, in if command we set color for rows depending on it’s
position. You can change to whatever you want.
After that, set adapter for ListView like this
adapter= new
listp(this,fru);
Run to see ListView with different color rows.
If want to change text font, add this line in class listp.
txtTitle.setTypeface(Typeface.MONOSPACE,
Typeface.BOLD);
If want to change font size, add this line
txtTitle.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP,
16);
Run to see font changed.
No comments:
Post a Comment