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 6931437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:34:38+00:00 2026-05-27T11:34:38+00:00

i have been all over the web, stackoverflow included and just can’t seem to

  • 0

i have been all over the web, stackoverflow included and just can’t seem to get a clear complete way to

I want to create a ListView that

1) has alternating colors (I am able to do that with code below)
2) retains the default orange selection behavior of android

to accomplish #1 I have an custom adapter that
extends ArrayAdapter and then I override getView like so

public View getView(int position,  View convertView,   ViewGroup parent)
{
  ....

  // tableLayoutId is id pointing to each view/row in my list
  View tableLayoutView = view.findViewById(R.id.tableLayoutId); 
  if(tableLayoutView != null)
  {
      int colorPos = position % colors.length;
      tableLayoutView.setBackgroundColor(colors[colorPos]);
  }
}

my member variable for colors is

private int[] colors = new int[] { 0x30ffffff, 0x30ff2020, 0x30808080 };

followed the article “Android – Applying Alternate Row Color in ListView with SimpleAdapter” found here

now this is where i am stuck, I see on stackoverflow some mention of doing this as it would see common, and they suggest adding this attribute to the

android:listSelector=”@color/list_item”

where list_item.xml would be something like

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="true"
    android:drawable="@drawable/transparent" />
   .....
 </selector>

Then I would have to add code to getView() to figure out which state I am in
and act accordingly.

Is there an example out there for getting this to work? Thanks all
I’ll gladly post mine for all to use if i could get it to work. 🙁

  • 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-05-27T11:34:39+00:00Added an answer on May 27, 2026 at 11:34 am

    A workaround is to use 2 selectors. From your adapter, instead of setting 2 colors, you set 2 selectors.

    if (position % 2 == 0) {
      view.setBackgroundResource(R.drawable.selector_1);
    } else {
      view.setBackgroundResource(R.drawable.selector_2);
    }
    

    selector_1 is defined in selector_1.xml like this:

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

    selector_2 is defined in selector_2.xml like this:

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

    So that, you have a bi-color listview and a third color/shape/whatever-you-want for selected item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching the web and all over stackoverflow, but yet couldn't find
I've been all over the web trying to find a way to get VIM
I have been all over the web including Apple's dev site but I can't
Hey all, I have been doing nothing but web development over the last few
I have been looking all over the web for the simplest solution for this,
I have been searching all over the web trying to find an example of
I have been all over the place, seems the UITableView with a static background
I have been search all over the net and couldn't find an appropriate solution
I've been looking all over for a simple example of how to have an
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/

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.