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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:08:38+00:00 2026-06-17T07:08:38+00:00

I am trying to create one android application using gridView. In this application I

  • 0

I am trying to create one android application using gridView. In this application I am trying to add images randomly in to the GridView. I have 5 images and that images added in to the drawable like R.drawable.c0, R.drawable.c1,
R.drawable.c2, R.drawable.c3,
R.drawable.c4.

While we are clicking the each grid the images in each grid will change randomly.My coding is working properly to change the images randomly in each grid click.But I need the help to get the image name.

    public class MainActivity extends Activity {
 GridView gridview;
 ImageView imageView ;

 public static Integer[] mThumbIds = {
    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
   };
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    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) {
        //gridview.getId();
         imageView = (ImageView) v;
         Random r=new Random();
            int i=r.nextInt(16);
            Log.e("i",""+i);
          imageView.setImageResource(mThumbIds[i]);

          ImageAdapter im = (ImageAdapter)parent.getAdapter();


       }

      @Override
     public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
     }



    public class ImageAdapter extends BaseAdapter {
      private Context mContext;

  // private GridItem[] items;


   public ImageAdapter(Context c) {
       mContext = c;
   }

   public int getCount() {
       return mThumbIds.length;
   }

   public Object getItem(int position) {
       return position;
   }

   public long getItemId(int position) {
       return position;
   }

   // 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(40,40 ));
           imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           imageView.setPadding(3, 3, 3, 3);
       } else {
           imageView = (ImageView) convertView;
       }

       Random r=new Random();
       int i=r.nextInt(5);

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


   }
    }
  • 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-17T07:08:40+00:00Added an answer on June 17, 2026 at 7:08 am

    Use set/getTag method of View class. You can attach any object to a view and retrieve it later.

    imageView.setImageResource(mThumbIds[i]);
    imageView.setTag(mThumbIds[i]);
    

    later

    Integer thumbId = (Integer)imageView.getTag();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an Android application that shows the current time. I want
I'm trying to create an Android application in Eclipse using the Maven plugin and
i have created one android application that was connecting to sqlite database. and i
Right now I am trying to create one Android application, assume it is going
I'm trying to create cookie from one folder that will also work in another.
I am trying to create a program that has one tableview and when you
I am trying to create application, which sould work this way: User dial a
I'm trying to create a scrollable layout in Android. Even using developers.android.com, though, I
Recently I've created one android application using eclipse and ran it using android virtual
I've been trying to create an android-application the last couple of weeks, and mostly

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.