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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:16:45+00:00 2026-06-04T22:16:45+00:00

I have got an activity that shows a grid view with different images. When

  • 0

I have got an activity that shows a grid view with different images. When clicking on one of those images I want the clicked image to be the background image of another activity.

How can I do that?

This is my activity that shows the grid view:

public class HelloGridViewActivity extends Activity {

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

    GridView gridView = (GridView) findViewById(R.id.gridview);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

//WHAT SHALL I PUT HERE????
    }
}
}
  • 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-04T22:16:46+00:00Added an answer on June 4, 2026 at 10:16 pm

    So, the poster didn’t (originally) specify exactly whether the other activity was to be opened immediately on click, or if the clicked item’s image just needed to be recorded for use later. The first problem seems more common to me, as in a GridView that shows a set of image thumbnails. The user clicks on that, and a new Activity showing just that item’s information comes up. That thumbnail image maybe goes full screen. Anyway, maybe that’s not what the poster had in mind, but that’s my assumption (and probably somebody will eventually find this answer with such a use case in mind).

    It also isn’t specified how the images are stored, so I’ll make assumptions that

    1. The ImageAdapter‘s images are bundled resources of this app
    2. We can make some changes to the ImageAdapter, to store some data to solve this problem

    So, with that, I take a standard ImageAdapter, and add one line of code to record the integer resource ID for each ImageView, in the ImageView’s tag property. There’s many ways to do this, but this is the way I chose.

    public class ImageAdapter extends BaseAdapter {
       /* see code removed at 
           http://developer.android.com/resources/tutorials/views/hello-gridview.html 
        */
    
       // create a new ImageView for each item referenced by the Adapter
       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]);
          // here we record the resource id of the image, for easy access later
          imageView.setTag(mThumbIds[position]);
          return imageView;
       }
    
       // references to our images
       private Integer[] mThumbIds = {
             R.drawable.pic1, 
             R.drawable.pic2, 
             R.drawable.pic3, 
             R.drawable.pic4, 
             R.drawable.pic5 
       };
    }
    

    Then, when the Hello activity has a grid item clicked, I retrieve the image’s resource id and pass it as an Intent extra (HelloGridViewActivity.java):

       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
    
          GridView gridView = (GridView) findViewById(R.id.gridview);
    
          // Instance of ImageAdapter Class
          gridView.setAdapter(new ImageAdapter(this));
    
          final Context activity = this;
    
          gridView.setOnItemClickListener(new OnItemClickListener() {
    
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Object tag = v.getTag();
                if (tag instanceof Integer) {
                   // we stored a reference to the thumbnail image in the ImageView's tag property
                   Intent i = new Intent(activity, AnotherActivity.class);
                   Integer resourceId = (Integer)tag;
                   i.putExtra("backgroundImage", resourceId);
                   startActivity(i);
                }
             }
          });
       }
    

    And finally, when the new activity (AnotherActivity) is opened, we retrieve that intent extra, and decode it as an integer resource id, and set it as the background image with a fullscreen ImageView (AnotherActivity.java):

       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.setContentView(R.layout.another);
    
          Intent i = getIntent();
          //String bgImage = i.getExtras().getString("backgroundImage");
          int resId = i.getExtras().getInt("backgroundImage");
    
          try {
             ImageView background = (ImageView) findViewById(R.id.bgImage);
             // Another alternative, if the intent extra stored the resource 'name',
             //   and not the integer resource id
             //Class<?> c = R.drawable.class;
             //background.setImageResource(c.getDeclaredField(bgImage).getInt(c));
             background.setImageResource(resId);
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    

    I also show above some commented out code, if for some reason you need to pass a string name of an image resource, instead of an integer resource code. The commented out code looks up the resource id for the given image name. According to the resource names used in my ImageAdapter, a sample string name to pass might be "pic1". If you do this, of course, the calling Activity (HelloGridViewActivity) needs to encode the Intent extra as a String, not an integer.

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

Sidebar

Related Questions

i got the following problem. I want to have an activity thats shows me
i have problems with my game. I have an Main Activity that's shows a
I have a block of images that I want to load on my screen.
I have got my website loggin user activity into a .txt file. I want
I have an activity with two fragments: one for showing products in a grid
Have got an NSString *str = @12345.6789 and want to find out if there
i have a default activity that starts first (Activity A), and from there the
I have an application, when I see that application in Activity Monitor the applications
My problem is, that I have got a TabActivity, which has 4 Tabs right
I am doing lazyloading to display images and text. I have got everything up

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.