0

I have a quick question!

In my styles.xml file, I have

<style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:padding">20px</item>
    <item name="android:background">#9cd0e8</item>
    <item name="android:textColor">#254b7c</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textStyle">bold</item>
</style>

And in my activity_main.xml, I have

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:theme="@+styles/TextViewStyle"
    android:text="Sample Text"/>

What I am trying to do is, in my Android application, on a certain activity I plan to place many TextViews with similar properties. Instead of writing these 'properties' every time with each TextView instance, I grouped them together in a style in styles.xml file and set theme of each of my TextViews to that style.

It works fine and does what I want it to do, but only with APIs above 21! My application's supposed to support devices from API level 15 up. Why is my approach not working with lower APIs?

Please help soon. I need to finish this soon.

EDIT

By 'working', I meant that the attributes I set in my style (padding, color, etc.) appear on the TextViews as they should. In lower APIs however, the TextViews appear as if I had not applied any attribute on them. Plain text appears instead of a styled one.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Saif Khan
  • 309
  • 1
  • 2
  • 19
  • 1
    What's different between the two versions? As in, what isn't working? – Chris Stillwell Apr 12 '16 at 19:58
  • @ChrisStillwell By 'working', I meant that the attributes I set in my style (padding, color, etc.) appear on the TextViews as they should. In lower APIs however, the TextViews appear as if I had not applied any attribute on them. Plain text appears instead of a styled one. – Saif Khan Apr 12 '16 at 20:00
  • Since you already have the resource file, You don't need the "+" preceding `styles/TextViewStyle`. Not sure if that will fix things for you or not, but worth a try. – Chris Stillwell Apr 12 '16 at 20:10
  • @ChrisStillwell No, that didn't fix my problem. – Saif Khan Apr 12 '16 at 20:13

1 Answers1

2
  1. remove parent from your style
  2. remove android:theme from textView, (why there is + sign?)
  3. instead of theme put this into your textView

style="@style/TextViewStyle"

btw, use dp instead of px ;)

  • Thanks! I had that + there because for some reason my Studio was giving me an error without it. The problem was with android:theme, I think. Why dp and not px, though? – Saif Khan Apr 12 '16 at 20:20
  • look here: http://stackoverflow.com/a/2025541/3360896 and here: http://developer.android.com/guide/practices/screens_support.html – Krzysztof Turek Apr 12 '16 at 20:24