What's the state of a pressed Tab in Android
Asked Answered
T

2

1

I'm trying to get my tabicon to change when a tab is pressed (i.e. when it changes color when you press the tab, but haven't released yet). I've created a selector as follows:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item android:state_focused="false" 
          android:state_selected="false" 
          android:state_pressed="false" 
          android:drawable="@drawable/ic_tab_icon1" /> 
    <item android:state_focused="false" 
          android:state_selected="true" 
          android:state_pressed="false" 
          android:drawable="@drawable/ic_tab_icon2" /> 

    <!-- Focused states --> 
    <item android:state_focused="true" 
          android:state_selected="false" 
          android:state_pressed="false" 
          android:drawable="@drawable/ic_tab_icon3" /> 
    <item android:state_focused="true" 
          android:state_selected="true" 
          android:state_pressed="false" 
          android:drawable="@drawable/ic_tab_icon4" /> 

    <!-- Pressed --> 
    <item android:state_pressed="true" 
          android:drawable="@drawable/ic_tab_icon5" /> 
</selector> 

However, for some reason only the first two states are reached (only icon1 and icon 2 are used). Can anyone tell me what the correct state is for a "pressed but not selected" tab?

*Edited to clarify new situation

Tisatisane answered 16/9, 2010 at 10:18 Comment(1)
Did you ever get this problem figured out? I'm encountering the same issue. It seems like my code (and your code) is correct according to the Android doc's but I cannot get the state_pressed to work...Sharla
A
0

Here is tab_indicator.xml from Android 2.2:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />

    <!-- Pressed -->
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>

You do not have anything where android:state_pressed="true". Try changing your selector to look a bit more like what Android uses.

Abeabeam answered 16/9, 2010 at 10:24 Comment(1)
I have tried this, but it results in the same. If I only change the first line (all false) to the first icon, and all others to the second, the first icon still keeps showing when the tab is pressed.Tisatisane
C
0

this is the default tab_indicator.xml for android. got from this gitkernal. you have to maintain all these states.

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_normal" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_selected" /> 

    <!-- Focused states --> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_normal" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_selected" /> 

    <!-- Pressed --> 
    <item android:state_pressed="true" android:drawable="@drawable/ic_tab_north_normal" /> 
</selector> 
Ceratodus answered 16/9, 2010 at 10:27 Comment(3)
I have tried this, but it results in the same. If I only change the first line (all false) to the first icon, and all others to the second, the first icon still keeps showing when the tab is pressed.Tisatisane
@Siebe: I edited the code with the value mentioned on your question.Try this.Ceratodus
Thank for your comment, but it's still the same. I've changed the icon for all the states to different ones and it seems it never reaches the 3rd, 4th or 5th state, i.e. only the images in the non focused states are used.Tisatisane

© 2022 - 2024 — McMap. All rights reserved.