add menu and menuitem to prefs dialog (shows up for main screen only)

to reset colors to defaults
This commit is contained in:
Andy2 2010-06-10 21:10:06 -07:00
parent f7c737bcde
commit f9fbe386f5
2 changed files with 84 additions and 1 deletions

View file

@ -219,6 +219,9 @@
<string name="phonies_disallow">Disallow phonies</string>
<string name="menu_prefs">Settings</string>
<string name="gamel_menu_dicts">Dictionaries</string>
<string name="menu_revert_colors">Restore colors</string>
<string name="confirm_revert_colors">Are you sure you want to
restore all color preferences to their default values?</string>
<string name="room_label">Room</string>
<string name="phone_label">Host phone number</string>

View file

@ -19,18 +19,79 @@
*/
package org.eehouse.android.xw4;
import android.preference.PreferenceActivity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.DialogInterface;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
public class PrefsActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int REVERT_COLORS = 1;
private String[] m_keys;
@Override
protected Dialog onCreateDialog( int id )
{
Dialog dialog = null;
switch( id ) {
case REVERT_COLORS:
DialogInterface.OnClickListener lstnr =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
SharedPreferences sp =
getPreferenceScreen().getSharedPreferences();
SharedPreferences.Editor editor = sp.edit();
int[] colorKeys = {
R.string.key_player0,
R.string.key_player1,
R.string.key_player2,
R.string.key_player3,
R.string.key_bonus_l2x,
R.string.key_bonus_l3x,
R.string.key_bonus_w2x,
R.string.key_bonus_w3x,
R.string.key_tile_back,
R.string.key_focus,
R.string.key_empty,
};
for ( int colorKey : colorKeys ) {
editor.remove( getString(colorKey) );
}
editor.commit();
PreferenceManager.setDefaultValues( PrefsActivity.this,
R.xml.xwprefs,
false );
// Now replace this activity with a new copy
// so the new values get loaded.
startActivity( new Intent( PrefsActivity.this,
PrefsActivity.class ) );
finish();
}
};
dialog = new AlertDialog.Builder( this )
.setTitle( R.string.query_title )
.setMessage( R.string.confirm_revert_colors )
.setPositiveButton( R.string.button_ok, lstnr )
.setNegativeButton( R.string.button_cancel, null )
.create();
break;
}
return dialog;
}
@Override
protected void onCreate( Bundle savedInstanceState )
{
@ -86,6 +147,25 @@ public class PrefsActivity extends PreferenceActivity
}
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.prefs_menu, menu );
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
boolean handled = false;
if ( R.id.menu_revert_colors == item.getItemId()) {
showDialog( REVERT_COLORS );
handled = true;
}
return handled;
}
private void setSummary( SharedPreferences sp, String key )
{
Preference pref = getPreferenceScreen().findPreference( key );