Reputation: 11
I have a question about dialogfragment - I created a class with dialogfragment by tutorial, but in the tutorial, the dialogfragment was shown by button click - I want to show dialogfragment by clicking on fragment tab in my MainActivity - I can't figure out how.. I have Fragment GetItem method, where i have empty case 4 in my code - It's the fragment, where I want to open dialogfragment by clicking on it. there are my codes. Thanks for all help and answers! Sorry if something isn't clear..
-Problem is fixed (viz comments) - I created new DialogFragment - xml file and java class - codes from guides.codepath.com/android/Using-DialogFragment#build-dialog and then I added this codes to MainActivity
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
int position = tab.getPosition();
if (position == 4) {
showAlertDialog();
}
}
});
}
private void showAlertDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialogFragment alertDialog = EditNameDialogFragment.newInstance("Some title");
alertDialog.show(fm, "fragment_alert");
}
Upvotes: 0
Views: 104
Reputation: 291
see the android example :
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
Upvotes: 0
Reputation: 192043
You probably want to detect which tab is clicked and show the dialog like any other
You need to add a TabLayout.OnTabSelectedListener
Set that up
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
int position = tab.getPosition();
if (position == 4) {
// Create MediaFragment here
}
}
});
Upvotes: 1