I asked a similar question here... I got some tutorials in the answers. But this question is diffrenet. because none of that method do not works in my project.
I want center all icons in toolbar
I test all availabe ways ...but always icons are floating in left.
I want centering icons (I prefer left icon floats left and right icon floats right and other icons floats center)
Activity.Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" // I add vertical
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent" // I add match_parent
android:layout_gravity="center" /> // I add center
</LinearLayout>.
Also in tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
android:layout_gravity="center" // I add center
xmlns:android="http://schemas.android.com/apk/res/android" />
in main_menu.xml
<menu 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" tools:context=".MainActivity">
<item
android:id="@+id/action_forward"
android:title="forward"
android:icon="@drawable/ic_action_arrow_forward"
app:showAsAction="always"
></item>
<item
android:id="@+id/action_zoom_in"
android:title="zoom in"
android:icon="@drawable/ic_action_zoom_in"
app:showAsAction="always"
></item>
<item ...
and for above code (main_menu.xml) in <item>
I tried all these codes:
android:layout_width="match_parent"
android:layout_weight=".2" // 20%
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
I realy I do not know whats can I do to align icons. I googled hours and I tried all senario
what is my wrong? for all tests I can not see any change ...
before I wanted pic2 (in above picture) But now I just want center it!
For my all test I only get pic 1. without any change.
suggest use android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" instead. To keep using default styles for the customised TextView, try something like style="@style/TextAppearance. AppCompat.
In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.
you can try applying
app:buttonGravity="center"
on Toolbar, that worked perfectly for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With