Access a value from the current theme in an xml layout - android

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.

Related

Theme/Style/Text appearance, which one takes precedent?

As my title, when I'm using a theme, style or text appearance, which one takes precedent?
Is there any way I can use all three of them? Thanks!
According to the docs, the priorities are as follows:
Applying character- or paragraph-level styling via text spans to
TextView-derived classes
Applying attributes programmatically
Applying individual attributes directly to a View
Applying a style to a View
Default styling
Applying a theme to a collection of Views, an activity, or your
entire app
Applying certain View-specific styling, such as setting a
TextAppearance on a TextView

How can I change the EditText style programatically in Android?

Hi I have a view with a form, and all button, labels, EditText, etc, are defined in a styles files, for example, for EditText I have app_edit_text and app_edit_text_error, my question is how can change in EditText the style from app_edit_text to app_edit_text_error?.
Thanks!
To be honest you can't change style programaticaly. You can only change text
appearance using setTextAppearance(int resId) method:
https://developer.android.com/reference/android/widget/TextView#setTextAppearance(int)
For your purpose you can use setError(String error) method of EditText or implement self states and handle it in EditText subclass. Here is good post about custom states: How to add a custom button state
You can't change styles programatically. You can either replace them with a new Instance (you can specify the styles in the constructor only) or you can use it with a new theme since theme can be changed programatically.
Themes

Is there any way to change a style attribute at run time in android

I want to change the textsize inside a style at run time. Can anyone suggest me how to achieve it. Here is the style "TitleView" in which textsize attribute is defined. I want to change it from 20sp to any other value at run time.
<style name="TitleView">
<item name="android:textSize">20sp</item>
</style>
I want to do it because I've 4 types of textview with different textsize. and In my app user can choose the textsize. So I've changed the text size of other view in relative to the user entered textsize.
Thanks in advance.
You cannot change style attribute at run time.
If you are just trying to change the text size just do:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
this will change your text size and you can do this at any time you want. It is not necessary to change your style attribute. For setting text size at runtime see this
Edit: The poster has altered the original question. This answer is no longer directly applicable.
This cannot be done. The styles along with all the other resource values are all compiled into R.java at compile time.
Instead you could you create two styles and switch the style at runtime using setTextAppearance

How to set margin for all TextView using styles.xml

I've read from another post that it's possible to set attributes like color to every TextView in an application: Setting global styles for Views in Android. However, I can't set layout_margin nor layout_height and layout_width attribute using that method to any textView, using "android:textviewStyle". On the other hand, if I use the style attribute and reference it to a style with all the attributes above, it works. Is there a way that you can use global styles and still set margin?
Thank you in advance
You can set padding instead of margin and you will have the same result.

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 ...

Categories

Resources