Suppose the following code:
<MyCustomComponent
style="#style/Base.TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In the custom component I have an EditText which I need to set its style attribute to #style/Base.TextAppearance.AppCompat.Headline in java code, according to the style attribute in the layout. How can I do that?
I am creating a custom component therefore I also need to know which style has been selected in layout by user of my component.
Is it available through AttributeSet? If yes please let me know how?
For styling TextViews programmatically, you must have a style inheriting a TextAppearance style, and you can apply it using the following code:
if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context,android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }
Define a custom style in style.xml like this:
<style name="MyHeadlineTextAppearance" parent="Base.TextAppearance.AppCompat.Headline"/>
Then set it to your TextView as below:
textView.setTextAppearance(context, R.style.MyHeadlineTextAppearance);
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
I'm using Android Studio and have a SwitchCompat widget in my main activity. The default fontfamily it had was sans-serif-medium and I changed it to quicksand_light. I also have some TextViews with each of their fontfamily's set to quicksand_light. On the design tab of the xml file for my activity it shows the SwitchCompat having the quicksand_light fontfamily just as the TextViews, but when I run it on my phone or on an emulator the SwitchCompat's fontfamily is sans-serif-medium. Is there something extra I need to do to change the fontfamily or is this a bug or is this just me?
I haven't dived deep into why it's not working correctly when defining the fontFamily attribute in xml, but IT WORKS if you set the typeface programmatically.
Here's an example using data-binding.
Add the following data-binding adapter:
#BindingAdapter("labelTypeface")
fun setLabelTypeface(view: SwitchCompat, #FontRes id: Int) {
view.typeface = ResourcesCompat.getFont(view.context, id)
}
and use it in your layout:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:labelTypeface="#{R.font.stratum}"
... />
The only way I've been able to set the font family on the switch's thumb (not its label) is with:
my_switch.setSwitchTypeface(ResourcesCompat.getFont(context, R.font.my_font))
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.
Through layout.xml it is working.
<RatingBar
android:id="#+id/hotel_rating_bar"
style="#style/custom_ratingbar" />
But i want to set the style Dynamically.
Any Answer will be highly appreciated...
Dynamically changing the style of views at runtime is not yet supported in Android, except for TextViews via setTextAppearance.
You have to set the style before the view gets drawn,
either via XML or via Java in the constructor.
RatingBar ratingBar = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
I would like to set a red colour text in my app, but I don't know how.
Please provide references. Thanks.
If you want it to set in XML layout then use:
<TextView
...
...
android:textColor="#FF0000" >
</TextView>
If you want to set programmatically then use:
textview.setTextColor(Color.RED);
//textview must be defined in your class
Read the docs for TextView, specifically for textColor.
Use the following in your xml where you want to change the color to red and by using the other hexacode of color you can change to any other color. This is an example to set the color to red:
android:textColor="#FF0000"
Use this in your layout:
<TextView>
....
textColor="red"
....
</TextView>
Create a custom style resource which uses an Android Theme as a parent then override the text colors as defined in the Android theme.xml. Reference this new style in your AndroidManifest.xml's application tag.