3

I'm new on this site.
I'm now working on application in android studio, I'm looking for a nice menu and found the Navigation Drawer, I searched how can I create my own Navigation but I want it to be on the right side of the screen. How can I do it?

Same Question about checkbox, I have the Checkbox on the left side and the text on the right side/ I want it to be the opposite.

Hope you understand my problem.

XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello Drawer"
        android:layout_gravity="center"
        android:gravity="center"/>
</LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="right">
</android.support.design.widget.NavigationView>

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

Java:

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if(mToggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

2

About the right side navigation drawer...I remember struggling with this question. I checked many answers from anywhere. Mostly they say: use setLayoutDirection method which works fine but for api>=17. I do the following:

0. Support Libraries

 dependencies {
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:design:25.3.1'
}

1. Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:openDrawer="end">

    <!--content of your Activity. Could be fragment container. Your toolbar is placed there-->
    <include
        layout="@layout/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right|end">

        <LinearLayout
            android:id="@+id/navigationLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!--navigation header. like user profile image &...-->
            <include
                layout="@layout/drawer_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <!--recycler of your navigation items-->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/navigationRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

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

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

main_content.xml

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

    <RelativeLayout 
        android:id="@+id/toolbarRL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/toolbarHamburger"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:src="@drawable/hamburger" />

    </RelativeLayout>

... other stuff ...

</RelativeLayout>

2. Activity

//Hamburger icon of your toolbar
            ImageButton hamburger= (ImageButton) findViewById(R.id.toolbarHamburger);
            hamburger.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT))
                        drawerLayout.closeDrawer(Gravity.RIGHT);
                    else
                        drawerLayout.openDrawer(Gravity.RIGHT);
                }
            });

Also you should close drawer using drawerLayout.closeDrawer(Gravity.RIGHT) when a recycler's item is clicked.

About CheckBox I don't know if there's any solution to switch positions of text and CB. But I wouldn't bother looking for it. I'd rather using a linearLayout containing CB & TV :)

Or maybe if I see it repetitive I create a custom view.

CB: CheckBox, TV: TextView

navid
  • 1,022
  • 9
  • 20
  • I tried doing that, I didnt understand where should I put the menu button? Is that ImageButton? – Revolution3z Jan 31 '18 at 06:38
  • If by menu button you mean the button that must open the drawer yes. Its the ImageButton. I added its layout now – navid Jan 31 '18 at 08:41
  • Thank you so much, Ill try this and let you know. The background is the icon menu? Or thr scr? – Revolution3z Jan 31 '18 at 10:43
  • You're wlc:) I hope it works for you. The background is set to nothing. @drawable/hamburger is the icon's picture – navid Feb 04 '18 at 17:19