how to left to right toolbar api 16
Asked Answered
R

2

12

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);
    }
Reborn answered 17/10, 2018 at 10:34 Comment(3)
Support for RTL layout direction was included in API 17 and it doesn't work before it. There's no solution.Devi
you can use multiple layout files to fix thisPhillipphillipe
@RahulKumar can you explain more please? or give me a link.Reborn
N
7

Unfortunately it's not possible to use android:layoutDirection="rtl" in API 16 or lower.

android:layoutDirection="rtl" was first included in API 17 and higher.

If you want more information I definitely recommend to read this article.

But there is always a solution in programming.

This question is similar to yours and maybe it will help you.

Nepenthe answered 4/11, 2018 at 12:51 Comment(0)
I
2

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

Indicative answered 5/11, 2018 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.