//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", ""));
checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2", false)).toString());
text.setText(prefs.getString("text", ""));
list.setText(prefs.getString("list", ""));
}
@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
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:text="Checkbox:"
android:paddingRight="5px"
/>
/>
android:text="Ringtone:"
android:paddingRight="5px"
/>
/>
android:text="Checkbox #2:"
android:paddingRight="5px"
/>
/>
android:text="Text:"
android:paddingRight="5px"
/>
/>
android:text="List Selection:"
android:paddingRight="5px"
/>
/>
//res\values\arrays.xml
- Philadelphia
- Pittsburgh
- Allentown/Bethlehem
- Erie
- Reading
- Scranton
- Lancaster
- Altoona
- Harrisburg
- PHL
- PIT
- ABE
- ERI
- RDG
- AVP
- LNS
- AOO
- MDT
//res\values\strings.xml
DialogsDemo
checkbox
ringtone
checkbox2
text
list
//res\xml\preferences.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:key="checkbox"
android:title="Checkbox Preference"
android:summary="Check it on, check it off"
/>
android:key="ringtone"
android:title="Ringtone Preference"
android:showDefault="true"
android:showSilent="true"
android:summary="Pick a tone, any tone"
/>
android:key="detail"
android:title="Detail Screen"
android:summary="Additional preferences held in another page">
android:key="checkbox2"
android:title="Another Checkbox"
android:summary="On. Off. It really doesn't matter."
/>
android:key="text"
android:title="Text Entry Dialog"
android:summary="Click to pop up a field for entry"
android:dialogTitle="Enter something useful"
/>
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" />