Preference dialog : Dialog « UI « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » UI » Dialog 
Preference dialog
  

//src\com\commonsware\android\prefdialogs\DialogsDemo.java
/* Copyright (c) 2008-2009 -- CommonsWare, LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
   
package com.commonsware.android.prefdialogs;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class DialogsDemo extends Activity {
  private static final int EDIT_ID = Menu.FIRST+2;
  
  private TextView checkbox=null;
  private TextView ringtone=null;
  private TextView checkbox2=null;
  private TextView text=null;
  private TextView list=null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    checkbox=(TextView)findViewById(R.id.checkbox);
    ringtone=(TextView)findViewById(R.id.ringtone);
    checkbox2=(TextView)findViewById(R.id.checkbox2);
    text=(TextView)findViewById(R.id.text);
    list=(TextView)findViewById(R.id.list);
  }
  
  @Override
  public void onResume() {
    super.onResume();
    
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
    
    checkbox.setText(new Boolean(prefs.getBoolean("checkbox"false)).toString());
    ringtone.setText(prefs.getString("ringtone""<unset>"));
    checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2"false)).toString());
    text.setText(prefs.getString("text""<unset>"));
    list.setText(prefs.getString("list""<unset>"));
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit Prefs")
        .setIcon(R.drawable.misc)
        .setAlphabeticShortcut('e');

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case EDIT_ID:
        startActivity(new Intent(this, EditPreferences.class));
        return(true);
    }

    return(super.onOptionsItemSelected(item));
  }
}



//src\com\commonsware\android\prefdialogs\EditPreferences.java
/* Copyright (c) 2008-2009 -- CommonsWare, LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
   
package com.commonsware.android.prefdialogs;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceActivity;


public class EditPreferences extends PreferenceActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    addPreferencesFromResource(R.xml.preferences);
  }
}




//res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <TableRow>
    <TextView
        android:text="Checkbox:"
        android:paddingRight="5px"
    />
    <TextView android:id="@+id/checkbox"
    />
  </TableRow>
  <TableRow>
    <TextView
        android:text="Ringtone:"
        android:paddingRight="5px"
    />
    <TextView android:id="@+id/ringtone"
    />
  </TableRow>
  <TableRow>
    <TextView
        android:text="Checkbox #2:"
        android:paddingRight="5px"
    />
    <TextView android:id="@+id/checkbox2"
    />
  </TableRow>
  <TableRow>
    <TextView
        android:text="Text:"
        android:paddingRight="5px"
    />
    <TextView android:id="@+id/text"
    />
  </TableRow>
  <TableRow>
    <TextView
        android:text="List Selection:"
        android:paddingRight="5px"
    />
    <TextView android:id="@+id/list"
    />
  </TableRow>
</TableLayout>



//res\values\arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="cities">
    <item>Philadelphia</item>
    <item>Pittsburgh</item>
    <item>Allentown/Bethlehem</item>
    <item>Erie</item>
    <item>Reading</item>
    <item>Scranton</item>
    <item>Lancaster</item>
    <item>Altoona</item>
    <item>Harrisburg</item>
  </string-array>
  <string-array name="airport_codes">
    <item>PHL</item>
    <item>PIT</item>
    <item>ABE</item>
    <item>ERI</item>
    <item>RDG</item>
    <item>AVP</item>
    <item>LNS</item>
    <item>AOO</item>
    <item>MDT</item>
  </string-array>
</resources>



//res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">DialogsDemo</string>
  <string name="checkbox">checkbox</string>
  <string name="ringtone">ringtone</string>
  <string name="checkbox2">checkbox2</string>
  <string name="text">text</string>
  <string name="list">list</string>
</resources>


//res\xml\preferences.xml
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
  <PreferenceCategory android:title="Simple Preferences">
    <CheckBoxPreference
      android:key="checkbox"
      android:title="Checkbox Preference"
      android:summary="Check it on, check it off"
    />
    <RingtonePreference
      android:key="ringtone"
      android:title="Ringtone Preference"
      android:showDefault="true"
      android:showSilent="true"
      android:summary="Pick a tone, any tone"
    />
  </PreferenceCategory>
  <PreferenceCategory android:title="Detail Screens">
    <PreferenceScreen
      android:key="detail"
      android:title="Detail Screen"
      android:summary="Additional preferences held in another page">
      <CheckBoxPreference
        android:key="checkbox2"
        android:title="Another Checkbox"
        android:summary="On. Off. It really doesn't matter."
      />
    </PreferenceScreen>
  </PreferenceCategory>
  <PreferenceCategory android:title="Other Preferences">
    <EditTextPreference
      android:key="text"
      android:title="Text Entry Dialog"
      android:summary="Click to pop up a field for entry"
      android:dialogTitle="Enter something useful"
    />
    <ListPreference
      android:key="list"
      android:title="Selection Dialog"
      android:summary="Click to pop up a list to choose from"
      android:entries="@array/cities"
      android:entryValues="@array/airport_codes"
      android:dialogTitle="Choose a Pennsylvania city" />
  </PreferenceCategory>
</PreferenceScreen>

   
    
  
Related examples in the same category
1.Dialog Helper
2.Use AlertDialog to inform exception
3.Add option item to Alert dialog and get user selection result
4.Add some padding to keep the dialog borders away
5.Dialog activity and TextView
6.Show dialog
7.Dialog layout
8.Layout dialog with xml layout file
9.extends DialogFragment
10.Color Picker Dialog
11.Dialog Yes No Message
12.Dialog Yes No Old School Message
13.Dialog Yes No Holo Light Message
14.Dialog Yes No Long Message
15.Dialog Yes No Ultra Long Message
16.Dialog with List of value
17.Dialog with Progress
18.Dialog with Single Choice
19.Dialog Multiple Choice
20.Dialog Multiple Choice Cursor
21.Dialog with Text Entry
22.Dialog with Xml layout
23.Dialog Activity
24.Demonstrates how to show an AlertDialog that is managed by a Fragment.
25.Fragment Dialog
26.Demonstrates the use of progress dialogs.
27.File open dialog
28.Show error AlertDialog
29.extends DialogPreference
30.Show Notification Alert Dialog
31.Text Dialog
32.Create Chang Log Dialog
33.Show Title And Message Dialog
34.Show dialog and parse URL
35.Error Dialog Wrapper
36.Ok Dialog Wrapper
37.Display an alert dialog
38.AlertDialog Question
39.import android.app.AlertDialog;
40.dialog Builder
41.A dialog that allows the user to select multiple entries.
42.Help Dialog Creator
43.Color Select Dialog
44.Create MessageBox
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.