ListView Selector and ImageButton not working

1

Both the button and the ListView are not modified when clicked, but the settings when the item is not pressed work.

ListView :

<ListView
        android:id="@+id/listaListas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#FFECECEC"
        android:dividerHeight="2sp"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/novalista"
        android:background="@drawable/layout_item_listview">
    </ListView>

layout_item_listview:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_selected="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_pressed="false" android:state_selected="false"
        android:drawable="@drawable/layout_item_normal" />
</selector>

layout_item_selected:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/item_selecionado"/>
</shape>

layout_item_normal:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/vermelho"/>
</shape>

Boot:

<ImageButton
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@color/amarelo"
            android:src="@drawable/img_estrelabotao"
            android:id="@+id/btnfavorito"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:visibility="visible"
            style="@style/estiloBotaoFavorito"/>

FavoriteBoard style:

<style name="estiloBotaoFavorito">
        <item name="android:background">@drawable/layout_botao_favorito</item>
        <item name="android:textColor">@color/icons</item>
        <item name="android:textSize">15dp</item>
    </style>

layout_botao_favorito:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/layout_botao_favorito_pressionado"/>
    <item android:state_pressed="false" android:drawable="@drawable/layout_botao_favorito_normal"/>
</selector>

layout_botao_favorito_pressado:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid
        android:drawable="@drawable/nine_patch_retangulo"
        android:color="@color/amarelo_escuro"
        android:elevation="8dp"/>
</shape>

layout_botao_favorito_normal:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid
        android:drawable="@drawable/nine_patch_retangulo"
        android:color="@color/amarelo"
        android:elevation="8dp"/>
</shape>

Another question I have is if within the shape of the button I can set elevation and drawable? As you did not accuse me I left it like this

    
asked by anonymous 20.09.2016 / 17:01

1 answer

1

The order in which states are declared matters in the way they are applied, which is not always obvious.

The best way to avoid such situations is to declare as few states as possible and to do so on separate items.

Change the selectors like this:

layout_item_listview:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_selected="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_activated="true" 
        android:drawable="@drawable/layout_item_selecionado"/>
    <item android:drawable="@drawable/layout_item_normal" />
</selector>

In order to make the selection active, you must add android:state_activated="true" and declare the android:choiceMode attribute in a ListView attribute with a mode other than none .

Note: This selector should be assigned to the background of the layout >.

layout_botao_favorito:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
        android:drawable="@drawable/layout_botao_favorito_pressionado"/>
    <item android:drawable="@drawable/layout_botao_favorito_normal"/>
</selector>

The last <item> , the one that has not been set, will be applied by default (when none of the previous ones is verified).

Regarding the other question, documentation only indicates the android:color can be used in element <solid> .

    
20.09.2016 / 18:27