Matching default color in custom preference - android

I have a custom preference that is basically a slider.
The custom XML for the preference slider can be used to set text color, but I need to MATCH the text color to the default that the device uses. The Title color seems fine, just uses default (same as another CheckBoxPreference title, or so it looks). The Summary color is also using the default color (same as Title), but I want this to match the default color of the Summary text of a CheckBoxPreference, which is defined right above it in XML.
Can I get the color of the Summary text from the CheckBoxPreference and just change the Summary text color of the custom preference to match the CheckBoxPreference Summary color, or is there a global (R.attr.xxxxxxxx whatever) that I can use that exists on all devices?
For example, I have a checkbox preference that does not specify title or summary colors - thus, it uses the default color scheme (but this color scheme looks like the title matches, but the summary text in the custom preference (from xml) is the same as the title color, but not the default summary color as default preferences)).
My custom slider (which has title and summary fields) needs to match the default OS colors for those two fields to the device default color scheme for those entries (title & summary).
How do I get the default device color that is set for a checkboxpreference when no color is specified?
How do I assign that color to my custom preference, which has it's own xml file that defines the custom preference view.
Hope this makes perfect sense, if not I'll elaborate a bit more.
preference definition for the custom preference (which is further defined in it's xml):
<com.xxx.sbp.SeekBarPref
android:id="#+id/xmlpreference_seekbar"
android:key="preference_seekbar"
android:title="Seek Bar Pref"
android:summary="Choose seconds"
android:defaultValue="20"
android:max="300"
sbp:min="10"
sbp:interval="10"
sbp:intervaldivide="10"
sbp:unitsLeft=""
sbp:unitsRight="s" />

I found these: android:textAppearance="?android:attr/textAppearanceLarge" and android:textAppearance="?android:attr/textAppearanceSmall" seem to do the trick.

A complicated solution is given in Android preferences summary default color?
I agree with the respondents to above that neither of the textAppearance settings will guarantee a color match.

The default color for preference summary text appears to be #DACC45. The way I did this was to take a screen snapshot and query the colored pixels with an image editor.

I've had the same problem and I've solved it with these lines in onCreateView method of seekBarPreference.java
TextView summaryText = new TextView(getContext());
summaryText.setText(getSummary());
summaryText.setTextAppearance(getContext(), android.R.style.TextAppearance_Small);

Related

Text styles used in Settings app for titles and subtitles

which text styles are used for the titles and subtitles in the Settings app? I tried using the following, but none of these produced the same result.
Title text
android:textAppearance="?android:titleTextAppearance"
android:textAppearance="?attr/title"
Subtitle text
android:textAppearance="?android:subtitleTextAppearance"
android:textAppearance="?attr/subtitle"
Text style is often not enough to achieve a particular style, you often need to set color too. In this case, I suspect they use Google Material Components styles. It's hard to judge the appropriate font size here given your font scale, but here's what I think they use:
Title
android:textAppearance="?textAppearanceBody1"
Subtitle
android:textAppearance="?textAppearanceBody2"
android:textColor="#color/material_on_background_emphasis_medium"
They also use a special font.

How to Create a custom tab layout that mimics the default tab text

I need to create a custom tab layout, but I want the text to mimic the style used in the normal TabLayout.TabView.
To do that, I've set my text appearance to TextAppearance.Design.Tab.
If you take a look at TabLayout itself, you can see that's what TabView's text uses:
mTabTextAppearance = a.getResourceId(R.styleable.TabLayout_tabTextAppearance, R.style.TextAppearance_Design_Tab);
...
TextViewCompat.setTextAppearance(mTextView, mTabTextAppearance);
However, when my tabs display, I can clearly see the text is not the same. It's very similar, but the default tab looks to be more bold.
What am I doing incorrectly?
The answer was to set the text appearance programmatically. Setting android:textAppearance on the TextView in the XML file doesn't seem to do anything. Setting the text appearance programmatically (like TabLayout.TabView) works.

Access a value from the current theme in an xml layout

I want a specific TextView to have the same font size as a Button, but they appear to be different. If my Button is not setting a custom textSize, how can I grab this value from the current theme and set it to the textSize of my TextView?
It should be fairly simple with styles. Buttons parent is android.widget.TextView so you could make a style that uses this as parent and overwrite the default textSize. Then let both use this style.

How to add style to a NumberPicker in Android

I am trying to add a custom style to an Android NumberPicker. What I am trying to do is have the text displayed by the picker as white since by default (or according to the default theme on the device) I have it in black. The problem is that my app background is a dark color so I want the text to be white or something clear.
so I have something like this in my style.xml file :
<style name="myPicker">
<item name="android:textColor">#FFFFFF</item>
</style>
the textcolor attribute doesn't even exist on the NumberPicker widget but I just tried to add it so that my text color changes. It does not work of course. The only thing that can be customized on this widget seems to be the background
Now the question : how can we change the text color of the picker ?
if somebody has the same issue or has solved it then please let us know.
I have found an "ugly" way to do it. Just get a reference to the child views of the number picker widget. There are three (3) child views : the upper arrow, the textview (containing the text or value to be displayed) and the lower arrow.
Let's say we have a Number picker called np
np.setValue(10);
np.setMinValue(5);
np.setMaxValue(50);
// retrieve the textview reference
TextView npTextView = (TextView) np.giftAmount.getChildAt(1); // since indexing begins at 0
npTextView.setTextColor(getResources().getColor(
R.color.my_custom_color)
I know that's bad but it works ...

In Android, how can string values and graphic resources be switched with a theme?

With an Android theme, it looks like you can change the default colors and appearance with a theme switch. However, I would like string values to change as well. For example, I have a TextView in my layout. If I switch to the Blue Theme, I would like the string to change to "This is the blue theme.". I also want to switch graphic resources as well.
How can I do this without generating the layout programmatically or putting an if-else for every branded string?
Thanks!
I found the answer: I took the example from http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html and I modified it so that the android:text string reference is inside the style definition and not inside the layout.xml. The string will then be automatically updated whenever you change themes.

Categories

Resources