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))
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 have this text view:
<TextView
android:id="#+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/Paragraph3"
tools:text="TÃtulo"/>
and trying to apply a custom font programatically after this, using this extension:
fun TextView.setCustomFont(font: Int) {
typeface = ResourcesCompat.getFont(context, font)
}
but its not work, if I remove the textAppearence from XML everything works.
Any ideas?
UPDATE 1
Already followed the documentation to setup the fonts:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
I solve this removing textAppearence from XML and doing a setTextAppearence in my code. Since appears that textAppearence xml attribute overrides all font customizations.
This seems to manage it.
After taking a snapshot of the layout and inspecting it with Layout Inspector, how do I find what font is used for the selected TextView or AppCompatTextView? Text and Theme property groups have no mentions of fontFamily property. However, I see the TextView has a custom font on it.
I'm running Android Studio 3.2.1
The only thing i can see about font is the typeface (text -> getTypeFaceStyle(), "bold", "normal" ...)
If the font is defined in styles, you can see the textviews style (theme) in LayoutInspector at the bottom
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);
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.