Dinesh Vadivel
Dinesh Vadivel

Reputation: 98

popwindow inside android Fragment

I have an activity with fragment, inside the fragment have button, on click of that i would like to show a popup window with custom layout. enter image description here

Here is the Fragment code sample

public class TabContent extends Fragment {
JSONArray jArray;
private ImageView ime;
private GridView gridView;
private GridViewAdapter gridAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tabcontent, container, false);
    Bundle bundle = getArguments();
    gridView = (GridView) view.findViewById(R.id.gridView);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageItem item = (ImageItem) parent.getItemAtPosition(position);
            try {
                final JSONObject menuData = (JSONObject) dobj.getMenuData(TabPosition,position);
                Log.i("item", String.valueOf(menuData));


                // here i want to trigger the popupwindow


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });


    return view;
}

Upvotes: 0

Views: 104

Answers (2)

dindinii
dindinii

Reputation: 655

      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());

 LayoutInflater inflater = getActivity().getLayoutInflater();
  View customView=inflater.inflate(R.layout.yourxml, null);
   dialogBuilder.setView(customView);

      final AlertDialog alertDialog = dialogBuilder.create();
                            alertDialog.setCancelable(false);
                            alertDialog.setCanceledOnTouchOutside(false);
            //Intialize your view components here
      example: TextView txtsample=(TextView)customView.findViewById(R.id.yourtxtviewid);



     alertDialog.show();

Upvotes: 1

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

As in the tutorial androidbegin.com/tutorial/android-dialogfragment-tutorial

Use below to open the Dialog Fragment

FragmentManager fm = getSupportFragmentManager();
    AlertDFragment alertdFragment = new AlertDFragment();
                    // Show Alert DialogFragment
                    alertdFragment.show(fm, "Alert Dialog Fragment");

Below is the code of Dialog Fragment which can have a custom layout, dialogfragment is the XML file in which you can have custom Layout

 public class DFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialogfragment, container,
                false);
        getDialog().setTitle("DialogFragment Tutorial");        
        // Do something else
        return rootView;
    }
}

Upvotes: 0

Related Questions