Search code examples
androiddialogandroid-gridlayout

How to create dialog with GridLayout?


How to create Dialog, which uses GridLayout and when a user clicks on the item of this layout and then click "choose" button. The item of GridLayout consist of an ImageView. How to realize this Dialog?


Solution

  • 1 -Make the custom layout you want

    2 - make myDialog class which extends DialogFragment and make it inflate the layout you made

    3-in the fragment add the functionality you want

    But I am using gridview , gridlayout is simpler but i suggest using gridview

    Here the Dialog class

    public class myDialog extends DialogFragment implements dis_listener {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_dialog_layout,container,false);
        GridView gv = view.findViewById(R.id.dialog_grid);
    
        grid_adapt adapt = new grid_adapt(getActivity(),this);
        gv.setAdapter(adapt);
        return view;
    
    
    
    }
    @Override
    public void listen() {
        this.dismiss();
    }
    }
    

    grid_adapt :-

    public class grid_adapt extends BaseAdapter {
    private Context cxt;
    //pass the data you want
    //private ArrayList<Integers> imgs
    private dis_listener img_dismiss;
    grid_adapt(Context context,dis_listener dismisser){
        this.cxt = context;
        this.img_dismiss = dismisser;
    }
    @Override
    public int getCount() {
        //return the data length
        //return imgs.length
        return 3;
    
    }
    
    @Override
    public Object getItem(int position) {
        //return imgs.get(position);
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder vh;
        if(convertView == null) {
            convertView= LayoutInflater.from(cxt).inflate(R.layout.grid_item, parent, false);
            vh = new viewHolder(convertView);
            convertView.setTag(vh);
    
        }else{
            vh = (viewHolder) convertView.getTag();
        }
        vh.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(cxt,"Open",Toast.LENGTH_LONG).show();
            }
        });
         vh.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_dismiss.listen();
            }
        });
        return convertView;
    }
    static class viewHolder{
        private Button button;
        private ImageView img;
        viewHolder(View view){
            button = view.findViewById(R.id.dialog_item_button);
            img = view.findViewById(R.id.img);
        }
    
    }
    

    }

    The interface has only one method called listen

    result: enter image description here