Skip to content

Instantly share code, notes, and snippets.

@kunmi
Last active December 12, 2018 00:00
Show Gist options
  • Save kunmi/7a5ce2a4780e9107ebaadf8d9bd72dcb to your computer and use it in GitHub Desktop.
Save kunmi/7a5ce2a4780e9107ebaadf8d9bd72dcb to your computer and use it in GitHub Desktop.
Custom Tabbed DialogFragment with alertdialog title, positive and negative buttons
public class TabbedDialog2 extends DialogFragment {
TabLayout tabLayout;
ViewPager viewPager;
View v = null;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity())
.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
);
dialogBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialogBuilder.setTitle("Add Beacon");
//dialogBuilder.setCancelable(true);
// call default fragment methods and set view for dialog
if(v==null)
v = onCreateView(getActivity().getLayoutInflater(), null, null);
onViewCreated(v, null);
dialogBuilder.setView(v);
AlertDialog dg = dialogBuilder.create();
//dg.setCanceledOnTouchOutside(false);
return dg;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(v==null) {
v = inflater.inflate(R.layout.dialog_sample,container,false);
tabLayout = (TabLayout) v.findViewById(R.id.tabLayout);
viewPager = (ViewPager) v.findViewById(R.id.masterViewPager);
CustomAdapter adapter = new CustomAdapter(getChildFragmentManager());
adapter.addFragment("Boy",CustomFragment.createInstance("John"));
adapter.addFragment("Girl",CustomFragment.createInstance("Stacy"));
adapter.addFragment("Robot",CustomFragment.createInstance("Aeon"));
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
return v;
}
}
@exzant
Copy link

exzant commented Jul 7, 2018

Awesome, great code Kunmi.
Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment