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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:00:50+00:00 2026-06-06T07:00:50+00:00

I have a ListView that sits on the left side of a tablet-size screen.

  • 0

I have a ListView that sits on the left side of a tablet-size screen. My goal was to give it a solid background with a border on the right, then apply an overlapping background on the list element to break that border so that it appears to be a part of the view on the right.


The ListView Background

I achieved the right border using a <layer-list> drawable as suggested by Emile in another question:

rightborder.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/black" />
        </shape>
    </item>
    <item android:right="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>

</layer-list>

…and here’s the ListView definition for good measure:

<ListView
    android:id="@+id/msglist"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:divider="@color/black"
    android:dividerHeight="1dp"
    android:background="@drawable/rightborder"
    android:paddingRight="0dip">
</ListView>
<!-- I added the android:paddingRight after reading something 
about shape drawables and padding, don't think it actually
did anything. -->

Attempting to override it with a color

In order to achieve the desired effect, I placed the following in the getView function of my adapter:

//If it's selected, highlight the background
if(position == mSelectedIndex)
    convertView.setBackgroundColor(R.color.light_gray);

else
    convertView.setBackgroundResource(0);

However, using this method, the black border of the ListView‘s drawable remained visible, and only the white part of the background was replaced by gray. Here’s a screen capture:

Border showing through color background


Fixing it with a drawable

On a hunch, I replaced the color I was assigning with a shape drawable:

selectedmessage.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/light_gray" />
</shape>

getView snippet:

//If it's selected, highlight the background
if(position == mSelectedIndex)
    convertView.setBackgroundResource(R.drawable.selectedmessage);

else
    convertView.setBackgroundResource(0);

This achieves the desired result, as shown below:

Border no longer showing


The question:

Why does assigning a rectangle as the background for my ListView element cover the entire view, while assigning the color allows the black border to show through? I’m happy it’s working, but I’d like to know why Android is rendering the view this way so I can learn more about how Android renders Views.

Other notes:

  • I’m running the project in the stock Android 3.2 emulator, if that makes any
    difference.
  • One clue may be that the light_gray color background seems to render darker than the light_gray shape resource.
  • I doubt it makes a difference, but light_gray is:

    <color name="light_gray">#FFCCCCCC</color>

  • 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-06T07:00:51+00:00Added an answer on June 6, 2026 at 7:00 am

    You can’t do this:

     convertView.setBackgroundColor(R.color.light_gray);
    

    setBackgroundColor does not take a resource id : http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)

    So your getting some incidental behaviour that isn’t doing what your expecting.

    You would have to do:

     convertView.setBackgroundColor(getResources().getColor(R.color.light_gray);
    

    http://developer.android.com/reference/android/content/res/Resources.html#getColor(int)

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

Sidebar

Related Questions

I have a listview that contains log messages. I want to set the background
I have created a listview that sits inside a larger activity (the list view
I have a listview that is binded to a ThreadSafeObservableCollection. The background of each
I have a ListView that is sandwiched between two buttons. The entire screen is
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have ListView that uses a GridView to display several columns of data. Two
I have a ListView that shows around 300 items. When something is changed and
I have a listview that highlights a row when you mouseover it with the
I have a listview that contain several EditTexts created dynamically according to the number
I have a ListView that displays a link button. I need the link button

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.