Change ListView color by selecting [duplicate]

1

How can I change the color of a listView when selecting?

Here are the steps in my project:

listDebitosPendentes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                       Intent intent= new Intent(getApplicationContext(),DetalhesDebitosActivity.class);

                      //Passa para a activity o id no banco de dados
                       intent.putExtra("ID", id);
                       startActivity(intent);

                }
            });

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/color_list"
    android:weightSum="1">


        <EditText
            android:layout_width="match_parent"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/edtPesquisa"
            android:drawableLeft="@android:drawable/ic_search_category_default"
            android:layout_marginTop="15dp"
            android:hint="Pesquisa rápida"
            tools:ignore="HardcodedText,NestedWeights,RtlHardcoded"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_height="45dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listDebitosPendentes"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
        tools:ignore="NestedScrolling"
        android:footerDividersEnabled="false"/>

</LinearLayout>

drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary" android:state_pressed="true"/>
</selector>
    
asked by anonymous 25.11.2016 / 14:46

1 answer

2

You should create a selector for the listview item:

In the drawable folder of the project create a new "drawable resource file", for example, let's call exemplo.xml

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

Within the values folder, create an xml named colors.xml if it does not exist

And insert the color

<color name="azul">#6EA6EC</color>

In the ITEM layout xml, set the backgroud property, for example:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/exemplo">

//..itens

</LinearLayout>

Result:

    
25.11.2016 / 15:06