Yury Pogrebnyak
Yury Pogrebnyak

Reputation: 4173

Android: keep focus when other view gets it

I have 4 buttons for switching displayed content. Any time one of them is pressed, it's highlighted:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/bottom_selected" android:state_pressed="true"></item>
<item android:drawable="@drawable/bottom_selected" android:state_pressed="false" android:state_focused="true"></item>
<item android:drawable="@android:color/transparent"></item>

</selector>

My problem is that I need last pressed button to keep its focus even if some other view is focused (f.e., I have EditText view, and when it's pressed, all buttons lose their focuses and none of them is highlighted). Is there any easy way to do that? Remembering last pressed button and rehighlighting it when other view is pressed doesn't sound like good solution.

UPD: It can be done by adding lastPressedButton.requestFocus() to other view's OnFocusChangeListener. Not too bright if there are many other views, maybe some other solution?

Upvotes: 0

Views: 1357

Answers (1)

Stelian Matei
Stelian Matei

Reputation: 11623

Try to add android:focusable="false" attribute to all the other fields that you don't want to be focusable.

Upvotes: 1

Related Questions