Reputation: 299
I have a Dialog class where I have kept my dialogs. Now the problem is that I want to get the View click listeners of my dialog back in my activity. I know this can be done by writing an interface but is there any other OOP way of doing it?
My Dialog class:
public class Dialogs{
public void testCompletionDialog() {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.test_complete_dialog);
dialog.setTitle("Ratta provet?");
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//I want my activity to know that this view is clicked.
dialog.dismiss();
}
});
dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//I want my activity to know that this view is clicked.
}
});
dialog.show();
}
}
My Activity:
if (areQueOver) {
Dialogs dialogs=new Dialogs(TestActivity.this);
dialogs.testCompletionDialog();
}
Upvotes: 2
Views: 2123
Reputation: 1637
Create an interface.
public interface OnDialogConfirmClickListener {
void onDialogConfirmClick(Class parameter//or empty);
}
Implement this interface to your activity.
public class MainActivity extends Activity implements OnDialogConfirmClickListener {
...
}
Send interface as parameter to Dialogs or testCompletionDialog method.
public void testCompletionDialog(OnDialogConfirmClickListener listener) {
...
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDialogConfirmClick(parameter//or empty);
dialog.dismiss();
}
});
...
}
Upvotes: 1
Reputation: 888
use listner for call buttons like this
Simpledialoginterface listner = new Simpledialoginterface() {
@Override
public void ok_button() {
//ok button click
}
@Override
public void cancel_button() {
//cancel button click
}
};
use this dialog
public static void popupnew(String tittle, String message, String Button, String Cancel,
final Activity context, final Simpledialoginterface listner) {
if (!((Activity) context).isFinishing()) {
android.app.AlertDialog.Builder alertDialog;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alertDialog = new android.app.AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
alertDialog = new android.app.AlertDialog.Builder(context);
}
alertDialog.setTitle(tittle);
alertDialog.setMessage(message);
alertDialog.setPositiveButton(Button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listner.ok_button();
}
});
alertDialog.setNegativeButton(Cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listner.cancel_button();
}
});
alertDialog.show();
}
}
//interface class
public interface Simpledialoginterface {
public void ok_button();
public void cancel_button();
}
popupnew("title","message","OK","Cancel",this,listner);//call dialog
Upvotes: 0
Reputation: 625
You may use it using EventBus
Inside your onClick in your Dialog class post an event telling that a dialog has been clicked. The event may contain a string variable telling which dialog is clicked.
Inside your Activity subscribe to and handle the event. You may check the String variable value to know which dialog was clicked.
Modify your Dialogs class as below:
public class Dialogs{
public void testCompletionDialog() {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.test_complete_dialog);
dialog.setTitle("Ratta provet?");
dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post("btn_marker");
dialog.dismiss();
}
});
dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post("btn_ratta");
}
});
dialog.show();
Inside your Activity:
@Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(String action){
if(action.equals("btn_ratta")){
} else if(action.equals("btn_marker")) {
}
}
inside onCreate add this-
EventBus.getDefault().register(this);
inside onDestroy add this-
EventBus.getDefault().unregister(this);
Alternative method:
Well, other than interface and EventBus, you may add a public method to your Activity say,
onDialogClicked(String dialogName){//TODO handle the click as per dialogName}
and then call this method from your onClick in your Dialogs class.
Upvotes: 2
Reputation: 5711
Yes if you want to call any method of Actvity then you can call through context of Activity :
suppose method1() is under Activity and you want to call from Dailog then you can call through .
((MyActivity)((Activity)context)).method1();
Upvotes: -2