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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:38:34+00:00 2026-06-11T22:38:34+00:00

I know it sounds very simple, and there are questions about this. But none

  • 0

I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:

I want to change background color of a list item in a ListActivity when user clicks on it, and change it back to original color when user clicks again (i.e. Select/Unselect item sort of look)

I tried using getChildAt, it works perfectly if I have all the items visible in one screen without having to scroll.

Code:

getListView().getChildAt(position).setBackgroundColor(Color.CYAN);

The problem begins when I have more items in the list and user has to scroll through them. Once background for an item is changed, The background color shows up on the newly visible items as I scroll. Also, the getChildAt(position) returns null (and hence a NullPointerException) when clicking again on the item.

Can anyone please help me with a simple code that helps me change background color of a list item?

Thanks in advance!

  • 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-11T22:38:35+00:00Added an answer on June 11, 2026 at 10:38 pm

    Sure thing. I would do this in the getView() method of a custom ListAdapter.

    MyAdapter extends SimpleAdapter {
        private ArrayList<Integer> coloredItems = new ArrayList<Integer>();
    
        public MyAdapter(...) {
            super(...);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
    
            if (coloredItems.contains(position)) {
                v.setBackgroundColor(Color.CYAN);
            } else {
                v.setBackgroundColor(Color.BLACK); //or whatever was original
            }
    
            return v;
        }
    }
    

    Update coloredItems when a list item is clicked.

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        if (coloredItems.contains(position)) {
            //remove position from coloredItems
            v.setBackgroundColor(Color.BLACK); //or whatever was original
        } else {
            //add position to coloredItems
            v.setBackgroundColor(Color.CYAN);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds a little bizarre, but there is a very simple application
I know it sounds very basic, but I am new to Javascript. Here is
I know there is this question that is very similar, but I wanted to
I know it probably sounds like very trivial question but I couldn't find any
I know the title makes this sound very easy, but I have a For
i know this sounds really common and so trivial but , am having a
I know this sounds like a homework assignment, but it isn't. Lately I've been
I know this sounds somewhat counterintuitive, but let me explain what I am trying
I know this sounds like a dumb question but I need to ask this.
Do browser vendors optimize against jQuery? I know this sounds absurd anti-standard, but I

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.