I want use PreferenceScreen for my application setting, but i want show this Preferences below the ToollBar.
toolbar.xml code :
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="Setting Page"
/>
setting_preferences.xml (path is : res/xml) code :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="setting_title_title_category"
android:title="Title options">
<CheckBoxPreference
android:id="#+id/setting_title_show_id"
android:key="setting_title_show"
android:title="Show Main Title"
android:summary="Show/hide MainPage title"
android:checked="true"/>
<EditTextPreference
android:key="setting_title_text"
android:title="Set Main Title"
android:summary="Change MainPage title"
android:dialogTitle="Change Title"
android:dialogMessage="Change title please..."/>
</PreferenceCategory>
<PreferenceCategory
android:key="setting_title_font_category"
android:title="Font options">
<ListPreference
android:key="setting_title_font_color"
android:title="Title font colors"
android:summary="Change title font colors"
android:entries="#array/colors"
android:entryValues="#array/colors"
android:dialogTitle="Change font color" />
</PreferenceCategory>
</PreferenceScreen>
SettingPage code :
public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("setting_title_font_color")) {
// get preference by key
Preference pref = findPreference(key);
// do your stuff here
}
if (key.equals("setting_title_show")){
Preference pref = findPreference(key);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting_prefrences);
}
}
How can it? show setting_preferences.xml beloe ToolBar .
I believe you can use a normal Activity / AppCompatActivity with a simple layout that contains the Toolbar. Then in the content area of the Activity layout, use a PreferenceFragment to show the PreferenceScreen.
Related
I am trying to make a setting menu with cardView and Preference, but I am not sure how to combine these two... Here's the screenshot and I want to move the ListPreference and PreferenceScreen entries into the setting cardView above.
Here's the preference.xml The layout/setting_group is a cardview
<PreferenceScreen
android:layout="#layout/setting_title_top"
android:title = "Padding" />
<PreferenceScreen
android:layout="#layout/setting_group">
</PreferenceScreen>
<ListPreference
android:id="#+id/list"
android:layout="#layout/setting_menu"
android:dialogTitle = "title one"
android:key = "key_1"
android:title = "ListPreference"
android:summary = "Summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListPreference>
<PreferenceScreen
android:layout="#layout/setting_menu"
android:key="Key two"
android:summary="Summary"
android:title="PreferenceScreen" />
<PreferenceScreen
android:layout="#layout/setting_title"
android:title = "Another Setting" />
The PreferenceScreen is a container, you are opening and closing him with noting inside.
You can create preferences like that:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/default_value" <!--The initial state of the preference -->
android:key="#string/pref_key" <!-- The key for retrieve the preference -->
android:singleLine="true"
android:title="#string/pref_title" /> <!-- The title who is showing above the preference -->
</PreferenceScreen>
Edit: Create a xml file to with the layout of your preferences and create and Activity, example:
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key))); // Is deprecated, but still a good option to no need change the values manually.
}
private void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(this);
onPreferenceChange(preference, PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
preference.setSummary(stringValue);
return true;
}
}
I want use PreferenceScreen in my application, and create XML layout for this activity in res/xml/setting_preference.xml. but this XML layout is very simple, i want use this layout in another layout.
For example : i want use setting_preference.xml into setting_activity.xml , setting_activity.xml path is res/layout/setting_activity.xml.
How can i it?
setting_preference XML code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="setting_title_title_category"
android:title="Title options">
<CheckBoxPreference
android:id="#+id/setting_title_show_id"
android:key="setting_title_show"
android:title="Show Main Title"
android:summary="Show/hide MainPage title"
android:checked="true"/>
<EditTextPreference
android:key="setting_title_text"
android:title="Set Main Title"
android:summary="Change MainPage title"
android:dialogTitle="Change Title"
android:dialogMessage="Change title please..."/>
</PreferenceCategory>
<PreferenceCategory
android:key="setting_title_font_category"
android:title="Font options">
<ListPreference
android:key="setting_title_font_color"
android:title="Title font colors"
android:summary="Change title font colors"
android:entries="#array/colors"
android:entryValues="#array/colors"
android:dialogTitle="Change font color" />
</PreferenceCategory>
<RingtonePreference
android:title="tes"/>
</PreferenceScreen>
setting_activity XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tellfa.nikandroid.mytestdbproject.SettingPage">
</RelativeLayout>
Setting Page code:
public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("setting_title_font_color")) {
// get preference by key
Preference pref = findPreference(key);
// do your stuff here
}
if (key.equals("setting_title_show")){
Preference pref = findPreference(key);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting_prefrences);
}
}
}
Add a frameLayout where you want to load your xml preference file in the layout xml file and implement PreferenceFragment in that activity.
by default android PreferenceActivity layouts are left to right and i can not change PreferenceActivity widgets to right from left. for example see this screen shot please:
i want to change this layout like with below screen shot
PreferenceActivity Activity:
public class ActivitySettings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_settings);
}
}
PreferenceActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="#drawable/img_app_setting" >
<PreferenceCategory android:title="#string/settings_alarm_title" >
<CheckBoxPreference
android:key="PLAY_SOUND"
android:summaryOff="#string/settings_summary_off_play_sound"
android:summaryOn="#string/settings_summary_on_play_sound"
android:title="#string/settings_play_sound" />
<CheckBoxPreference
android:key="SHOW_NOTIFICATION"
android:summaryOff="#string/settings_summary_off_show_notification"
android:summaryOn="#string/settings_summary_on_show_notification"
android:title="#string/settings_show_notification" />
</PreferenceCategory>
</PreferenceScreen>
I have MenuSettings in which I have PreferenceActivity class. In PreferenceActivity I have displayed checkboxPreference like below.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Options">
<CheckBoxPreference
android:key="pref_opt1"
android:title="Option 1"
android:summary="Tick to set this option"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_opt2"
android:title="Option 2"
android:summary="Tick to set this option"
android:defaultValue="true"
/>
</PreferenceCategory>
</PreferenceScreen>
I want to use checkboxes to perform function for synchronization that I have build, But Please can any body let me know how to use checkboxes in PreferenceActivity. Is there any need to create another Activity? or we can perform function in same PreferenceActivity?
here is my PreferenceActivity:
public class SharedPref extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sharedpref);
}
// StorePreference method{
}
// readPreferences method{
}
}
I have xml file that defines some preference screens like the following example
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root_preferencescreen">
<PreferenceScreen android:key="general_sett" android:title="general settings" />
....
<PreferenceScreen android:key="extras_sett" android:title="extras settings" />
</PreferenceScreen>
I would like to be able to increase the font size for the text of the preference screen , but because the within a preference screen there is no android:textsize tag , i have no idea how to accomplish that !
You can simply add android:textSize inside your theme for preference screen:
e.g :
<style name="settingsTheme" parent="PreferenceThemeOverlay">
<item name="colorAccent">#color/color_ten</item>
<item name="android:background">#color/colorPrimaryLight</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
<item name="android:textColorPrimary">#color/colorTextBodyLight</item>
<item name="android:textColorSecondary">#color/colorTextCaptionLight</item>
<item name="android:textSize">14sp</item>
</style>
Your SettingsActivity class :
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction()
.replace(R.id.bodylayout, new PrefsFragment())
.commit();
}
#Override
protected void onPause() {
super.onPause();
}
}
PrefsFragment class :
public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreatePreferences(Bundle bundle, String s) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
final Context myContext = this.getActivity();
//set your theme
myContext.setTheme(R.theme.settingsTheme);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingspreference);
//do other stuffs
}
//.....
}
You can make a TextView layout xml that looks something like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:textColor="#android:color/white"
android:id="#+android:id/title"
/>
and set the layout of your preference category in your preferences.xml like this:
<PreferenceCategory
android:title="Category Title"
android:layout="#layout/pref_category"
/>
As long as the TextView has the id #+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.