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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:45:16+00:00 2026-05-29T20:45:16+00:00

I create a ListView in my activity, with another two button (prev, next) to

  • 0

I create a ListView in my activity, with another two button (prev, next) to move a highlight on the ListView. When click the two button, I call setSelection(pos), but there’s no highlight shown on list view.

I have also tried custom the list item with layout file, and register selectors on it, as described in:
http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html

Unfortunately it is not working as expected. This method did change the color when I touch the list item, but no highlight is shown when I call setSelection().

layout/main.xml (main layout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="4"
    >
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <LinearLayout
        android:orientation="vertical"
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3">
        <Button
            android:id="@+id/btn_prev"
            android:text="prev"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:id="@+id/btn_next"
            android:text="next"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

List.java (Activity):

package com.android.list;

import android.app.Activity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TableRow;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.graphics.Point;
import android.graphics.Color;

import java.util.ArrayList;
import java.util.HashMap;

public class List extends Activity
    implements View.OnClickListener
{
    private ArrayList<HashMap<String, String>> mList;
    private Button mPrev;
    private Button mNext;
    private ListView mListView;
    private int mPosition;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        String[] ent = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"};
        for (int i=0; i<ent.length; i++)
        {
            map = new HashMap<String, String>();
            map.put("content", ent[i]);
            mList.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,
            mList, R.layout.list_item, 
            new String[] {"content"}, 
            new int[] {R.id.list_text});
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(adapter);
        mPosition = 0;
        mListView.setSelection(mPosition);

        mPrev = (Button) findViewById(R.id.btn_prev);
        mPrev.setOnClickListener(this);
        mNext = (Button) findViewById(R.id.btn_next);
        mNext.setOnClickListener(this);
    }

    public void onClick(View view)
    {
        if (view == mPrev && mPosition >= 0)
        {
            mListView.setSelection(mPosition);
            mPosition--;
        }
        else if (view == mNext && mPosition < mList.size())
        {
            mListView.setSelection(mPosition);
            mPosition++;
        }
    }
}

layout/list_item.xml (list item layout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:chess="http://schemas.android.com/apk/res/org.pengguang.chess"
    android:orientation="vertical"
    android:background="@color/list_bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView
    android:id="@+id/list_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

color/list_bg.xml (list item selector):

<?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/grey" />
<item 
    android:state_pressed="true" 
    android:drawable="@color/blue" />
<item 
    android:state_selected="true"
    android:state_pressed="false" 
    android:drawable="@color/red" />

</selector>

values/colors.xml (color file):

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color name="blue">#0303ff</color>
    <color name="grey">#f7f7f7</color>
    <color name="red">#ff0000</color>
</resources>
  • 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-29T20:45:17+00:00Added an answer on May 29, 2026 at 8:45 pm

    I haven’t try it.

    listView.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        arg1.setSelected(true);
                    }
    
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
    
                    }
    
                })
    
    • 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 uses Linkify to create a link to another activity
I want to create a layout which contains TextView , Button and ListView here
I'm trying to create a ListView with items that have two lines of text,
I have created an Activity that shows a listview and on swipe action another
i'm trying to create a ListView with a Webview inside, but the app shows
I have an Activity with a listView with few options and a button at
I'm gonna create a ListView in WPF like the below image (source: picfront.org )
I'm trying to create a table with Listview and one of the fields I'm
I am attempting to create a custom Adapter for my ListView since each item
Is it possible to create an integer (or DataTime, etc) column in ListView? It

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.