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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:43:30+00:00 2026-06-14T22:43:30+00:00

I am developing a project in which I am required to change the color

  • 0

I am developing a project in which I am required to change the color of text in a ListView on selecting the item of the ListView.

I can change the color of text on selecting the list item but, after a few seconds, the color changes back to what it was previously. I want that color unchanged until I click on another list item. Just look at the following screenshot:

enter image description here

I can get the changed color text in white on click of item of the ListView, but after a few seconds the text changes back to the previous color.

I want to keep the white color of the textview until I click to the other item of the ListView.

The following is my selector file for textviews. For Black text:

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

For Red text:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white"/>
    <item android:state_focused="true" android:color="@android:color/white"/>
    <item android:state_selected="false" android:color="@color/red"/>
</selector>
  • 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-14T22:43:31+00:00Added an answer on June 14, 2026 at 10:43 pm

    You need to store the position of the selected item somewhere and override the getView of the adapter so that the background is set correctly when recycling the views. Also you need to tell your adapter to refresh the views when the item gets selected.

    If you use ArrayAdapter you can for instance extend it like that:

    private class SelectionArrayAdapter extends ArrayAdapter<String> {
    
        private int selectedItem = -1;
    
        public SelectionArrayAdapter(Context context, int resource, String[] objects) {
            super(context, resource, objects);
        }
    
        public void setSelection(int position) {
            selectedItem = position;
            notifyDataSetChanged();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            if(position == selectedItem) {
                view.setBackgroundColor(Color.RED);
            }
            else {
                view.setBackgroundColor(Color.TRANSPARENT);
            }
            return view;
        }
    }
    

    And when clicking an item you just need to call the setSelection method:

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelection(position);
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a Java project with Visual Paradigm which I am required to use.
I am developing a project which works with multiple arithmetic types. So I made
I'm currently developing on a project which uses some TimerJobs. One of the jobs
I am going to be developing a large project which will include a large
I am developing an android project in which I have to load from group
I am developing an android app in which I have included a library project,
I am developing a simple class library project, which will give me a dll.
I'm developing a small POS for a university project. I have a form which
We are developing customized installer using visual studio 2008 installer project. The requirement which
i am working on a project, which i am developing with Python and PyQT4.

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.