Skip to main content

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 from this blog

Circular Seek Bar - Example

MyView.java package com.rakesh.androidcircularseekbar; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MyView extends View {     /** The context */     private Context mContext;     /** The listener to listen for changes */     private OnSeekChangeListener mListener;     /** The color of the progress ring */     private Paint circleColor;     /** the color of the inside circle. Acts as background color */     private Paint innerColor;     /** The progress circle ring background */     private Paint circleRing;   ...

SSL pinning in Android - A brief discussion

HTTP protocol Communication between the client and a server typically non-encrypted or plain text while we use HTTP protocol.  Pitfall Any middle hacker can interrupt the connection between the client and server and manipulate the data as it involves no encryption. How to overcome this ? As the domain owner one can purchase a digital certificate from CA(Certificate Authority) who are considered as trusted.  A certificate will contain the Owner's name, public key , Issuer's(CA's) name,Issuer's(CA's) signature, domain details, expiry date etc . After the SSL/Leaf certificate is associated with a domain,the communication between client and server will be encrypted. Now the HTTP will become HTTPs. Note : Associating the SSL certificate means it enable the encryption between client and server but does not mean ,the domain owner will never misuse your personal information. How does SSL work ? Pitfall There is a problem here. Let's assume that there is a hacker comes i...

Android - Activity Life cycle

The android activity provides 7 call backs which will be invoked based on the states of the activity For better undersatnding,The Activity A and Activity B with the all the call backs overridden. ActivityA :  public class ActivityA extends Activity {     @Override    protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Log.e("A-OnCreate", "Object Creation");     }     public void next(View v) {         Intent i = new Intent(this, Main2Activity.class);         startActivity(i);     }     @Override    protected void onStart() {         super.onStart();         Log.e("A-onStart", "View is visible but will not be able to interact");     } ...