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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:19:31+00:00 2026-05-18T22:19:31+00:00

I have a listview defined by the following xml. I need to toggle the

  • 0

I have a listview defined by the following xml. I need to toggle the image in the list during runtime when the user clicks on any row. How can I achieve this? Any help is highly appreciated. Thanks

//list_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" 
android:id="@+id/img"
/> 
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="@+id/txt"
/>
</LinearLayout>
  • 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-18T22:19:32+00:00Added an answer on May 18, 2026 at 10:19 pm

    You can locate the item’s img view by calling findViewById on the second parameter (which is the View of the clicked item) within the onItemClick handler:

    public void onItemClick(AdapterView parentView, View clickedItemView, int pos, long id)
    {
        ImageView imageView = (ImageView) clickedItemView.findViewById(R.id.img);
        // ...
    }
    

    EDIT: Be aware that Android reuses list item View objects (also called view recycling), so the toggle state of each item needs to be stored for later use. The toggle state of each item needs to be accessed whenever a list item view is bound to a list item for display.

    For example, here is working example of an activity that toggles the image of each item on click:

    import java.util.Arrays;
    import java.util.List;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    
    public class SO4539968TestCaseActivity extends Activity {
        private static final List<String> ITEM_TEXTS = Arrays.asList(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"});
    
        private boolean[] itemToggled;
    
        private class MyArrayAdapter<T> extends ArrayAdapter<T>
        {
            public MyArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
                super(context, resource, textViewResourceId, objects);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View itemView = super.getView(position, convertView, parent);
                ImageView imageView = (ImageView) itemView.findViewById(R.id.img);
                imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off);
                return itemView;
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            itemToggled = new boolean[ITEM_TEXTS.size()];
            Arrays.fill(itemToggled, false);
    
            ListView listView = (ListView) findViewById(R.id.list_view0);
            listView.setAdapter(new MyArrayAdapter<String>(this, R.layout.list_item, R.id.txt, ITEM_TEXTS));
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) {
                    itemToggled[position] = ! itemToggled[position];
    
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.img);
                    imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off);
                }
            });
        }
    }
    

    The important parts to study are the onItemClick callback and the override of getView in MyArrayAdapter.

    The getView method of the Adapter class is responsible for inflating the item layout. In this example, I call the getView method of the superclass to initially prepare the item view, but then I make sure to appropriately set the resource ID of the item’s img view:

    imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off);
    

    See also: Developing Applications for Android – Gotchas and Quirks

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

Sidebar

Related Questions

I have defined a List View in xml as below <ListView android:id=@+id/mylist android:layout_width=fill_parent android:layout_height=wrap_content
I have a listView which defined like this in xml: <ListView android:id=@android:id/list android:layout_width=match_parent android:layout_height=match_parent
I have a listView with custom objects defined by the xml-layout below. I want
I have the following layout. This defines a row in my ListView. I noticed
I have defined in XAML a list view, see following fragment: <Grid> <Button Content=_Generate
I have the following layout defined in XML: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_height=wrap_content
I have the following selector defined in an XML file under res/color/redeemlist_item_color.xml : <?xml
I have the following window, which displays a listview. I defined a style for
Possible Duplicate: WPF Listview Access to SelectedItem and subitems I have a listview defined
I have listview control.There is an option to remove selected items.After the user removes

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.