0

I want to style all my ImageButtons in a theme. After searching for quite some time I found the solution to my problem. But I don't know why it works like it does.

My main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_foreground" />
</LinearLayout>

This is my original theme that didn't work. It styles my TextView but ignores the ImageButton. The result is shown in the screenshot below.

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:imageButtonStyle">@style/redBackground</item>
        <item name="android:textViewStyle">@style/redBackground</item>
    </style>

    <style name="redBackground">
        <item name="android:background">#FF0000</item>
    </style>
</resources>

enter image description here

And here's the theme that works:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="imageButtonStyle">@style/redBackground</item>
        <item name="android:textViewStyle">@style/redBackground</item>
    </style>

    <style name="redBackground">
        <item name="android:background">#FF0000</item>
    </style>
</resources>

enter image description here

The only difference is the missing 'android:' prefix in front of the 'imageButtonStyle' attribute.

So my questions are:

  • What is the difference between imageButtonStyle and android:imageButtonStyle?
  • Why does android:textViewStyle work but not android:imageButtonStyle? They are both defined the plattforms 'attrs.xml'.
  • Why is there no textViewStyle (without android prefix)? Removing the prefix yields an error.
  • Where are the attributes defined that have no prefix? Apparently not in the plattforms 'attrs.xml'.
  • Where can I find proper documentation for the whole style stuff? Of course I halve already read the respective Google docs (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html). But still i have basic questions like this one.

Interestingly, it seems like the 'android:imageButtonStyle' version has worked some years ago: How to apply an style to all ImageButtons in Android?. I haven't tested that myself, though.

And here's the post that proposed removing the android prefix. Including unanswered comments that ask why it works: buttonStyle not working for 22.1.1

Tobias Uhmann
  • 2,757
  • 2
  • 25
  • 35

2 Answers2

0

android tag that you use is used for attribute coming from Android SDK.

app tag is used if you are using the support library.app is just a namespace for any custom parameters for a custom View.

This can be anything but if you see the root element there's probably a line xmlns:app="http://schemas.android.com/apk/res-auto" that assigns the namespace

You may also see other namespaces if you are using custom views (of your own or form a library).

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • The posted code is complete. I didn't leave out any namespace declarations. But that leads me to the next question: Why do I apply an AppCompat attribute (srcCompat) although I didn't use the support libraries ImageButton? I was never really aware of that.. – Tobias Uhmann Sep 12 '17 at 13:50
  • Ok, I did some research about the support libraries layout inflater in general that answers my question in my comment but unfortunately not my original ones. – Tobias Uhmann Sep 12 '17 at 14:09
0

In case that anyone else stumbles across the question: I've found the answer in this Droidcon talk: https://youtu.be/Jr8hJdVGHAk?t=21m12s

The topic is handled in a minute starting at 21:12.

As I understand it, specifying no namespace results in the global namespace being searched which seems to include the support libraries attributes. And indeed both, the SDK's R.attr as well as the support library's R.attr define the imageButtonStyle attribute (with slightly different descriptions). However, the support library does not define a textViewStyle attribute. So that explains why you can't omit it's android: prefix.

To answer my last question concerning the documentation: Despite the Google guide and the R.attr classes' documentation, the video mentioned above and this Google I/O talk are quite informative: https://www.youtube.com/watch?v=TIHXGwRTMWI

So the only question that is left open is why the SDK's imageButtonStyle does not work.

Tobias Uhmann
  • 2,757
  • 2
  • 25
  • 35
  • "why the SDK's `imageButtonStyle` does not work" - You're apparently in an `AppCompatActivity`, which automatically creates the `AppCompat` versions of certain SDK `View`s if they're found in the layout. `ImageButton` is one of those that is "upgraded", and `AppCompatImageButton` will use the `imageButtonStyle` attribute in the app's namespace. – Mike M. Sep 14 '17 at 01:16