Tord Larsen
Tord Larsen

Reputation: 2836

Howto use the DrawerLayout with ConstraintLayout

I have the android.support.design.widget.NavigationView with DrawerLayout working ok but now I wanted to port it to ConstraintLayout. This is not going so well and wanted to ask for advice?

ORIGINAL WORKING XML:

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.port.android.ui.ActivityMain">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_view_header"
        app:menu="@menu/menu_navigation">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:padding="20dp">

            <Button
                android:id="@+id/btn_exit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address" />

        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

PORTING TO ConstraintLayout: (not working the drawer is stale, cannot swipe in/out)

The "@+id/frame" is where i insert Frags

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.port.android.ui.ActivityMain">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar"
            android:layout_width="0dp"
            android:layout_height="56dp"
            tools:layout_editor_absoluteY="100dp"
            tools:layout_editor_absoluteX="8dp" />

        <android.support.constraint.ConstraintLayout
            android:id="@+id/frame"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.22000003"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginLeft="16dp">

        </android.support.constraint.ConstraintLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_view_header"
            app:menu="@menu/menu_navigation"
            android:fitsSystemWindows="true"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">

            <Button
                android:id="@+id/btn_exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address" />

        </android.support.design.widget.NavigationView>
    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.DrawerLayout>

Upvotes: 4

Views: 10300

Answers (3)

ThingsMaster3000
ThingsMaster3000

Reputation: 131

Sorry, late to the party, but I don't understand how top answer works since Android Studio is preventing to do that.

Because of that I investigated further and found this to work:

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        tools:ignore="HardcodedText">

        <!--your ConstraintLayout code here, like this button -->
        <Button
            android:id="@+id/butt"
            android:layout_width="60dp"
            android:layout_height="36dp"
            android:layout_marginTop="5dp"
            android:text="30"
            app:backgroundTint="@color/teal_200"
            app:layout_constraintEnd_toStartOf="@+id/butt"
            app:layout_constraintStart_toEndOf="@+id/butt"
            app:layout_constraintTop_toBottomOf="@+id/editTextIzvornaValuta" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout>

Upvotes: 1

Chandan kushwaha
Chandan kushwaha

Reputation: 949

You can try this ..

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout     
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <android.support.v4.widget.DrawerLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">    
    </android.support.v4.widget.DrawerLayout>
    
    </android.support.constraint.ConstraintLayout>

Upvotes: 1

Harsha HD
Harsha HD

Reputation: 1

You can combine your button and frame layout widgets in a constraint layout instead of a linear layout. But, you should use linear layout for defining NavHostFragment and you can seperately define NavigationView as the last child of DrawerLayout. Note: Here LinearLayout and ConstraintLayout are 2 childs of drawer layout. Linear layout holds your NavHostFragment, ConstraintLayout holds your Toolbar, widgets that you define in FrameLayout, and Exit button. Drawer Layout will be the 3rd child.

Upvotes: 0

Related Questions