Text size within an PreferenceScreen - android

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.

Related

How to create a single activity without headers

I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...

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.

How to add ToolBar in PreferenceActivity?

I want to add ToolBar in PreferenceActivity in my android application. I wrote the following code.
public class SettingsActivity extends PreferenceActivity {
SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
root.addView(bar, 0);
bar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
This worked perfectly in my android Kitkat phone API 19 but force closed in API level 10 i.e. gingerbread. Please suggest me.
You need a layout that contains a Toolbar, and a ListView with android:id="#android:id/list"
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/content_frame"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
addPreferencesFromResource(R.xml.preferences);
...
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
#Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
#Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
private void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
...
}
Check out my fully working example:
activity_settings.xml
SettingsActivity.java
Reference from the Android team: AppCompatPreferenceActivity
Try:
public class SettingsActivity extends AppCompatPreferenceActivity {
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
/* getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment())
.commit();
*/
//addPreferencesFromResource(R.xml.pref_general);
}
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
app_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
I'm late but here's a small fix to the problem instead of writing tons of code or adding bulky libraries.
Go to AndroidManifest.xml and to your Preference Activity add the custom theme
<activity
android:name=".Dashboard.PreferencepActivity"
android:label="#string/title_activity_preference"
android:theme="#style/Pref"></activity>
Here's the custom theme added to the above #style/Pref
<style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Hope it helps!
You can easily add toolbar from #android:style/Theme.Material.Light.DarkActionBar
In AndroidManifest.xml :
<activity
android:name=".activity.SettingsActivity"
android:theme="#style/SettingsTheme"
android:label="Settings"/>
In v21/styles.xml
<style name="SettingsTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
In v14/styles.xml for Back API support
<style name="SettingsTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar.V14.Movie.NoTitle</item>
Very easy and working well in my case by inflating custom toolbar.
In your java code do as below,
public class SettingsPrefActivity extends AppCompatPreferenceActivity {
// private static final String TAG = SettingsPrefActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting up toolbar
getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings");
setSupportActionBars(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
}
}
and at your xml side code,add one preference category at the top do as below,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
<PreferenceCategory android:layout="#layout/toolbar_setting"></PreferenceCategory>
and in your layout resource folder toolbar_setting,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="#dimen/appbar_elevation"
android:minHeight="?attr/actionBarSize"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
This method will not solve adding toolbar, but you can get the default ActionBar back for your settings page. Create a theme for settings activity, by inheriting from parent theme.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Settings" parent="AppTheme">
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
</style>
In AndroidManifest.xml set the android:theme
<activity
android:name=".Settings"
android:theme="#style/AppTheme.Settings" />
That is all.
Instead of:
public class PreferencesActivity extends Activity
Do this:
public class PreferencesActivity extends AppCompatActivity

how to hide the divider between preferences in fragment

I want to hide the divider between preferences in fragment.
The code is below:
1.SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragement = new SettingsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, settingsFragement, "settings");
transaction.commitAllowingStateLoss();
}
2.SettingsFragment.java
public class SettingsFragment extends PreferenceFragment{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
3.settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference
android:key="preference_a"
android:title="preferenceA"/>
<Preference
android:key="preference_b"
android:title="preferenceB"/>
<ListPreference
android:key="list_preference_c"
android:title="list_preferenceC"/>
</PreferenceCategory>
</PreferenceScreen>
There are system default dividers amoung the preferences.
I want to hide the dividers.
How to hide the dividers? Thanks a lot.
Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.
ListView list = (ListView) findViewById(android.R.id.list);
if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider
#Nullable
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
setDivider(new ColorDrawable(Color.TRANSPARENT));
setDividerHeight(0);
}
These solutions didn't work for me. Here's what I did in PreferenceFragmentCompat:
#Override
public void setDivider(Drawable divider) {
super.setDivider(new ColorDrawable(Color.TRANSPARENT));
}
#Override
public void setDividerHeight(int height) {
super.setDividerHeight(0);
}
Define custom preference theme without dividers in your style.xml:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0dp</item>
</style>
and don't forget to use it in your main app theme which is also in style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="preferenceTheme">#style/PrefsTheme</item>
</style>
You can remove your divider by style.
<style name="PreferenceFragment.Material">
<item name="android:divider">#null</item>
</style>
First: if used the listview in xml
set the android:dividerHeight="0dp" in listview in xml file ,
second:
ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.TRANSPARENT)); // or some other color int
list.setDividerHeight((int) getResources().getDisplayMetrics().density);

Categories

Resources