0

I am newbie in android. May I Know how to add a alertDialog as confirmation in wifi setting. The alertDialog should consist "yes" for turn on wifi while "no" for close the app. here is part of coding:

> // Check for wifi is disabled > if (mainWifi.isWifiEnabled() == false) > { > // If wifi disabled then enable it > Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", > Toast.LENGTH_LONG).show(); > > AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); > > // Setting Dialog Title > alertDialog.setTitle("Confirm..."); > > // Setting Dialog Message > alertDialog.setMessage("Do you want to turn on your wifi?"); > > > /* i want to add in "YES" and "NO" for the wifi setting > * > * if yes then turn on > * { > * * mainWifi.setWifiEnabled(true); > * } > * if no then close the app > * { > * // if this button is clicked, close > > mainWifi.this.finish(); > * } > * > * > * > * */ > // Showing Alert Message > alertDialog.show(); > } 

1 Answer 1

1

Simplest way is to create a AlertDialog Builder and set a Title and Message and map the Yes/No to Positive/Negative buttons with a dialog click listener to handle the button click

 AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("Dialog Title") .setMessage("Dialog Message") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // handle your code to wifi on } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // handle your code to wifi off } }); // show the dialog builder.show(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.