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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:51:06+00:00 2026-05-23T11:51:06+00:00

I have a grid view that looks roughly like this (each image will be

  • 0

I have a grid view that looks roughly like this (each image will be a different in the end):

enter image description here

When the user clicks any image in the array, I want that image to change to this:

enter image description here

If they click again it changes to this:

enter image description here

And then clicking again reverts back to:

enter image description here

Here’s my code so far, just creating a GridView with Imageadapter:

public class GridScroll extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // CHANGE IMAGE HERE
            Toast.makeText(GridScroll.this, "" + position, Toast.LENGTH_SHORT).show();


        }
    });
}

}

And:

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.lifestyle_5,R.drawable.lifestyle_6,
        R.drawable.lifestyle_7,R.drawable.lifestyle_8,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 

};
  • 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-23T11:51:07+00:00Added an answer on May 23, 2026 at 11:51 am

    To do this, we need to do two things:

    1. Change the drawable of the item when it is clicked.
    In onItemClick(...), change the drawable for the View that is passed to you. This View will be the same one that you created in getView(...) of your adapter.

    2. Make sure that the item is shown with the correct drawable the next time it comes on screen.
    To do this, keep track of the state of each item. Every time you make a view for the item in getView(...), assign it the correct drawable for its state.


    Here is an example. I am assuming ImageAdapter is a subclass of ArrayAdapter. If not, then you will need to modify this code to work with what you are doing.

    Put these somewhere:

    private static final int WHITE = 0;
    private static final int TEAL = 1;
    private static final int MAROON = 2;
    private List<Integer> mStates = new ArrayList<Integer>();
    

    This goes in your ImageAdapter:

    // Map each state to its graphics.
    private Map<Integer, Integer> mStateResources = new HashMap<Integer, Integer>();
    mStateResources.put(WHITE, R.drawable.white);
    
    public void add(...) {
        super.add(...);
    
        // The new item will start as white.
        mStates.add(WHITE);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        //ImageView image = ...
    
        // Set the correct image for the state of this item.
        int state = mStates.get(position);
        int resId = mStateResources.get(state);
        image.setImageResource(resId);
    }
    

    In your OnItemClickListener:

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // Change the image and state for this item.
        int nextState;
        switch(mStates.get(position)) {
        case WHITE:
            nextState = TEAL;
            break;
        case TEAL:
            nextState = MAROON;
            break;
        case MAROON:
            nextState = WHITE;
            break;
        }
    
        // Set the new state and image for this item.
        mStates.put(position, nextState);
        int resId = mStateResources.get(nextState);
        image.setImageResource(resId);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Tree View that looks like this: <TreeView Grid.Column=0 Grid.Row=2 MinHeight=100 MinWidth=100
I will have to implement a calendar view/controller that looks pretty much like the
I have a data grid that looks like this <tk:DataGrid ItemsSource={Binding Parents} AutoGenerateColumns=False> <tk:DataGrid.Columns>
I have a container view that looks something like this <UserControl x:Class=Views.ContainerView> <UserControl.Resources> <ResourceDictionary>
I have a view hierarchy that looks something like this: UIView (A) UIView >
I have a table that looks something like this. EDIT - I re-extracted code.
I am using MVVM Light. I have created a window that looks like this:
I have an ASP.NET GridView which has columns that look like this: | Foo
I have used a list view that uses a grid view in WPF. I
I have a Data Grid View inside a control that is displayed in 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.