20

What are the differences between R.styleable, R.style and R.attr? I found TextAppearance in all of these three classes.

Zahid Hasan
  • 555
  • 1
  • 6
  • 12
  • Where did you find these? – Code-Apprentice Jan 02 '13 at 21:29
  • 2
    Are you looking at the android.R class? This is certainly a good question, if so. I am disappointed by the Android API docs. This is only another of several examples where I see that the docs lack important details that would be helpful to developers. – Code-Apprentice Jan 02 '13 at 21:32

2 Answers2

23

R.style has all styles android provided (including all Theme android provided). E.g., Theme.Translucent, Widget.AbsListView.

R.attr has all attrs android provided (that could be set to view or window). E.g., layout_width can be set to view, windowIsFloating can be set to window.

R.styleable has all attrs of a specific view or window that android provided AND can be defined in a style. E.g., FrameLayout_Layout_layout_gravity: layout_gravity can be styled for FrameLayout, Window_windowIsFloating: Flag indicating whether this is a floating window.

To answer your question, TextAppearance is a attribute (R.attr) AND it is declared styleable, attrs.xml:

<attr name="textAppearance" format="reference" />
<declare-styleable name="TextViewAppearance">
     <!-- Base text color, typeface, size, and style. -->
     <attr name="textAppearance" />
</declare-styleable>

TextAppearance is also a Theme/ Style (Theme is just a style), styles.xml:

<style name="TextAppearance">
    <item name="android:textColor">?textColorPrimary</item>
    <item name="android:textColorHighlight">?textColorHighlight</item>
    <item name="android:textColorHint">?textColorHint</item>
    <item name="android:textColorLink">?textColorLink</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">normal</item>
</style>

Just in case you don't understand what the "?" means, check: Question mark (?) in XML attributes for Android And in case you are puzzled by what is declare-styleable, check: Difference between declare-styleable and style

Community
  • 1
  • 1
Helin Wang
  • 4,002
  • 1
  • 30
  • 34
  • Then can you tell what attrs we can use when creating/inheriting a TextAppearance style ? `` says it is a reference but on which styleable ? – Pol Mar 28 '17 at 20:42
0

R.style is used for theme definitions (configure default or specific sets of styles for elements to be reused in your layouts).

R.styleable contains individual attrs. R.attr is used to define attributes for custom views. Say you create your own custom view called CardView, and it takes in 2 Strings, then builds its layout based on the size of those Strings. You can set those as attributes that are assigned in your XML layouts via R.attr (more info / better explanation here).

PrplRugby
  • 631
  • 5
  • 16
  • 1
    It's incorrect. R.attr is used to define individual attrs while R.stylable is used to define attrs for custom Views. – szcoder Oct 28 '13 at 02:20
  • From the documentation linked: `To define custom attributes, add resources to your project. It's customary to put these resources into a res/values/attrs.xml file.` – PrplRugby Jan 01 '14 at 22:12