Catalin Ghita Catalin Ghita - 2 months ago 11
Android Question

How to inflate a layout in GridView adapter

I have populated a

GridView
with some
grid_items
.

I am looking for a way to inflate a new layout (let's call it
pop_up_layout.xml
) that would occupy only a portion of the screen and this event should be triggered when clicking a button placed in a certain
grid_item
view.
I know I have to place the functionality in the adapter at the current item's position.

My purpose is to display more info about the
grid_item
as well as providing new options for the users through new buttons in the
pop_up_layout.xml
that is being inflated.

I have searched and skimmed through most of the questions related in SO as well as websites but I seem to find solutions only to display pop-up views.

Can someone point me in the right direction? Would appreciate some sample-code for better understanding (if considered necessary).

Something like this is what I am looking for.

Thank you in advance.

Answer Source

You can use DialogFragment for your case inflating layout is not the perfect answer.

To open the fragment you can use in the on click of your grid item

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        BaseDialogFragment Fragment = new BaseDialogFragment();
        transaction.add(fragment, "loading");
        Bundle args = new Bundle();
        args.putString("your_data", yourdata);
// you can add as many string int or boolean. Explore it.
        fragment.setArguments(args);
        transaction.commitAllowingStateLoss();





     public class BaseDialogFragment extends DialogFragment {

            Typeface MR, MRR;

            @Override
            public void onViewCreated(View view, Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                getDialog().setCanceledOnTouchOutside(false);



            }

            @Override
            public void onStart() {

                super.onStart();

                Window window = getDialog().getWindow();
                WindowManager.LayoutParams windowParams = window.getAttributes();
                windowParams.dimAmount = 0.50f;

                window.setAttributes(windowParams);
            }

            public void onResume() {

                super.onResume();
                WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
                params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
                params.gravity = Gravity.CENTER;
                getDialog().getWindow().setAttributes(params);



            }

            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

                View v = inflater.inflate(R.layout.yourlayout, container, false);

                // init your views here. or 
get the data here from the Grid View
                String your_data = getArguments().getString("your_data");

                return v;
            }
        }