Tolou Tolou - 1 month ago 12
Android Question

make a RTL navigation Drawer in android

I want to make my navigation open from

right to left
. But as soon as I change any of these steps my program face to error force closed after click on navigation after running the app.

My main activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="right"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>


My custom toolbar.xml

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

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="right"
android:background="@color/colorPrimary">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:padding="8dp"
android:textColor="#fff"
android:textSize="18sp"
/>

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cnews"
android:tint="#fff"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>


//content
<include layout="@layout/content_main" /> </LinearLayout>


My appbar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="test.MainActivity">

<include layout="@layout/toolbar_main" />

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


my main activity.java

import android.app.FragmentManager; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.home_page) {
Home_page_fragment homeFragment = new Home_page_fragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.mainContent , homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.tour_page) {
Tour_page_fragment tourFragment = new Tour_page_fragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.mainContent , tourFragment, tourFragment.getTag()).commit();

} else if (id == R.id.lstour_page) {
LsTour_page_fragment LstourFragment = new LsTour_page_fragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.mainContent , LstourFragment, LstourFragment.getTag()).commit();

} else if (id == R.id.hotel_page) {

} else if (id == R.id.agency_page) {

} else if (id == R.id.news_page) {

}else if (id == R.id.logbook_page) {

}else if (id == R.id.message_page) {

}else if (id == R.id.favorite_page) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} }


I changed
"Start" to "end"
in
.xml
main activity and
java
main activity get force closed!

I change "start" to "right" get force closed!!

I used
RTL library
but again get forced closed!!!

Thanks in advance

Answer

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.

Comments