Android PreferenceActivity Item Height - android

I have a PreferenceActivity with two checkboxes. Everything is working correctly, but I have a UI problem; not all of the text for the checkbox fits onto the checkbox row.
Is there a nice way to set the minimum height for these checkbox preferences without hardcoding a value?
EDIT - adding xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="alarm_set"
android:title="#string/set_alarm_title"
android:defaultValue="true" />
<CheckBoxPreference
android:key="store_pages"
android:title="#string/store_downloaded_pages_title"
android:summary="#string/store_downloaded_pages"
android:defaultValue="false" />
</PreferenceScreen>

Based off this question about text preference having the same iussue I use this class.
public class LongSummaryCheckBoxPreference extends CheckBoxPreference
{
public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs, int defStyle)
{
super(ctx, attrs, defStyle);
}
public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs)
{
super(ctx, attrs);
}
#Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView summary= (TextView)view.findViewById(android.R.id.summary);
summary.setMaxLines(4);
}
}
In the xml it then looks like this.
<com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="#string/key_distance_line_enable"
android:title="#string/title_distance_line_enable" android:summary="#string/summary_distance_line_enable"
android:defaultValue="true" />
<com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="#string/key_altitude_line_enable"
android:title="#string/title_altitude_line_enable" android:summary="#string/summary_altitude_line_enable"
android:defaultValue="true" />
I have exactly the same thing for EditTextPreference which has the same problem of a max of 2 lines for the summary in Api <= 7

In the xml you can set the height to "wrap_content". At that point it will size it to fit whatever you put in it. So, something along these lines:
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />

This should help
Just do it this way:
<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Min item height -->
<item name="android:listPreferredItemHeight">10dp</item>
</style>
another styling attributes that can be overridden can be found here preference item layout

Related

Custom SwitchPreference in Android

How to set a custom style or other background selector drawable for the SwitchPreference widget in Android?
(Note: not the regular Switch widget, I mean the standart SwitchPreference widget that used in PreferenceActivity / PreferenceFragment)
You have to create a custom layout for the switch itself and you can apply it dynamically like.
preference.setWidgetLayoutResource(R.layout.custom_switch);
But I'll go into details and show you exactly how to achieve this.
So, you define your preference in an xml file like preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="YOUR_CATEGORY_TITLE" >
<SwitchPreference
android:key="SWITCH"
android:title="YOUR_TITLE_FOR_SWITCH" />
</PreferenceCategory>
</PreferenceScreen>
Then read it in the onCreate() method inside your PreferenceActivty class:
SwitchPreference pref = (SwitchPreference) findPreference(getString(R.string.SWITCH));
//pref.setChecked(true); // You can check it already if needed to true or false or a value you have stored persistently
pref.setWidgetLayoutResource(R.layout.custom_switch); // THIS IS THE KEY OF ALL THIS. HERE YOU SET A CUSTOM LAYOUT FOR THE WIDGET
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Here you can enable/disable whatever you need to
return true;
}
});
The custom_switch layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textStyle="bold"
android:track="#drawable/switch_track"
android:thumb="#drawable/switch_thumb"/>
And for the switch you'll have 2 selectors for the track and thumb properties.
The drawables for these selectors can be generated with the Android Holo Color Generator, which was suggested by tasomaniac. In this case, all you have to do, is to copy the content of the generated drawable folders(only for the drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi). But you can create custom views for each state you need.
Here is how these selectors will look like:
switch_track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_bg_focused" android:state_focused="true"/>
<item android:drawable="#drawable/switch_bg"/>
</selector>
switch_thumb:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_thumb_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_thumb_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/switch_thumb_activated" android:state_checked="true"/>
<item android:drawable="#drawable/switch_thumb"/>
</selector>
And that's pretty much it. This solution helped me out. If I omitted something, please let me know and I'll correct the issues.
You can use the below website to generate style for your Switch.
http://android-holo-colors.com/
And then you can use following libraries to custom implementation of the regular Switch. These libraries also include SwitchPreference alternative.
https://github.com/BoD/android-switch-backport
https://github.com/ankri/SwitchCompatLibrary
One way of doing this is to subclass the SwitchPreference and override the onBindView method. In doing so, you'll want to still call super.onBindView(view) in that method, but then find the Switch in the child views and style it as appropriate:
package com.example;
import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import com.example.R;
public class CustomSwitchPreference extends SwitchPreference {
#SuppressLint("NewApi")
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
if (theSwitch!=null) {
//do styling here
theSwitch.setThumbResource(R.drawable.new_thumb_resource);
}
}
private Switch findSwitchInChildviews(ViewGroup view) {
for (int i=0;i<view.getChildCount();i++) {
View thisChildview = view.getChildAt(i);
if (thisChildview instanceof Switch) {
return (Switch)thisChildview;
}
else if (thisChildview instanceof ViewGroup) {
Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
if (theSwitch!=null) return theSwitch;
}
}
return null;
}
}
Create a style in your style.xml file and give it Widget.AppCompat.CompoundButton.Switch parent.
<style name="theme_switch_compat" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="colorAccent">#color/YourColorAccent</item>
</style>
Then you can use the link below to complete your theme
How to change the track color of a SwitchCompat

Change height of Preference items

I have a PreferenceFragment subclass. I want each one of its items (Preferences and SwitchPreferences) to have a height of 120dp. How to do that?
Here is the related code:
public class SettingsFragment extends PreferenceFragment {
public SettingsFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main);
}
}
and
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference android:key="app_main_switch"
android:title="#string/app_name"
android:defaultValue="true"/>
<Preference android:title="#string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="#string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<SwitchPreference android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="#string/learning"/>
</PreferenceScreen>
Here is how it looks like now:
So I want all four items of the list to have a height of 120dp. As you can see I'm not the one creating the ListView, it's created internally. I tried to retrieve it with
findViewById(android.R.id.list)
but iterating over its elements gives Preference objects which do not allow me to set the height.
Just do it this way:
<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Min item height -->
<item name="android:listPreferredItemHeight">10dp</item>
</style>
another styling attributes that can be overridden can be found here preference item layout
Try creating a custom Preference class (you may have to do it for every type of Preference you are going to use/want height to be 120DP)
public class SwitchPref extends SwitchPreference {
Context context;
public Pref(Context context) {
super(context);
this.context = context;
}
#Override
protected View onCreateView(ViewGroup parent) {
LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(120));
layout.setLayoutParams(params1);
//if this returns just the linearlayout, you will have to add your own switch
// or reference to a layout.xml resource
return super.onCreateView(layout);
}
public int dpToPx(int dp) {
int valueInDp = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
.getDisplayMetrics());
return valueInDp;
}
}
Then, in your preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--use your custom SwitchPrefence class-->
<my.package.name.SwitchPref android:key="app_main_switch"
android:title="#string/app_name"
android:defaultValue="true"/>
<Preference android:title="#string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="#string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<my.package.name.SwitchPref android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="#string/learning"/>
</PreferenceScreen>
Hope this helps, happy coding!
Here is how I managed to do this:
I did need to subclass Preference but MattMatt's answer didn't work.
public class HigherPreference extends Preference {
public HigherPreference(Context context) {
super(context);
}
public HigherPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected View onCreateView(ViewGroup parent) {
Resources res=getContext().getResources();
View v=super.onCreateView(parent);
v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.rowheight)));
return v;
}
}
The layout XML goes like what he provided.

How do I add a clickable button to a textview that is clickable currently

New to android trying to figure something out, any help appreciated.I have a textview which is currently clickable. I just need to put a clickable button background to it. This is my xml file:
mainscreen.xml under layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:drawable/divider_horizontal"
android:orientation="vertical"
android:showDividers="middle" >
<TextView
android:id="#+id/mainscreen_option"
style="#style/TextView.MainscreenItem"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="1dp"
android:clickable="true"
android:onClick="onMainscreenClicked"
android:text="#string/nav_option"
/>
</LinearLayout>
and the selector class code where i have defined the button is under drawable folder :
bg_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/menu_btn_active"/>
<item android:state_pressed="true" android:drawable="#drawable/menu_btn_active" />
<item android:drawable="#drawable/menu_btn" />
</selector>
and the corresponding javacode for the textview is :
mainscreennav.java
private void highlightMenuItem(){
TextView highlightedTextView = null;
final String activeFragmentTitle = getArguments().getString(ACTIVE_MENU_ITEM);
final Resources resources = Application.getAppResources();
if (resources.getString(R.string.nav_option_mainscreen).equals(FragmentTitle)) {
highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_mainscreen);
} highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));
}
}
Can anyone guide me as to how I can convert this textview into a button such that both are clickable together and i can set the text margin in the button from left as certain dp's.
Thanks in advance! Justin
Edit:
create a selector xml with your code and put in your drawable folder. I'll create a btn_custom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_active" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_active" />
<item
android:drawable="#drawable/btn_default" />
</selector>
Then, on your TextView:
<TextView android:text="MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:clickable="true"
android:background="#drawable/btn_custom"
android:padding="10dp"/>
On padding, you adjust your button padding.
On background, put the name of your selector (in my case, #drawable/btn_custom)
and your effect is already on.
Than, just register an onClickListener
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextButton = (TextView) findViewById(R.id.my_button_id);
myTextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "ButtonClick", 200).show();
}
});
}
Ps: You can use your onClick method instead :P
Always remember: A Button is just a "styled" TextView.
This is the Android source code for Button class:
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Yes, it's just that.
As I understand your question, you want to have clickable textview in front of the button?
If so create RelativeLayout instead of your LinearLayout, place your textview in front of button (or other way around according to your needs). And assign onClick for both of them.
Hope this helps and enjoy your work.

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.

How to change text color of preference category in Android?

The textColor attribute isn't working. Here's my XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Any ideas?
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" />
An easy way to do this is to set the custom layout for the preferenceCategory here:
<PreferenceCategory
android:layout="#layout/preferences_category"
android:title="Privacy" >
Then set your code inside your preferences_category layout file:
<TextView
android:id="#android:id/title"
android:textColor="#color/deep_orange_500"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textAllCaps="true"/>
One solution is to make a theme for your PreferenceScreen.
So in your themes.xml or styles.xml (better to put it in themes.xml) :
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">#color/yourCategoryTitleColor</item>
</style>
then in your AndroidManifest.xml :
<activity
android:name="MyPreferenceActivity"
...
android:theme="#style/PreferenceScreen" >
</activity>
It worked perfectly for me.
To change text color of preference category only set a theme to your PreferenceActivity in your Android Manifest and make sure that colorAccent item exists. This color is taken by your PreferenceCategory.
Actually just found out that preference category text using colorAccent. If your app didn't using style of colorAccent, you can go to styles.xml and find <item name="colorAccent">#color/colorPrimary</item>, and change the color as you want.
Other way around will be mention the theme in your AppTheme, application level
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
.....//your other items
<item name="preferenceTheme">#style/PrefTheme</item>
</style>
<style name="PrefTheme" parent="#style/PreferenceThemeOverlay">
<item name="preferenceCategoryStyle">#style/CategoryStyle</item>
</style>
<style name="CategoryStyle" parent="Preference.Category">
<item name="android:layout">#layout/pref_category_view</item>
</style>
XML : pref_category_view
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:textColor="#color/red"
android:layout_height="wrap_content"
/>
For more customization visit v7 Preferences res
Important: I am using PreferenceFragmentCompat from lib v7 Preference.
Using Material Theme, you just have to override the following property :
<style name="YourTheme" parent="#style/Theme.MaterialComponents">
<item name="colorAccent">#color/your_custom_color</item>
</style>
public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
protected View onCreateView(ViewGroup parent) {
// It's just a TextView!
TextView categoryTitle = (TextView)super.onCreateView(parent);
categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));
return categoryTitle;
}
}
And in your prefs.xml:
<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>
Or you can also use this answer
Define your own PreferenceTheme and override colors.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/AppTheme.PreferenceTheme</item>
</style>
<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="colorAccent">`#color_value`</item>
</style>
Inspired by #AliSh answer, but I needed to only change the colour of one Preference text item. So, for all the Kotlin guys out there:
class TextColorPreference : Preference {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(
context: Context, attrs: AttributeSet,
defStyle: Int
) : super(context, attrs, defStyle)
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
context?.let {
(holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
ContextCompat.getColor(
it,
R.color.colorPrimary
)
)
}
}
}
And then put it in your xml/prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<this.should.be.your.package.TextColorPreference
android:id="#+id/settings_logout"
android:key="#string/prefs_key_logout"
android:title="#string/settings_logout" />
</PreferenceScreen>
For androidx preference library you can extend PreferenceCategory or Preference class like this:
class DangerPreference(
context: Context?,
attrs: AttributeSet?,
): Preference(context, attrs) {
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
holder?.itemView?.findViewById<TextView>(android.R.id.title)?.setTextColor(Color.RED)
}
}
then use it normally in your PreferenceScreen:
<com.github.anastr.myscore.util.pref.DangerPreference
app:key="deleteServerData"
app:title="#string/delete_server_data"
app:iconSpaceReserved="false" />
A lot of the other answers didn't work for my case. I'm using a PreferenceFragmentCompat and I didn't want to have actual code doing this. So I simply made a copy of the preference category xml file and changed the textColor field. The file goes under the Apache license, Version 2.
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ 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 -->
<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"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginStart="?android:attr/listPreferredItemPaddingLeft"
android:orientation="vertical">
<TextView
android:id="#android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingRight"
android:textAlignment="viewStart"
android:textColor="#color/app_accent"
android:textStyle="bold"
tools:ignore="RtlSymmetry"/>
<TextView
android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="?android:attr/textColorSecondary"/>
</LinearLayout>
And imported it in my .xml layout for the Preference fragment:
<PreferenceCategory
android:layout="#layout/preference_category_companion"
android:key="my_preference_title"
android:title="#string/my_preference_title">

Categories

Resources