Gridview with Custom Dialog

GridView with CustomDialog Here i'm giving example on GridView with Custom Dialog .

main.xml: 


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

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

</LinearLayout>




  custom.xml: 


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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>



GridviewActivity.java:

package com.gridview;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class GridviewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gv=(GridView)findViewById(R.id.gridView1);
        
        gv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                        final Dialog custom=new Dialog(GridviewActivity.this);
                        custom.setTitle("image"+(arg2+1));
                        custom.setContentView(R.layout.custom);
                        ImageView iv=(ImageView)custom.findViewById(R.id.imageView1);
                        iv.setImageResource(image[arg2]);
                        custom.show();    
                        iv.setOnClickListener(new OnClickListener() {
                            
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                custom.dismiss();
                            }
                        });
            }
        }); 
        
        
        MyAdapter adapter=new MyAdapter(this);
        gv.setAdapter(adapter);
    }
    
    class MyAdapter extends BaseAdapter{
        Context c;
        public MyAdapter(Context c) {
            // TODO Auto-generated constructor stub
            this.c=c;
        }
        public int getCount() {
            // TODO Auto-generated method stub
            return image.length;
        }
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
        public View getView(int position, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            ImageView iv=new ImageView(c);
            iv.setImageResource(image[position]);
            iv.setScaleType(ScaleType.FIT_XY);
            iv.setLayoutParams(new GridView.LayoutParams(100, 100));
            
            return iv;
        }
        
    }
           int[] image={R.drawable.laptop1,R.drawable.laptop2,R.drawable.laptop3
                   ,R.drawable.laptop4,R.drawable.laptop5,R.drawable.laptop6,
                   R.drawable.laptop7,R.drawable.laptop8,R.drawable.laptop9,R.drawable.laptop10};
}


Comments

  1. Hi, How can i have a gridview in AlertDialog?

    ReplyDelete
  2. You have create your own layout file ...

    Example :
    grid.xml



    then have to create the dialog like below,

    Dialog gridViewDialog=new Dialog(context);
    gridViewDialog.setContentView(R.layout.grid);
    // You have to create the GridView object with gridViewDialog
    GridView gv =((GridView)gridViewDialog.findViewById(R.id.gridview));
    gv.setAdapter(yourAdapter);
    gridViewDialog.show();

    Please try this and let me know .

    ReplyDelete

Post a Comment

Popular Posts