How to declare attr for layout_gravity property

63 views Asked by At

I have a project which comes with two flavors.

  • In Flavor1 I want to have the NavigationDrawer with the menu on the left side.
  • In Flavor2 I want to have the NavigationDrawer with the menu on the right side.

My current solution is to define an attribute in the apps attrs.xml:

<resources>
    <attr name="navigationDrawerPosition">
        <flag name="start" value="0x00800003" />
        <flag name="end" value="0x00800005" />
    </attr>
</resources>

The attribute is referenced in my XML layout:

<com.google.android.material.navigation.NavigationView
            android:id="@+id/menu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="?attr/navigationDrawerPosition"
            android:fitsSystemWindows="true">

        <include
                android:id="@+id/menuDrawer"
                layout="@layout/menu_drawer" />

    </com.google.android.material.navigation.NavigationView>

An finally I set the attribute in the theme of the flavor's styles.xml:

Flavor1:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
        <item name="navigationDrawerPosition">start</item>
...
    </style>

Flavor2:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
        <item name="navigationDrawerPosition">end</item>
...
    </style>

In fact this working somehow, but just due to the fact that I copied the attribute values from the Android SDK's attrs.xml:

   <flag name="start" value="0x00800003" />
   <flag name="end" value="0x00800005" />

Is there a better way to achieve the initial goal? Can I somehow reference the android layout_gravity attributes in my attribute definition? Something like

<attr name="navigationDrawerPosition" type="reference" from="?android:attr/layout_gravity">

Or any better idea?

0

There are 0 answers