Right-to-left navigation box with v7 ActionBarDrawerToggle

I use the navigation box in my application where I want to use material design.

I used the following link to create a navigation box link .

It worked for me. The navigation box works fine with scrolling left and right.

Now that I have to implement my navigation box from right to left. I made the following changes. Hear my code

file: MainActivity.java

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {

    String TITLES[] = {"Home","Events","Mail","Shop","Travel"};
    int ICONS[] = {R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};

    //Similarly we Create a String Resource for the name and email in the header view
    //And we also create a int resource for profile picture in the header view

    String NAME = "Akash Bangad";
    String EMAIL = "akash.bangad@android4devs.com";
    int PROFILE = R.mipmap.ic_profile;

    private Toolbar toolbar;                              // Declaring the Toolbar Object

    RecyclerView mRecyclerView;                           // Declaring RecyclerView
    RecyclerView.Adapter mAdapter;                        // Declaring Adapter For Recycler View
    RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout Manager as a linear layout manager
    DrawerLayout Drawer;                                  // Declaring DrawerLayout

    private ActionBarDrawerToggle mDrawerToggle;                  // Declaring Action Bar Drawer Toggle




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

    /* Assinging the toolbar object ot the view
    and setting the the Action bar to our toolbar
     */
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        //getSupportActionBar().setHomeButtonEnabled(true);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.ic_launcher));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("cek", "home selected");
                if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
                    Drawer.closeDrawer(Gravity.RIGHT);
                } else {
                    Drawer.openDrawer(Gravity.RIGHT);
                }
            }
        });




        mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View

        mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size

        mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
        // And passing the titles,icons,header view name, header view email,
        // and header view profile picture

        mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView

        mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager

        mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager


        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);        // Drawer object Assigned to the view
        mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
                // open I am not going to put anything here)
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                // Code here will execute once drawer is closed
            }



        }; // Drawer Toggle Object Made
        /*Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle*/

       /* mDrawerToggle = new ActionBarDrawerToggle(this, Drawer,
                R.mipmap.ic_launcher, //navigation menu toggle icon
                R.string.app_name,
                R.string.app_name
        ) {
            public void onDrawerClosed(View view) {
                Log.e("onDrawerClosed", "onDrawerClosed");
            }

            public void onDrawerOpened(View drawerView) {
                Log.e("onDrawerOpened","onDrawerOpened");



            }
        };*/

        /*mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar,R.string.openDrawer,R.string.closeDrawer) {

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (item != null && item.getItemId() == android.R.id.home) {
                    if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
                        Drawer.closeDrawer(Gravity.RIGHT);
                    } else {
                        Drawer.openDrawer(Gravity.RIGHT);
                    }
                }
                return true;
            }

            public void onDrawerClosed(View view) {
                Log.e("onDrawerClosed", "onDrawerClosed");
            }

            public void onDrawerOpened(View drawerView) {
                Log.e("onDrawerOpened","onDrawerOpened");



            }
        };*/

        Drawer.setDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();               // Finally we set the drawer toggle sync State
       /* setActionBarUpIndicator((Drawable) mSlider,
                Drawer.isDrawerOpen(Gravity.RIGHT) ?
                        mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);*/


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (item != null && item.getItemId() == android.R.id.home) {
            //open close drawer
            if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
                Drawer.closeDrawer(Gravity.RIGHT);
            } else {
                Drawer.openDrawer(Gravity.RIGHT);
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

file: activity_main.xml

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

<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:elevation="7dp">


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


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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World" />

    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#ffffff"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>


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

android.support.v7.app.ActionBarDrawerToggle import ActionBarDrawerToggle. insted , android.support.v4.app.ActionBarDrawerToggle, , , , .

V4, , .

, Gravity.RIGHT, V7-. .

:

06-17 17:06:22.772    3296-3296/com.demo.nasir.materialdesign E/AndroidRuntime๏น• FATAL EXCEPTION: main
    Process: com.demo.nasir.materialdesign, PID: 3296
    java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
            at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1322)
            at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:289)
            at android.support.v7.app.ActionBarDrawerToggle.access$100(ActionBarDrawerToggle.java:65)
            at android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:201)
            at android.view.View.performClick(View.java:4463)
            at android.view.View$PerformClick.run(View.java:18789)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

.

+4
1

android: layout_gravity , , , : . ( / , .)

XML RIGHT

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice" />

    <!-- Right drawer -->

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>

java

" ", Android Studio, onOptionsItemSelected. ,

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

. NavigationDrawerFragment 3 :

1

public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

2

public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

3

public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}
0

All Articles