Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:26:55+00:00 2026-06-13T18:26:55+00:00

I have a custom list View that contains 7 text views..I have created a

  • 0

I have a custom list View that contains 7 text views..I have created a custom linear layout for that..And then I’m populating it…What i want to do is set a custom color…When the user clicks on the list item and keep it highlighted till he clicks another item…How will i do that??…

This is the custom layout for listitems…

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LLtv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/colors"
    android:cacheColorHint="#00000000" >

    <TextView
        android:id="@+id/TvVLdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/TvVLdate"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLcardno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/TvVLtime"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLsaletext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TvVLdate"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:text="Sale transaction:Rs"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLamnt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLsaletext"
        android:layout_toRightOf="@+id/TvVLsaletext"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtexttip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLamnt"
        android:layout_toRightOf="@+id/TvVLamnt"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:text="Tip:Rs"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLtexttip"
        android:layout_toRightOf="@+id/TvVLtexttip"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

</RelativeLayout>

And my list layout..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0E0E0"
    android:orientation="vertical" >

    <include
        android:id="@+id/Hvoid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <RelativeLayout
        android:id="@+id/RLvoid"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Hvoid"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="12dp"
            android:text="Main Menu"
            android:textColor="#000000"
            android:textSize="15dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:src="@drawable/ivoid" />
    </RelativeLayout>

    <View
        android:id="@+id/Vvoidtop"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_below="@+id/RLvoid"
        android:background="@android:color/darker_gray" />

    <ListView
        android:id="@+id/Lvvoid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/Bconfirm"
        android:layout_below="@+id/Vvoidtop"
        android:background="#E0E0E0"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/transparent"
        android:dividerHeight="20dp" >
    </ListView>

    <Button
        android:id="@+id/Bconfirm"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_above="@+id/Vvoidbot"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/cbconfirm" />

    <View
        android:id="@+id/Vvoidbot"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_above="@+id/Tvfooter"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/Tvfooter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:text="© India Transact Services Ltd."
        android:textColor="#000000"
        android:textSize="15dp" />

</RelativeLayout>

And the drawable colors xml file…

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selected Item -->
    <item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
    <item android:drawable="@android:color/darker_gray"  android:state_focused="true"/>
    <!-- Default Item -->
    <item android:drawable="@android:color/white" />
</selector>

And my code….

public class Void extends Activity {
    ListView lmenu;
    View Vlistselector;
    String s;
    View layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voidlist);

        Menu Menu_data[] = new Menu[] {
                new Menu("29/09/2012", "05:40", "2234 xxxx xxxx 1990", "100",
                        "10"),
                new Menu("09/12/2012", "00:34", "2234 xxxx xxxx 1990", "99",
                        "12"),
                new Menu("29/09/2012", "12:20", "2234 xxxx xxxx 1990", "111",
                        "0"),
                new Menu("31/01/2012", "13:40", "2234 xxxx xxxx 1990", "105",
                        "19"),
                new Menu("13/10/2012", "01:40", "2234 xxxx xxxx 1990", "203",
                        "1"),
                new Menu("23/11/2012", "07:40", "2234 xxxx xxxx 1990", "44",
                        "5") };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.voidlisttext,
                Menu_data);

        lmenu = (ListView) findViewById(R.id.Lvvoid);

        lmenu.setAdapter(adapter);
        // lmenu.setSelector(R.drawable.listcolorselector);
        lmenu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> ada, View v, int position,
                    long id) {
                // TODO Auto-generated method stub

                /*
                 * v.setSelected(true);
                 */
                v.setBackgroundColor(Color.parseColor("#FCD5B5"));

                if (!(Vlistselector == null) && Vlistselector != v)
                    Vlistselector.setBackgroundColor(Color
                            .parseColor("#EEEEEE"));
                Vlistselector = v;

                Toast.makeText(getApplicationContext(),
                        String.valueOf(position + 1), Toast.LENGTH_LONG).show();
            }
        });

        findViewById(R.id.BLogout).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    public class Menu {
        public String date;
        public String time;
        public String cardno;
        public String amnt;
        public String tip;

        public Menu() {
            super();
        }

        public Menu(String date, String time, String cardno, String amnt,
                String tip) {
            super();
            this.date = date;
            this.time = time;
            this.cardno = cardno;
            this.amnt = amnt;
            this.tip = tip;

        }
    }

    public class MenuAdapter extends ArrayAdapter<Menu> {

        Context context;
        int layoutResourceId;
        Menu data[] = null;

        public MenuAdapter(Context context, int layoutResourceId, Menu[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            MenuHolder holder = null;

            if (row == null) {
                LayoutInflater inflater = ((Activity) context)
                        .getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new MenuHolder();
                holder.date = (TextView) row.findViewById(R.id.TvVLdate);
                holder.time = (TextView) row.findViewById(R.id.TvVLtime);
                holder.cardno = (TextView) row.findViewById(R.id.TvVLcardno);
                holder.amnt = (TextView) row.findViewById(R.id.TvVLamnt);
                holder.tip = (TextView) row.findViewById(R.id.TvVLtip);
                row.setTag(holder);

            } else {
                holder = (MenuHolder) row.getTag();
            }

            Menu Menu = data[position];
            holder.date.setText(Menu.date);
            holder.time.setText(Menu.time);
            holder.cardno.setText(Menu.cardno);
            holder.amnt.setText(Menu.amnt);
            holder.tip.setText(Menu.tip);

            return row;
        }

        public class MenuHolder {
            TextView date;
            TextView time;
            TextView cardno;
            TextView amnt;
            TextView tip;
        }
    }
}

What i want is instead of the color darker gray set a new custom color….
Have i done it the right way or is there any other way to do it?????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:26:56+00:00Added an answer on June 13, 2026 at 6:26 pm
    View listColor;//declare this at the top
    

    Do it onclick of listviewitem

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
    
    if(listColor!=null){
        listColor.setBackgroundColor(Color.BLACK);
        listColor=view;
    }else{
        listColor=view;
    }
    listColor.setBackgroundColor(Color.BLUE); //intead of blue you can change it to something you want like darkergray
    //show toast on the clicked item 
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have created custom listview by using the Base adapter. List view contains Text
I have list view with custom row layout (list_row.xml) that contains CheckBox element: <TableLayout
I have a custom list view which contains two text views and on button.
I have a custom list view that is getting data from server and changing
I have created a custom list view, each row has it's own custom selector,
I have set onItemClicklistener on my custom list view but onItemClicklistener not working I
I have created a custom xml file that contains some elements that make up
I have a LinearLayout view that already contains several elements. I want to add
I have a model that contains an IEnumerable of a list of custom objects.
I have an ExpandableListView that I want to populate with my custom views of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.