I just making a class thats supposed to be a simple “Do you want to exit?” dialog for each of my activites in my application, and i have some questions. Im a beginner with OOP so dont be mad.
So this is my ExitDialog class:
public class ExitDialog extends Dialog implements OnClickListener
{
private Button dialogOk;
private Button dialogCancel;
private TextView dialogText;
public ExitDialog(Context context)
{
super(context);
final Dialog dialog = new Dialog(context, R.style.DialogAnim);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.exitdialog);
dialogOk = (Button)dialog.findViewById(R.id.dialogOk);
dialogCancel = (Button)dialog.findViewById(R.id.dialogCancel);
dialogText = (TextView)dialog.findViewById(R.id.dialogText);
//How to reach any reference from R.java ?
//
//dialogOk.setText(getString(R.string.Yes));
//ialogText.setText(getString(R.string.Exit));
dialogOk.setOnClickListener(this);
dialogCancel.setOnClickListener(this);
dialog.show();
}
@Override
public void onClick(View v)
{
//Many people said on answers, that i must use **getId()** to compare
//these two views, but i can do just like this, bacause i got the message in logcat!
//but the dismiss() just not get called...
if(v == dialogOk)
{
Log.i("ExitDialog", "dialogOk clicked");
this.dismiss();
}
}
}
I have 3 questions for you:
How can i reach my application’s R.java file for String references?
As you see i commented out the getString(R.string.Yes) and getString(R.string.Exit) functions because i cannot use it in this outer class.
Any suggestions about who can i do this?
Second question is about .dismiss(). If i call this.dismiss(), my dialog just dont go away it is stays on screen, why is it occurs? How to dismiss then?
Third question is: How to get the parent activity from this outer dialog class? I need it to call .finish() on it, so my app can exit.
Any suggestions will be greatly appreciated.
Thanks.
Don’t extend your activity by Dialog,extend it by activity.Call your dialog activity like u call normal activity.But in manifest file add below line under your dialog activity:
and this above setcontentView of activity:
You will be able to call Resource and dismiss() function normally as you do for rest of the activities.
Dialog Activity: