Custom Preferencecategory Headings

Custom PreferenceCategory Headings

You should take a look at Preference.Category style:

<style name="Preference.Category">
<item name="android:layout">@android:layout/preference_category</item>
<item name="android:shouldDisableView">false</item>
<item name="android:selectable">false</item>
</style>

Let's take a look at preference_category.xml file:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+android:id/title"
/>

So you need to create custom theme that extends default android Theme and override listSeparatorTextViewStyle value with ListHeader style. And then apply this theme to Activity that extends PreferenceActivity .


Here is how you can do it.

First, in your styles.xml add next code:

<style name="PreferenceListHeader" 
parent="@android:style/Widget.TextView.ListSeparator">

<item name="android:textColor">#000000</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">12sp</item>
<item name="android:background">#cccccc</item>
<item name="android:paddingTop">6px</item>
<item name="android:paddingBottom">6px</item>
<item name="android:paddingLeft">12px</item>
</style>

<style name="Theme.Custom" parent="@android:style/Theme">
<item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>
</style>

Then in your AndroidManifest.xml add theme to your preference acitivity:

 <activity android:name=".MyPreferencesActivity" 
android:theme="@style/Theme.Custom"
... >
...
</activity>

Here is a screenshot:

Sample Image

Set Title style for PreferenceCategory in PreferenceFragmentCompat

SOLVED
I added the Linear Layout around the Text View and the background gets visible in my choice of color.

Here is the code.

settings_text.xml

<LinearLayout 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">

<TextView
android:id="@android:id/title"
style="@style/CustomPreferenceCategoryText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceStyle</item>
</style>

<style name="PreferenceStyle" parent="@style/PreferenceThemeOverlay.v14.Material">
<item name="preferenceCategoryStyle">@style/CustomPreferenceCategory</item>
</style>

<style name="CustomPreferenceCategory" parent="@style/Preference.Category">
<item name="android:layout">@layout/settings_text</item>
</style>

<style name="CustomPreferenceCategoryText" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/colorDialogPop</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/text_medium</item>
<item name="android:padding">@dimen/padding_large</item>
<item name="android:background">@color/colorAccent</item>
</style>


preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/heading_general">

<SwitchPreference
android:padding="@dimen/padding_medium"
android:title="@string/enable_push" />

<SwitchPreference
android:padding="@dimen/padding_medium"
android:title="@string/send_weekly_email" />

</PreferenceCategory>

<PreferenceCategory android:title="@string/heading_account">

<EditTextPreference
android:padding="@dimen/padding_medium"
android:title="@string/email" />

<EditTextPreference
android:padding="@dimen/padding_medium"
android:title="@string/name" />

</PreferenceCategory>
</PreferenceScreen>

Android Preference Category Styling

yes. you can use custom layout. take a look at the answer of this question. Custom PreferenceCategory Headings

How to change text color of preference category in Android?

use this customize PreferenceCategory class :

public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
}

public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTextColor(Color.RED);
}
}

and add this at your Pref.xml file :

<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />

How to change the text color of PreferenceCategory/PreferenceScreen

I added a new style:

<style name="PreferenceScreen" parent="Theme.AppCompat">
<item name="android:textColor">#ffffff</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="colorPrimary">#ff0000</item>
<item name="colorPrimaryDark">#ff0000</item>
<item name="colorAccent">#ff0000</item>
<item name="android:background">#CCCCCC</item>
</style>

and included it in the activity:

<activity android:name="com.smartipcamera.owlcam.ui.BasicSettingsActivity"
android:theme="@style/PreferenceScreen" >
</activity>


Related Topics



Leave a reply



Submit