I am developing an app that when I press a button ,It starts a new activity (ContactList.java) that contains a listview called mContactList. When the activity starts I want the first item of the listview to be automatically focused.
I tried something like that:
ContactList.java
mContactsList.requestFocus(mContactsList.getFirstVisiblePosition());
I have also created this two xml resources to ensure that when the view is focused a rectangle is displayed around the item of the listview:
selected_rectangle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="@color/blue"
android:width="5dp"/>
</shape>
selector_contact_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/selected_rectangle"/> </selector>
For some reason this code doesn´t work and I am not able to set focus on the first item of the list (since no rectangle appears around the first item of the list).
Can someone give me clues what is happening?