'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

<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" />

My custom_toolbar.xml

<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

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

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.



Solution 1:[1]

I suggest you that use this code to Oncreate main_activity.java :

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58