Skip to content Skip to sidebar Skip to footer

Change The Background Image Of Listview On Focus

My code is : ListContacts.java public class ListContacts extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

Solution 1:

Add an xml file to manage the background of the LinearLayout.

list_bg_selector.xml :

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- Active tab --><itemandroid:state_selected="true"android:state_focused="false"android:state_pressed="false"android:drawable="@drawable/list_selected_bgimage" /><!-- Inactive tab --><itemandroid:state_selected="false"android:state_focused="false"android:state_pressed="false"android:drawable="@drawable/list_default_bgimage" /><!-- Pressed tab --><itemandroid:state_pressed="true"android:drawable="@drawable/list_selected_bgimage" /><!-- Selected tab (using d-pad) --><itemandroid:state_focused="true"android:state_selected="true"android:state_pressed="false"android:drawable="@drawable/list_selected_bgimage" /></selector>

To manage the text color, set the text color to another xml.

text_selector.xml:

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_selected="true"android:color="@android:color/black"/><itemandroid:state_focused="true"android:color="@android:color/black"/><itemandroid:state_pressed="true"android:color="@android:color/black"/><itemandroid:color="@android:color/white"/></selector>

Solution 2:

in getView

if(position == focusedPosition) {
    textView.setColor(context.getResources().getColor(R.color.color_for_focused_item);
} else {
    textView.setColor(context.getResources().getColor(R.color.color_for_normal_item);
}

Solution 3:

try this

    listViewContactList.setOnFocusChangeListener(newOnFocusChangeListener(){

    @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
    v.setBackgroundColor(Color.BLACK);

   ImageView img=(ImageView)v.findViewById(R.id.callimage);
   img.setImageResource(R.drawable.yourimage);
   TextView tv=(TextView)v.findViewById(R.id.your_textview_id);
   tv.setBackgroundResource(R.drawable.your_back);      
    }

    });

Post a Comment for "Change The Background Image Of Listview On Focus"