Narender Sharma
Narender Sharma

Reputation: 53

how can open alert dialog box in android

I want to open alert dialog box after successfully submitted data. I am using following code but not work.

     dialog = ProgressDialog.show(TanantDetails.this, "", "Please Wait...", true);
                new Thread(new Runnable() {
                       public void run() {
                String response;

             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences sp=getSharedPreferences("login",MODE_WORLD_READABLE);
            try {
                 Utility utility=new Utility();
                // 

                 new_url=url+mobile_no.getText().toString();
                response = utility.getResponse(utility.urlEncode(new_url));
                dialog.dismiss();
                if (response.equals("Success"))
                {

                     AlertDialog alertbox = new AlertDialog.Builder(getBaseContext())
                        //.setIcon(R.drawable.no)
                        .setTitle("Submit successfully")
                        .setMessage("“Police will verify documents between preferred timing")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {

                            // do something when the button is clicked
                            public void onClick(DialogInterface arg0, int arg1) {
                               TanantDetails.this.finish();
                                Intent i=new Intent(getApplicationContext(),MainActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                startActivity(i);

                            }
                        })
                          .show(); 


                }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                       }
                 }).start();       


        }  

toast show the message response success.

I am new to android

Upvotes: 1

Views: 7998

Answers (3)

VirtualProdigy
VirtualProdigy

Reputation: 1687

Your alert dialog needs to be displayed on the UI thread. The code you are running is on a separate thread. In most cases when you want to update a UI element while in a separate thread it is done by using runOnUiThread() Please see the code below

if (response.equals("Success"))

{

runOnUiThread(new Runnable() {
@Override

public void run() {

AlertDialog alertbox = new AlertDialog.Builder(getBaseContext())

//.setIcon(R.drawable.no)

.setTitle("Submit successfully")

.setMessage("“Police will verify documents between preferred timing")

.setPositiveButton("ok", new DialogInterface.OnClickListener() {


// do something when the button is clicked

public void onClick(DialogInterface arg0, int arg1) {

TanantDetails.this.finish();

Intent i=new Intent(getApplicationContext(),MainActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(i);

}

}).show(); 

}

});

}

Upvotes: 0

Ashini
Ashini

Reputation: 492

Try this code:

new AlertDialog.Builder(this)
.setTitle("Your title")
.setMessage("Your message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // Your code
    }
 })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do nothing
    }
 })
 .show();

Upvotes: 1

Jibran Khan
Jibran Khan

Reputation: 3256

Simple Alert Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Alert")
                   .setTitle("Warning");

AlertDialog alert =builder.create();
                alert.show();

If you want to add ok, cancel buttons then add

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User clicked OK button
                    }
                   });

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User clicked cancel button
                       }
                   });

Upvotes: 5

Related Questions