Change Listview color when selecting

Asked

Viewed 1,254 times

1

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

below goes as is the stuck 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>
  • You want it to change color when the item is "clicked"?

  • @Marcogiovanni, this, I want you to change when you click. and then go back to the pattern.

  • See if it fits you. Bug listDebitosPendentes.setBackgroundColor(Color.BLUE); within the onItemClick.

  • @Rodrigopaixão, that way no. Because he painted the whole list and she does not return to the pattern.

1 answer

2

You must create a selector for the listview item:

In the folder drawable 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>

Inside the folder values create an xml called colors.xml if there is no

And insert the color

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

In the xml of the ITEM layout, set the property backgroud, 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>

Upshot:

inserir a descrição da imagem aqui

  • But that way, it will color the whole list, I need the item to stay colored only when it is clicked, then it goes back to normal.

  • Won’t color every list, will color only the item that is clicked.

  • In java code I don’t need to add anything?

  • No need to change java.

  • Leaving as said not Linearlayout, he did not change the color when I asked in the listview item, I put in the background to test tbm, ai ele coloriu toda a lista.

  • You have put the backgroud in the xml of the item you inflate in your listview?

  • I added the layout code to the question, but the drawable

  • Insert an image to see if this is what you want

  • That’s exactly what I need.

  • I do exactly what I put in the answer, it can be some code of yours that makes select all listview, tries to create another list from scratch to test.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.