1

In my app user can chose in what side he'll have NavigationView

how set layout_gravity="end" programatically ?

<android.support.design.widget.NavigationView
    android:id="@+id/main_navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    >


    <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/light_grey"
    android:dividerHeight="0.5dp">

    </ListView>

</android.support.design.widget.NavigationView>
Eugene
  • 43
  • 1
  • 2
  • 12

4 Answers4

2

you can play with this:

 DrawerLayout.LayoutParams params = new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.WRAP_CONTENT, DrawerLayout.LayoutParams.MATCH_PARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            params.setLayoutDirection(Gravity.START);
        }
        navigationView.setLayoutParams(params);
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
2

Kotlin

For this to work, the entire xml has to be a DrawerLayout (with id drawer_layout, containing a NavigationView)

val params = drawer_layout.main_navigation_view.layoutParams as DrawerLayout.LayoutParams

then to open from the left side

params.gravity = Gravity.START

or from the right side

params.gravity = Gravity.END

kk.
  • 3,747
  • 12
  • 36
  • 67
J7bits
  • 688
  • 8
  • 14
0

Get your DrawerLayout variable declared and initialize in Activity and use following line to set its gravity :

drawer.openDrawer(Gravity.LEFT);

Where drawer is variable of your DrawerLayout and you can set gravity according to your requirement.

R.R.M
  • 780
  • 4
  • 10
0

you can use openDrawer() method to open drawer programaticaly

void openDrawer (int gravity)

Open the specified drawer by animating it out of view.

Parameters gravity int: Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. GravityCompat.START or GravityCompat.END may also be used.

sample code

drawer.OpenDrawer(Gravity.LEFT);
drawer.OpenDrawer(Gravity.RIGHT);
Community
  • 1
  • 1
AskNilesh
  • 67,701
  • 16
  • 123
  • 163