make a RTL navigation Drawer in android

天涯浪子 提交于 2019-12-06 15:37:05

The problem is that your customized Gravity is not defined and returns NULL. First of all, you should update your menu.xml and build an item as an icon for right menu, like this:

<item
        android:id="@+id/menu_right"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@android:drawable/ic_dialog_dialer"
        android:title="@string/action_menuright"
/>

And in main_activity.java disable the default toggle drawer which opened from left and update this:

toggle.syncState();
toggle.setDrawerIndicatorEnabled(false);//this is added line

and similary udate this one:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
         <   here:  add this line:  >
    DrawerLayout drawer;

and finally change this class to:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    // noinspection SimplifiableIfStatement
    if (id == R.id.menu_right) {
        if(drawer.isDrawerOpen(Gravity.RIGHT) ) {
             drawer.closeDrawer(Gravity.RIGHT);
        }
        else{
             drawer.openDrawer(Gravity.RIGHT);
        }
        return true;
    }

    return super.onOptionsItemSelected(item);
}

and everything is fine now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!