Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using <include> tag with ?attr/myAttr

I'm trying to include different layouts in my View depending on the parent Theme.

Following the idea:

attrs.xml

<attr name="themeLayout" format="reference" />

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="themeLayout">@layout/layout_a</item>
</style>

<style name="AppThemeSecond" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="themeLayout">@layout/layout_b</item>
</style>

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="?attr/themeLayout" />

</RelativeLayout>

When I run the Code above I'll get the following Exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.MainActivity}: android.view.InflateException: You must specifiy a valid layout reference. The layout ID ?attr/themeLayout is not valid.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: You must specifiy a valid layout reference. The layout ID ?attr/themeLayout is not valid.
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:866)

What am I doing wrong? Is it possible to do this at all?

Note #1: I set the Theme of my MainActivity to android:theme="@style/AppTheme"

Note #2: In the Design-View of the Layout Editor everything works as expected. If I switch the Theme, the include gets updated properly.
See the following Screenshots as well:

preview design tab android studio

like image 305
user3420815 Avatar asked May 15 '15 12:05

user3420815


1 Answers

Unfortunately this is not possible but I really like the idea. I have tracked the flow of LayoutInflater and it requieres the layout attribute to be TypedValue.TYPE_REFERENCE which means ?attr is not allowed. They even left a comment in the method to explain.

public int getAttributeResourceValue(int idx, int defaultValue) {
    int t = nativeGetAttributeDataType(mParseState, idx);
    // Note: don't attempt to convert any other types, because
    // we want to count on aapt doing the conversion for us.
    if (t == TypedValue.TYPE_REFERENCE) {
        return nativeGetAttributeData(mParseState, idx);
    }
    return defaultValue;
}

android.content.res.XmlBlock.java:385

Basically you have done nothing wrong -- the inflation in Preview works differently which caused the confusion.

like image 193
Lamorak Avatar answered Oct 09 '22 20:10

Lamorak