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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:48:09+00:00 2026-06-15T10:48:09+00:00

I need to highlight a listview item when i touch it and to stay

  • 0

I need to highlight a listview item when i touch it and to stay highlighted. I tried everything I found but nothing worked. Here is my code:

This is the listview:

<ListView
    android:id="@+id/lvUsers"
    android:layout_width="300dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:background="@drawable/border2"
    android:choiceMode="singleChoice"
    android:padding="5dp" >
</ListView>

The @drawable/border2 is just a border around the listview.

This is the listview item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutUsersRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvAllUsersName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="User name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

This is list_selector_background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/blue" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/blue" /> <!-- pressed -->
    <item android:drawable="@color/transparent" /> <!-- default -->
</selector>

This is the code from the listview adapter

public class UsersAdapter extends BaseAdapter implements OnClickListener {

LoginActivity context;
private List<String> listOfUsers;

public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
    context = _context;
    this.listOfUsers = listOfUsers;
}

public int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    String entry = listOfUsers.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row, null);
    }

    TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(entry);

    LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(false);
    LinLayout.setFocusable(false);
    LinLayout.setOnClickListener(this);

    return convertView;
}

public void onClick(View v) {
    // get the row the clicked button is in
    LinearLayout row = (LinearLayout) v;

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}
}

This is not working. It only highlights the item while it’s pressed. As soon as i release it, the highlight is gone.

HERE IS THE SOLUTION

Remove the android:background="@drawable/list_selector_background" line from the listview item layout. The selector is not needed at all. And here is the adapter’s code:

public class UsersAdapter extends BaseAdapter {

LoginActivity context;
private List<String> listOfUsers;
boolean[] arrBgcolor;
private int blue = Color.BLUE;
private int transparent = Color.TRANSPARENT;

public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
    context = _context;
    this.listOfUsers = listOfUsers;

    arrBgcolor = new boolean[listOfUsers.size()];
    resetArrbg();

}

private void resetArrbg() {
    for (int i = 0; i < arrBgcolor.length; i++) {
        arrBgcolor[i] = false;                  
    }
}

public int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    String entry = listOfUsers.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row, null);
    }


    TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(entry);

    LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(false);
    LinLayout.setFocusable(false);
    LinLayout.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            handleClick(v, position);
        }
    });

    if (arrBgcolor[position])
        LinLayout.setBackgroundColor(blue);
    else 
        LinLayout.setBackgroundColor(transparent);

    return convertView;
}

public void handleClick(View v, int position) {
    LinearLayout row = (LinearLayout) v;

    resetArrbg();
    arrBgcolor[position] = true;
    notifyDataSetChanged();

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}

}
  • 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-15T10:48:10+00:00Added an answer on June 15, 2026 at 10:48 am

    remove the selector and try the following:

    EDIT

    public class UsersAdapter extends BaseAdapter implements OnClickListener {
    
            LoginActivity context;
            private List<String> listOfUsers;
            boolean[] arrBgcolor;
            private int blue = Color.BLUE;
    
            public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
                context = _context;
                this.listOfUsers = listOfUsers;
    
                arrBgcolor = new boolean[listOfUsers.size()];
                resetArrbg();
    
            }
    
            private void resetArrbg() {
                for (int i = 0; i < arrBgcolor.length; i++) {
                    arrBgcolor[i] = false;                  
                }
            }
    
            ......
    
            public View getView(final int position, View convertView, ViewGroup parent) {
                String entry = listOfUsers.get(position);
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.users_row, null);
                }
    
    
                TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
                UserName.setText(entry);
    
                LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
                LinLayout.setFocusableInTouchMode(false);
                LinLayout.setFocusable(false);
                LinLayout.setOnClickListener(this);
    
                //add the following, the position is accessible from here
                if (arrBgcolor[position]) {
                    convertView.setBackgroundColor(blue);
                }
                convertView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        resetArrbg();
                        arrBgcolor[position] = true;
                        notifyDataSetChanged();
                    }
                }); 
                return convertView;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview and I need to highlight a item programmatically. I tried
I need to highlight source code in LaTeX. The package listings seems to be
I need to highlight a column in my bar chart. I found this example
I write articles on my blog and I need to highlight the source code(java,
I've come with a problem. I need to highlight the selected item within a
I am using CheckedListBox , but I need to highlight individual items with different
I need to highlight line with caret in NSTextView using CALayer overlay and grab
I need to highlight changes(diff) between 2 database text fields in a asp.net application.
I'm programming an application in Struts and I need to highlight some reminders in
I have a regular texbox control. I need to highlight some words with a

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.