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

  • Home
  • SEARCH
  • 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 8019143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:17:54+00:00 2026-06-04T21:17:54+00:00

I have two fragments. The first with buttons inside, the other with a ListView

  • 0

I have two fragments. The first with buttons inside, the other with a ListView inside (ListFragment).

I would like the first fragment (thanks to its buttons) to allow the user to browse the ListView which is in the second fragment.

So I want the ListView to be controlled by the first fragment with buttons.

I’ve no problem communicating between fragment (sending orders from 1st fragment to the 2nd), but I don’t know how to tell my ListView to select (programmatically) a particular list item.

What kind of ListView should I use and how can I tell the ListView to Select/Highlight/Focus one of its items?

I am in touch mode as the user presses on the buttons of the 1st fragment.

Should I use setFocusableInTouchMode(true) or setChoiceMode(ListView.CHOICE_MODE_SINGLE) or something else?

  • 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-04T21:17:56+00:00Added an answer on June 4, 2026 at 9:17 pm

    This is for everyone trying to :

    –Select programmatically an Item in a ListView

    –Making this Item stay Highlighted

    I’m working on Android ICS, I don’t know if it works for all levels Api.

    First create a listview (or get it if you’re already in a listActivity/listFragment)

    Then set the choice mode of your listview to single with :Mylistview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    Then select programmatically your item with :Mylistview.setItemChecked(position, true); (position being an integer indicating the rank of the item to select)

    Now your item is actually selected but you might see absolutely nothing because there’s no visual feedback of the selection. Now you have two option : you can either use a prebuilt listview or your custom listview.

    1) If you want a prebuilt listview, give a try to simple_list_item_activated_1, simple_list_item_checked , simple_list_item_single_choice, etc…

    You can set up your listview like this for e.g : setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_activated_1, data))

    following which prebuilt listview you chose you’ll see now that when selected you have a checkbox ticked or the backgound color changed , etc…

    2) If you use a custom listview then you’ll define a custom layout that will be used in each item. In this XML layout you will attribute a selector for each part view in you row which need to be changed when selected.

    Let’s say that when selected you want your row to change the color of the text and the color of the background. Your XML layout can be written like :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/menu_item_background_selector"
        android:orientation="horizontal" >
    
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="@drawable/menu_item_text_selector" />
    

    Now, in the drawable folder you create menu_item_background_selector.xml and menu_item_text_selector.xml.

    menu_item_text_selector.xml :

     <?xml version="1.0" encoding="utf-8"?>
    <selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_activated="true"
         android:color="#FFF">
    </item>
    
    <item android:state_pressed="true"
         android:color="#FFF">
    </item>
    
    <item android:state_pressed="false"
         android:color="#000">
    </item>
    
    </selector>
    

    The text will be white when selected.

    Then do something similar for your background: (remember that you’re not forced to use color but you can also use drawables)

    menu_item_background_selector.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    
            <item android:state_activated="true"
            android:color="#0094CE">
            </item>
    
            <item android:state_pressed="true"
            android:color="#0094CE">
            </item>
    
            <item android:state_pressed="false"
            android:color="#ACD52B">
            </item>
    
    
          </selector>
    

    Here the background is blue when selected and green when it is not selected.

    The main element I was missing was android:state_activated. There’s indeed (too) many states : activated,pressed,focused,checked,selected…

    I’m not sure if the exemple I gave with android:state_activated and android:state_pressed is the best and cleanest one but it seems to work for me.

    But I didn’t need to make my own class in order to get a Custom CheckableRelativeLayout (which was dirty and scary) nor I used CheckableTextViews. I don’t know whyothers used such methods, it maybe depends on the Api level.

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

Sidebar

Related Questions

I have two fragments, one a ListView and one a details view. Each fragment
I have create a sample Fragment application. I have two fragments like list fragment
I have a layout with two fragments. The left hand fragment is a ListFragment
I have a layout with two Fragments in it. Second one loads dynamically. Fragment
I need your help. I have one Activity with two fragments: one fragment with
I have an activity with two fragments . I am not using <fragment/> tags,
I have a layout with two fragments and both fragments have their own action
I have a ViewPager with 3 fragments into it. Everything works fine with two
I have two textures generated using a fragment shader. I want to be able
I have a HTML fragment which contains two anchor tags in various parts of

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.