0

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tom
  • 7,515
  • 7
  • 38
  • 54

1 Answers1

2

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

Mayuri Khinvasara
  • 1,437
  • 1
  • 16
  • 12
  • The problem is I don't create the TextViews. The ListView creates them itself from an array adapter. I thought by using android:theme the text appearance setting flows down the view hierarchy even if the list doesn't use it itself. – Tom Sep 26 '19 at 19:46
  • You can apply style programmatically in your code when your create the text view from the array adapter – Mayuri Khinvasara Sep 26 '19 at 19:48
  • You mean going through the list with `getItemAtPosition()`? I haven't thought of that, but it can work. – Tom Sep 26 '19 at 19:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200035/discussion-between-mayuri-khinvasara-and-tom). – Mayuri Khinvasara Sep 26 '19 at 21:07