Android change PreferenceActivity layout float - android

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>

Related

How to set ToolBar into PreferenceScreen in android

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.

How to set PreferenceScreen into other layout in android

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.

Preference Menu on 10 inch tablet

I am trying to figure out why the layout of my Preference menu is displayed in a false way on 10' tablets when using it in landscape mode. It looks like the lines that are to the right of the Checkboxes are actually to the left of them. You can see this here http://imageshack.us/photo/my-images/109/schnappschuss2013052109.png/
On 7' tablets, everything works like expected to do.
My menu looks like that:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="#string/pref_message"
android:key="pref_key_message_settings">
<CheckBoxPreference
android:key="pref_key_add_timestamp"
android:summary="#string/pref_summary_addTimestamp"
android:title="#string/pref_addTimestamp"
android:defaultValue="true"/>
<ListPreference
android:dependency="pref_key_add_timestamp"
android:summary="#string/pref_summary_timestamp_option"
android:key="pref_key_timestamp_option"
android:title="#string/pref_timestampOption"
android:dialogTitle="#string/pref_timestampOptionDialog"
android:entries="#array/pref_timestampArray"
android:entryValues="#array/pref_timestampValues"
android:defaultValue="Time" />
<CheckBoxPreference
android:key="pref_key_add_newline"
android:summary="#string/pref_summary_addNewLine"
android:title="#string/pref_addNewLine"
android:defaultValue="true"/>
<ListPreference
android:dependency="pref_key_add_newline"
android:key="pref_key_newline_option"
android:summary="#string/pref_summary_newline_option"
android:title="#string/pref_newLineOption"
android:dialogTitle="#string/pref_newLineOptionDialog"
android:entries="#array/pref_newLineArray"
android:entryValues="#array/pref_newLineValues"
android:defaultValue="LF" />
<CheckBoxPreference
android:key="pref_key_clear_text"
android:summary="#string/pref_summary_clearText"
android:title="#string/pref_clearText"
android:defaultValue="false"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/pref_storage"
android:key="pref_key_storage_settings">
<EditTextPreference
android:key="pref_key_change_name"
android:summary="#string/pref_summary_editFilename"
android:title="#string/pref_editFilename"
android:defaultValue="history"/>
<CheckBoxPreference
android:key="pref_key_add_date"
android:summary="#string/pref_summary_addDate"
android:title="#string/pref_addDate"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="pref_key_add_time"
android:summary="#string/pref_summary_addTime"
android:title="#string/pref_addTime"
android:defaultValue="true"/>
<at.rtcmanager.ConfirmPreference
android:key="pref_key_clear_history"
android:summary="#string/pref_summary_clearHistory"
android:title="#string/pref_clearHistory"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/pref_time"
android:key="pref_key_time_settings">
<CheckBoxPreference
android:key="pref_key_autosend_time"
android:summary="#string/pref_summary_austosendTime"
android:title="#string/pref_autosendTime"
android:defaultValue="false"/>
<ListPreference
android:key="pref_key_mode_sentTime"
android:title="#string/pref_mode_sentTime"
android:summary="#string/pref_summary_mode_sentTime"
android:dialogTitle="#string/pref_modeSentTimeDialog"
android:entries="#array/pref_sentTimeModeArray"
android:entryValues="#array/pref_sentTimeValues"
android:defaultValue="10Byte" />
</PreferenceCategory>
<PreferenceCategory
android:title="#string/pref_about"
android:key="pref_key_settings_about">
<Preference android:title="#string/prefs_about_app" >
<intent android:action="AboutActivity"/>
</Preference>
</PreferenceCategory>
</PreferenceScreen>
and is loded with:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsMenuFragment())
.commit();
}
public static class SettingsMenuFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.app_prefs);
}
}
I had the same problem. My solution is to extend SettingsActivity from usual Activity, not from PreferenceActivity. But SettingsFragment should extend PreferenceFragment of course. It helped me, hope this solves your issue too.

How to use Checkboxes for PreferenceActivity class in android

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{
}
}

Preference items being automatically re-set?

This appears like a bug to me: When you load many switch preferences in a preference fragment, they somehow re-set themselves , when you scroll the preferences. I have separately tested this with little demo code:
/res/xml/prefs.xml (Just a bunch of switch preferences, just enough to make preferences scroll on screen) :
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="my_prefs">
<PreferenceCategory android:key="my_prefs_cat" android:title="Settings">
<SwitchPreference android:key="p1" android:title="p1" android:defaultValue="false" />
<SwitchPreference android:key="p2" android:title="p2" android:defaultValue="false" />
<SwitchPreference android:key="p3" android:title="p3" android:defaultValue="false" />
<SwitchPreference android:key="p4" android:title="p4" android:defaultValue="false" />
<SwitchPreference android:key="p5" android:title="p5" android:defaultValue="false" />
<SwitchPreference android:key="p6" android:title="p6" android:defaultValue="false" />
<SwitchPreference android:key="p7" android:title="p7" android:defaultValue="false" />
<SwitchPreference android:key="p8" android:title="p8" android:defaultValue="false" />
<SwitchPreference android:key="p9" android:title="p9" android:defaultValue="false" />
<SwitchPreference android:key="p10" android:title="p10" android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>
/src/Prefs.java (A simple PreferenceFragment) :
package com.example.preflistbug;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class Prefs extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
/res/layout/main.xml (Placed PreferenceFragment in Activity layout) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.preflistbug.Prefs"
android:id="#+id/frg_prefs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/src/MyActivity.java (Demo Activity) :
package com.example.preflistbug;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Problem: If you change the first switch preference , scroll down, scroll back up, the switch is reset. Same is true for other switch preferences which scroll out of view and are visited later. (specially, in horizontal orientation)
Happens on emulator too. I'm compiling on platform version 15, ICS. As you can see in above code, this is a very simple setup, I can't find anything in this code, that might explain why this is happening.
Update
Bug reported as Issue 26194.
Update 2
It is supposed to be fixed in android L release.
I was able to reproduce this issue. I also found a workaround but I don't know why it works :)
Create a derived class from SwitchPreference like so:
public class Pref extends SwitchPreference {
public Pref(Context context) {
super(context);
}
public Pref(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Pref(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Then, instead of using these in your prefs.xml:
<SwitchPreference ... />
You can use these instead
<com.example.preflistbug.Pref ... />
The derivation seems to somehow fixes the issue where the view recycling in the ListView-driven preference list is reusing the controls without "freeing" them from their previous Preference object first (or so I think). I'll update this answer if I figure out more.

Categories

Resources