in Android,
I have list that I fill using the following code
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("xxxxxxxx")) {
imageView.setImageResource(R.drawable.one);
} else if (s.equals("yyyyyyyyyy")) {
imageView.setImageResource(R.drawable.two);
} else if (s.equals("zzzzzzzzzzzzzzzzz")) {
imageView.setImageResource(R.drawable.three);
}
// to change the background color
return rowView;
}
how to change the selection color instead of orange , in the layout I use the following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#DFE1E5">
<ImageView
android:id="@+id/logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:src="@drawable/one" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20dp" >
</TextView>
</LinearLayout>
to solve that I did that
first add color xml to drwable
<?xml version="1.0" encoding="utf-8"?>
<resources> <color name="selection_color">#0587F4</color>
</resources>
secondly I added XML to drwable to handle the selection
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/selection_color"
android:state_selected="true"/>
<!-- <item android:drawable="@drawable/list_selection_bg" android:state_pressed="true"/>
-->
</selector>
the first problem is that it raaise error regardign "@color/selection_color"
any idea to achieve that
Create a XML of Selector and save it in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_selection_bg" android:state_selected="true"/>
<item android:drawable="@drawable/list_selection_bg" android:state_pressed="true"/>
</selector>
Then add the following code in getView method of the Adapter class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("xxxxxxxxxxx")) {
imageView.setImageResource(R.drawable.one);
} else if (s.equals("yyyyyyyy")) {
imageView.setImageResource(R.drawable.two);
} else if (s.equals("zzzzzzzzzzzzzzzzz")) {
imageView.setImageResource(R.drawable.three);
}
// to change the background color
rowView.setBackgroundResource(R.drawable.alternate_list_color1);
return rowView;
}
in the ListView mention it
android:listSelector="@drawable/list_selector"
and make list_selector.xml in drawable and paste this code and you can change as per your requirement...
<!-- Selected -->
<item android:drawable="@drawable/list_hover" android:state_focused="true" android:state_pressed="false"/>
<!-- @drawable/tab_focus -->
<!-- Pressed -->
<item android:drawable="@drawable/list_hover" android:state_pressed="true"/>
<!-- @drawable/tab_press -->
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.