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
Related
I read about Android styles and themes today and tried to apply it to a list in my app as a test. The app has a list element which have TextViews added to it programmatically.
According to the docs applying a style as a theme affects child views too.
So I tried this:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextAppearance.AppCompat.Small"/>
I expected that the TextView texts in the list became small, but nothing happened.
I also tried #style/TextAppearance.MaterialComponents.Headline1, but it had no effect either.
Why is that?
I didn't change any of the style or theme settings myself. I use the default settings which Android Studio generated for the project.
Shouldn't applying a style as a theme to a view like above change something?
You are using the style "TextAppearance.AppCompat.Small" which is applicable to a TextView and not a generic one like ListView.
You need to apply the style to the list item which is the TextView. You can create a custom style for text appearance, font, sizing, etc and reuse it in your entire app.
If you are creating your views programmatically, you can use a custom adapter (which extends your default adapter), override getView method to apply your style. Refer this
Refer to this awesome article by #Nick Butcher
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
I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.
I have 1 Class that are opened two different ways. One way is it's opened from a sliding drawer and another way is as a dialog. Below you can see both of them. However, you can see that the edittext does not look the same in both. How can I modify the dialog style to look like the fragment?
Here is how it is made:
final EditText editTextView = new EditText(a);
editTextView.setHint(R.string.hintNote);
editTextView.setTag(tag); editTextView.setId(_id);
You can add the style attribute to the layout XML. They will both then use the style specified:
style="#android:style/Widget.EditText"
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.