Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to left to right toolbar api 16

I'm using NavigationView and add toolbar with include . I use android:layoutDirection="rtl" and all apis works fine but in api 16 it doesn't work.

So how can i left to right toolbar in api 16?

drawer_layout.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:layoutDirection="rtl"
   tools:openDrawer="end">


   <FrameLayout
       android:id="@+id/content_frame"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/appbar" />

   <include
       android:id="@+id/appbar"
       layout="@layout/app_bar_main"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:visibility="visible" />

   <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:layoutDirection="rtl"
       app:headerLayout="@layout/nav_header_main"
       app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".view.activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:elevation="0dp">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary" />
    </android.support.design.widget.AppBarLayout>
</LinearLayout>

MainActivity.java

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
like image 642
Hamed Karami Avatar asked Oct 17 '18 10:10

Hamed Karami


1 Answers

There is no way to support rtl Toolbars in APIs below 17. Try to use a custom Toolbar such as RtlToolbar.

Build.gradle:

dependencies {
    compile 'com.alirezaafkar:toolbar:1.1.2'
}

layout.xml:

<com.alirezaafkar.toolbar.RtlToolbar
    android:id="@+id/toolbar_main"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:direction="rtl"
    app:optionsMenu="@menu/menu_main"
    app:font="@string/font_path"
    app:navigationIcon="@drawable/ic_menu"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:title="@string/app_name"/>

Result:

enter image description here

like image 197
aminography Avatar answered Sep 28 '22 01:09

aminography