I am having a hard time making a Preference with Dialog, none of the methods suggested in the site works for me. So here is what I have first. In MainActivity I have this
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stage_1, menu);
return true;
}
This opens the menu, and when you choose Settings, in the next screen there is only one preference, created from this preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:summary="clears all data"
android:title="Reset Data" >
<intent
android:targetClass="com.example.myapp.settings.DialogActivity"
android:targetPackage="com.example.myapp" >
</intent>
</Preference>
I want when I click this option, a dialog to appear with Yes and No button, so I ca define somewhere what the buttons will do programatically. How can I do it?
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.
Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.
The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activity before they continue moving forward. The dialog appears over the current window and display the content defined in it.
You can extend the DialogPreference. Override the onDialogClosed
method, which is called when the Dialog
closes. The argument to the method indicates whether user selected positive
or negative
button.
CustomDialogPreference.java :
public class CustomDialogPreference extends DialogPreference {
public CustomDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// Set the layout here
setDialogLayoutResource(R.layout.custom_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
// When the user selects "OK", persist the new value
if (positiveResult) {
// User selected OK
} else {
// User selected Cancel
}
}
}
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.example.test1.CustomDialogPreference
android:key="pref_dialog"
android:title="dialog"/>
</PreferenceScreen>
You can refer the official Settings documentation.
In this case, if you can using Android Studio, you can right click the folder that contains your activities and choose to create a new SettingsActivity
.
Then in your xml for ActionBar in MainActivity, set up the setting button like this:
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="99"
android:showAsAction="ifRoom"/>
Then set up Listener for the Setting button on ActionBar:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
//Start your new Activity here
return true;
}
return super.onOptionsItemSelected(item);
}
You can still use my code below on building a AlertDialog in your Listener in SettingsActivity
.
AlertDialog.Builder alert = new AlertDialog.Builder(self);
alert.setTitle(getResources().getString(R.string.action_about));
//Set up your AlertDialog and buttons
alert.setMessage(message);
alert.setNegativeButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.setCancelable(true);
alert.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With