Skip to content Skip to sidebar Skip to footer

How Can I Create Custom Alert Dialog With Grid View In Android?

How can I create a Alert Dialog with a GridView as shown in the image above?

Solution 1:

Here is a simple implementation: Call this method in your code inside activity.

privatevoidshowAlertDialog() {
        // Prepare grid viewGridViewgridView=newGridView(this);

        List<Integer>  mList = newArrayList<Integer>();
        for (inti=1; i < 36; i++) {
            mList.add(i);
        }

        gridView.setAdapter(newArrayAdapter(this, android.R.layout.simple_list_item_1, mList));
        gridView.setNumColumns(5);
        gridView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
            @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
                // do something here
            }
        });

        // Set grid view to alertDialog
        AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
        builder.setView(gridView);
        builder.setTitle("Goto");
        builder.show();
    }

Solution 2:

You can use popup window

import android.widget.PopupWindow;


        private PopupWindow mpopup;

    // getting the layout of the popup view . in this case it is about.xmlfinalViewpopUpView= getLayoutInflater().inflate(R.layout.about, null,false);


                 mpopup = newPopupWindow(popUpView, 400, 500, true); // here 400 and 500 is the height and width of layout
                 mpopup.setAnimationStyle(android.R.style.Animation_Dialog);  
                 //location of popup view on the screen
                 mpopup.showAtLocation(popUpView, Gravity.CENTER, 0, 0);


        // if you have button in the xml file of about.xml
        Button cancel=(Button)popUpView.findViewById(R.id.close1);
        cancel.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View arg0) {
               // to dismiss popup();
                mpopup.dismiss();

            }
        });

and here R.layout.about is an xml file where you will put your grid view inside and other stuff

Post a Comment for "How Can I Create Custom Alert Dialog With Grid View In Android?"