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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:19:38+00:00 2026-05-23T06:19:38+00:00

Xml for the Grid layout. <GridView xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/myGrid android:layout_width=fill_parent android:layout_height=fill_parent android:padding=2dip android:verticalSpacing=10dp android:horizontalSpacing=10dp android:numColumns=auto_fit

  • 0

Xml for the Grid layout.

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myGrid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="2dip"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="148dp"
    android:stretchMode="spacingWidthUniform"
    android:gravity="center"
    />
</RelativeLayout>

imagenselect.xml for Image and checkbox.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#000080">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
 <CheckBox 
     android:id="@+id/check1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="Android" />
</LinearLayout>

Class for adding the images in gridview and displaying,

private class ImageAdapter extends BaseAdapter {

        private Context context;
        public ImageAdapter(Context localcontext){  
            context = localcontext;
        }
        public int getCount() { 
            return cursor.getCount();
        } 

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub\

    View MyView = convertView;
    ImageView picturesView;
    picturesView = new ImageView(context);

    if (convertView == null) {

     LayoutInflater li = getLayoutInflater();
     MyView =  li.inflate(R.layout.imagenselect, null);

    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(8, 8, 8, 8);
    picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));           
        }
    else {
         picturesView = (ImageView) convertView;
    }
    return picturesView;
      }
    }
}

Here with this code , I get only the images in gridview. But i want to inflate the view and use it such that i’m able to add a checkbox along with the image. (for each image a checkbox).

Since there are two views in the function “myView” and “picturesView”. If i try to typecast the picturesView to myView then i’m getting a crash. Thanks in advance !

on changing as u suggested im getting a crash.

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub\

        View myView = convertView;
        if (convertView == null) {
           LayoutInflater li = getLayoutInflater();
           myView =  li.inflate(R.layout.imagenselect, null);
            }
        ImageView picturesView;
        picturesView = new ImageView(context);
               picturesView = (ImageView) myView.findViewById( R.id.grid_item_image);

        // Move cursor to current position
        cursor.moveToPosition(position);
        // Get the current value for the requested column
        int imageID = cursor.getInt(columnIndex);
        // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
              MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

        picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        picturesView.setPadding(8, 8, 8, 8);
        picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));               

        return myView;
    }

  • 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-23T06:19:39+00:00Added an answer on May 23, 2026 at 6:19 am

    Your code actually looks really confused to me. Your code always returns an ImageView object, and never the MyView view that you inflate from your layout which contains your check box. That would explain why your check box is not appearing.

    I think that you need something along the lines of:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View myView = convertView
        if( myView == null )
        {
            LayoutInflater li = getLayoutInflater();
            myView = li.inflate(R.layout.imagenselect, null);
        }
        ImageView pictureView = (ImageView) myView.findViewById( R.id.grid_item_image );
        // initialise pictureView here.
        return myView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following layout XML file: <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/main_layout android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical> <GridView
My XML: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=match_parent android:background=#FFFFFF android:orientation=vertical > <ImageView android:id=@+id/imageMenu android:layout_width=wrap_content android:layout_height=wrap_content android:layout_alignParentLeft=true
I have the following layout: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent> <GridView android:id=@+id/grid
I am dynamically inflating following xml <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=wrap_content android:layout_height=wrap_content android:layout_margin=10dip
I have the following xml object representing a ImageButton <?xml version=1.0 encoding=utf-8?> <ImageButton xmlns:android=http://schemas.android.com/apk/res/android
I have the following code in resultlookup.xml <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:id=@+id/layoutResultOuter
I need to center grid view horizontally in my android layout.xml. I have searched
Has anybody gotten the support library to render a grid layout correctly in Android
In my Android app, I have a GridView layout with a set of Buttons.
I'm practising grid view from the book, Beginning android 2, I typed this xml

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.