[ACCEPTED]-handle button clicks in a DialogFragment-android-dialogfragment
This is the code for a Dialog I'm using 11 (the actual GUI for the dialog is defined 10 in the layout resource confirm_dialog.xml):
public class ConfirmDialog extends DialogFragment {
public static String TAG = "Confirm Dialog";
public interface ConfirmDialogCompliant {
public void doOkConfirmClick();
public void doCancelConfirmClick();
}
private ConfirmDialogCompliant caller;
private String message;
public ConfirmDialog(ConfirmDialogCompliant caller, String message){
super();
this.caller = caller;
this.message = message;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doOkConfirmClick();
}
});
((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doCancelConfirmClick();
}
});
return view;
}
}
The 9 dialog is created with the following lines
confirm_dialog = new ConfirmDialog(this, message);
confirm_dialog.show(getActivity().getSupportFragmentManager(), ConfirmDialog.TAG);
The 8 interface definition is used to assure that 7 the caller (Fragment or Activity) implements 6 the methods to handle the events thrown 5 by the controller. That is, a Fragment or 4 Activity calling this dialog must implement 3 the given interface.
Maybe there is a better 2 solution but this is the one I figured out. Hope 1 it helps!
here is an example to handel a cancel button 2 click on a dialog from the FragmentDialog 1 class:
i used android.support.v4.app.DialogFragment;
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment(){}
public static String TAG = "info Dialog";
Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.info_layout, container);
getDialog().requestWindowFeature(STYLE_NO_TITLE);
btn=(Button)view.findViewById(R.id.close_dialog_btn_info_layout);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getDialog().dismiss();
}
});
return view;
}
}
Do along these lines
Dialog dl = mDialogFragment.getDialog();
Button btn = dl.findViewById(R.id.btn);
btn.setOnClickListener(this);
0
Another option is to let your custom DialogFragment
class 3 implement OnClickListener
. Then you just setOnClickListener
for whatever 2 views you want to handle clicks on and catch 1 the clicks in onClick
.
// 1. implement OnClickListener
public class MyDialogFragment extends DialogFragment implements View.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_dialog_layout, null);
// 2. set click listeners on desired views
view.findViewById(R.id.my_view_1).setOnClickListener(this);
view.findViewById(R.id.my_view_2).setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
// ...
return builder.create();
}
// 3. capture the clicks and respond depending on which view
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.my_view_1:
// do something
break;
case R.id.my_view_2:
// do something
break;
default:
break;
}
}
}
Its easy in activity but in DialogFragment 7 we do some more codes.
Inside the DialogFragment 6 class do your rutin findView on onActivityCreated
method
btn_ocak = (Button) view.findViewById(R.id.btn_cal_ocak);
btn_subat = (Button) view.findViewById(R.id.btn_cal_subat);
btn_mart = (Button) view.findViewById(R.id.btn_cal_mart);
btn_nisan = (Button) view.findViewById(R.id.btn_cal_nisan);
btn_ocak.setOnClickListener(this);
btn_subat.setOnClickListener(this);
btn_mart.setOnClickListener(this);
btn_nisan.setOnClickListener(this);
Implement 5 onClick OnClickListener to your class
public class CalendarPopUp extends DialogFragment implements View.OnClickListener
and 4 do what you want inside onClick method, by 3 doing these we activated the onClick events 2 of our views
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_cal_ocak:
seciliAy = "Ocak";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_subat:
seciliAy = "Subat";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_mart:
seciliAy = "Mart";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_nisan:
seciliAy = "Nisan";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_mayis:
seciliAy = "Mayıs";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_haziran:
seciliAy = "Haziran";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_temmuz:
seciliAy = "Temmuz";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_agustos:
seciliAy = "Agustos";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_eylul:
seciliAy = "Eylül";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_ekim:
seciliAy = "Ekim";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_kasim:
seciliAy = "Kasım";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
case R.id.btn_cal_aralik:
seciliAy = "Aralık";
setMonthOnShare(seciliAy);
mCallback.onSelectedData(seciliAy);
dismiss();
break;
default:
break;
}
}
and if you wonder how to pass 1 values follow these step clike_here
or
class MyDialog extends DialogFragment {
public View.OnClickListener onButtonOk;
public EditText edit_text;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(builder.getContext());
View view = li.inflate(R.layout.custom_dialog, null);
Button buttonOK = view.findViewById(R.id.button_ok);
edit_text = view.findViewById(R.id.edit_text);
buttonOk.setOnClickListener(onButtonOk);
builder.setView(view);
return builder.create();
}
}
// use
final MyDialog dialog=new MyDialog();
dialog.onButtonOk=new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), dialog.edit_text.getText(), Toast.LENGTH_SHORT).show();
}
};
dialog.show(getSupportFragmentManager(),null);
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.