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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:30:31+00:00 2026-05-27T23:30:31+00:00

I want to display different images using gridview in android? Xml file: <?xml version=1.0

  • 0

I want to display different images using gridview in android?
Xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >

<GridView android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
 android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center" />

</LinearLayout>

Android[Java Code]:

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

    GridView gridview = (GridView) findViewById(R.id.gridview);
     gridview.setAdapter(new ImageAdapter(this));
          }

    public class ImageAdapter extends BaseAdapter
                  {
    private Context mContext;
    public ImageAdapter(Context c)
           {
               mContext = c;
          }

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

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

    public long getItemId(int position)
    {
    return 0;
     }
    // 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)
    {
     imageView = new ImageView(mContext);
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(6, 6, 6, 6);
    }
    else
    {
    imageView = (ImageView) convertView;
     }
    imageView.setImageResource(R.drawable.image1);
      return imageView;
                                    }
    private Integer[] mThumbIds = {
            R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4,
                       };
    }   


}

But the image1 is appear in all postion like this image is appear in grid view in row and column.but other images are not displayed.How to rectify this problem?

  • 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-27T23:30:32+00:00Added an answer on May 27, 2026 at 11:30 pm

    Its simple brother….

    public View getView(int position, View convertView, ViewGroup parent)
    {
    ImageView imageView;
                                                    if (convertView == null)
    {
     imageView = new ImageView(mContext);
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(6, 6, 6, 6);
    }
    else
    {
    imageView = (ImageView) convertView;
     }
    imageView.setImageResource(R.drawable.image1);         
      return imageView;
    

    }

    in above code change

     imageView.setImageResource(R.drawable.image1); 
    

    to

     imageView.setImageResource(mThumbIds[position]);  
    

    and make sure add number of element in mThumbs array that you want child of Gridview

    Second doubt
    after this line
    GridView gridview = (GridView) findViewById(R.id.gridview);
    use this

    gritview.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View imageview, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
    
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display images using different color maps in different figures. Following code
i want to display some information in a listview using the GridView. i have
I want to display 3 different background images according to the time of the
Im working in Java Swing using JWindows to display images... Ive made different JLabels
I want to download 3 images from 3 different urls and display them in
I want to display a different set of 2 or 3 images in a
I want to display different types of objects in the same ajax called controller
I have some user-submitted variables that I want to display in a different part
I want to popuate a listbox with objects of different types. I display the
In ListView I can change the divider image using android:divider=image but I want to

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.