Catalin Ghita Catalin Ghita - 2 months ago 11
Android Question

How to inflate a layout in GridView adapter

I have populated a

with some

I am looking for a way to inflate a new layout (let's call it
) that would occupy only a portion of the screen and this event should be triggered when clicking a button placed in a certain
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
as well as providing new options for the users through new buttons in the
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.

     public class BaseDialogFragment extends DialogFragment {

            Typeface MR, MRR;

            public void onViewCreated(View view, Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);


            public void onStart() {


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


            public void onResume() {

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


            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;