0

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.

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

5 Answers5

2

First Question:

context.getString(R.string.exit);

Third Question:

((Activity) context).finish();

For question 2 I think your if isn't resulting to true. I wouldn't compare a View by it's memory address. I think dialogOk should be null in the onClick listener.

s.krueger
  • 1,043
  • 8
  • 13
2

It is easy to get the string:

String a = context.getResources().getString(R.string.myString);

You do not use dismiss() within this class but within your activity when you create an instance of this class.

I think you are comlicating things by extending dialog class. Here is how to create a custom dialog http://developer.android.com/guide/topics/ui/dialogs.html

If you really want freedom of creating a custom dialog then use a transparent activity and startActivityForResult How do I create a transparent Activity on Android?

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67
1

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:

android:theme="@android:style/Theme.Dialog"

and this above setcontentView of activity:

requestWindowFeature(Window.FEATURE_NO_TITLE);

You will be able to call Resource and dismiss() function normally as you do for rest of the activities.

button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent=new Intent(YourActivityName.this,DialogClass.class);
                startActivity(intent);

            }
        });

Dialog Activity:

public class DialogClass extends Activity{
    Button button;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom);

        button=(Button) findViewById(R.id.cancel);//belongs to xml file
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();
            }
        });

        textView=(TextView) findViewById(R.id.ttt);////belongs to xml file
        textView.setText(R.string.app_name);
    }
}
AkashG
  • 7,868
  • 3
  • 28
  • 43
  • Why is it better than extending Dialog class.. ? – Adam Varhegyi Jul 10 '12 at 08:45
  • 1
    See we are more familiar with activities rather than doing this,you wont have faced problem while doing with activities and you dont get desired results with the minor mistakes which you can easily have achieved with activity.you jst have to add its theme in manifest file and rest is the same – AkashG Jul 10 '12 at 09:06
1

for your 1st question

   static Context context = getApplicationContext();
   context.getString(R.string.app_name);

2nd question

   this will not work in outer class
   use context.dialog.dismiss();
Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56
-1

for your first question "R.java file for String references?"

you can use gerResource().getString(R.string.exit);

for your second question : use dialog.cancel() or dialog.dismiss();

rajpara
  • 5,203
  • 1
  • 29
  • 46