suganya
suganya

Reputation: 145

How can I change the color of selected item from ListView?

I have created an android project in which i have a list view with 6 items. I want that every time I select any item from the list, it gets a colour orange which stays until I press button submit.

the code is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.SafeWalkApp.SecondActivity" >

     <ListView
        android:id="@+id/sampleListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
       android:entries="@array/vol_list" >
    </ListView>

     <Button
         android:id="@+id/onsubmit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:text="@string/submit" />

</RelativeLayout>

this is my xml code. I have tried adding the color to color.xml but that is showing me an error message and basically it is for the backgroung. So please help me out on this.

Upvotes: 0

Views: 237

Answers (3)

Furqan Ali
Furqan Ali

Reputation: 137

Well if you are using a custom listview and had an item set by custom adapter then you can change the color of the listview item in

nList = (ListView) findViewById(android.R.id.yourlistname); nList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

    }
});

In above function nList is the list in which you want to make change and in its on click listner you can get the selected item view as in its parameters as View arg1 and from that you can get the linear layout and its all other controls and make changes to any control color, text etc as you want

get the view in here and set any color you want by getting the view and setting its background etc. I can not write the whole code here but I hope you get my?

Upvotes: 1

Mikelis Kaneps
Mikelis Kaneps

Reputation: 4584

  1. Make a class that contains all the info that is needed for your adapter item + a boolean variable, that you are going to check in adapters getView, if the variable is going to be true, change the color to orange else normal color.
  2. in onItemClick find the position of the correct object in the list that you gave to your adapter and change its boolean value.
  3. after that refresh the adapters referenced list.
  4. Call notifyDataSetChanged() on your adapter. Now according to point(1.) the adapter is going to check the boolean value of the item and change its color to orange.

Upvotes: 0

acostela
acostela

Reputation: 2727

You must create state drawable colors. And some xml.files to define different colors depending on the state of your row.

Maybe this can help you.

How to change color of ListView items on focus and on click

Android LinearLayout with color resource: What am I doing wrong?

Upvotes: 0

Related Questions