androidqq6
androidqq6

Reputation: 1526

Dialog still show after click positive button

I have button on (CustomDilaog activity) when clicked show custom dialog with password edittext and OK button and cancel button , if you put correct password it open another activity (Text activity), till now every things work fine ,

i have question with two parts.

Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss

Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .

(CustomDilaog activity):

public class CustomDilaog extends Activity {

final Context context = this;
private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
    tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    tv1.setText(Html.fromHtml(getString(R.string.introclusion)));

    button = (Button) findViewById(R.id.button1); 
    // add button listener
    button.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {

        // custom dialog
        final Dialog dialog = new Dialog(context,R.style.cust_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
        dialog.setContentView(R.layout.custom);

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        Typeface font = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        text.setTypeface(font);
        text.setText("write password :");

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        Typeface font1 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        dialogButton.setTypeface(font1);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText password = (EditText) dialog.findViewById(R.id.password);
                ////
                if( password.getText().toString().length() > 0 ) {
                    if( password.getText().toString().equals("test")) {
                Intent intent = new Intent(CustomDilaog.this,Text.class);
                startActivity(intent);

               }
                    else{
                        // get your custom_toast.xml layout
                        LayoutInflater inflater = getLayoutInflater();

                        View layout = inflater.inflate(R.layout.custom_toast,
                                (ViewGroup) findViewById(R.id.custom_toast));

                        // set a dummy image
                        ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
                        image.setImageResource(R.drawable.ic_launcher);

                        // set a message
                        TextView text = (TextView) layout.findViewById(R.id.text_toast);
                        text.setText("Wrong password");

                        // Toast...
                        Toast toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();

                        }
                    }                   
            }
        });

        Button dialogButtonCancell = (Button) dialog.findViewById(R.id.cancel);
        Typeface font11 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        dialogButtonCancell.setTypeface(font11);

        dialogButtonCancell.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {                            
               dialog.dismiss();                        
               }
            });

        dialog.show();
      }
    });
}
}

Upvotes: 0

Views: 1369

Answers (2)

Tomas Žemaitis
Tomas Žemaitis

Reputation: 1907

call dialog.dismiss() at the beginning of dialogButton click listener:

dialogButton.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {

      dialog.dismiss();

      EditText password = (EditText) dialog.findViewById(R.id.password);
      if( password.getText().toString().length() > 0 ) {

      ...
   }
});

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss

Simply call finish() in your CustomDialogActivity when you start the new Activity. It looks like your Intent is calling this same Activity though so I'm a little confused on that.

if( password.getText().toString().length() > 0 ) {
       if( password.getText().toString().equals("test")) {
            Intent intent = new Intent(CustomDilaog.this,Text.class);
            startActivity(intent);

Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .

Add an else statement to your first if and inside it put dialog.dismiss()

Upvotes: 0

Related Questions