Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar image centered

Using the last android.support.v7.widget.Toolbar I want to center an image in the toolbar but it keeps staying on the left.

The best way for me would be to use toolbar.setLogo() method and to center the logo but i don't see any way to do it.

I use a layout for the Toolbar:

  <android.support.v7.widget.Toolbar style="@style/ToolBarStyle"                                    xmlns:android="http://schemas.android.com/apk/res/android"                                    android:layout_width="match_parent"                                    android:layout_height="wrap_content"                                    android:background="?attr/colorPrimary"                                    android:minHeight="@dimen/abc_action_bar_default_height_material">  </android.support.v7.widget.Toolbar> 

Is there any possibility to add an image (logo) to the toolbar and center it? programatically or in the layout?

like image 474
Nicolas Hauzaree Avatar asked Dec 17 '14 20:12

Nicolas Hauzaree


People also ask

How do I center an image on android?

In LinearLayout , use: android:layout_gravity="center" . In RelativeLayout , use: android:layout_centerInParent="true" .

How do I center text in toolbar?

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.

How can I center the logo on the toolbar?

To guarantee the logo being centered, try using a layer-list as a background of your toolbar instead of trying to use an image as a view.

How to center image in navbar using CSS?

Using CSS: In this method, we use our own styling to center the image in the navbar. We are going to embed CSS code into HTML code. Here we have given flex property to our brand (image or logo), the width of 100%, and justify-content as the center.

How do I Center an image in an HTML page?

To center an image, set left and right margin to auto and make it into a block element: Note that it cannot be centered if the width is set to 100% (full-width).

How to center an image horizontally in AutoCAD?

1 Text-Align. The first way to center an image horizontally is using the text-align property. 2 Margin: Auto. Another way to center an image is by using the margin: auto property (for left-margin and right-margin). 3 Display: Flex. The third way to center an image horizontally is by using display: flex. ...


2 Answers

Toolbar is just a ViewGroup, you can customize is as much as you want.

Try this :

<android.support.v7.widget.Toolbar    style="@style/ToolBarStyle"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="?attr/colorPrimary"    android:minHeight="@dimen/abc_action_bar_default_height_material">      <ImageView         android:layout_width="wrap_content"         android:contentDescription="@string/logo"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:src="@drawable/ic_launcher"/>   </android.support.v7.widget.Toolbar> 

This should bring your imageView in center of toolbar.

like image 136
Abhinav Puri Avatar answered Sep 30 '22 17:09

Abhinav Puri


If you have any views and can't quite get the logo centered, it will be because the centering is based on remaining space, not the entire toolbar.

To guarantee the logo being centered, try using a layer-list as a background of your toolbar instead of trying to use an image as a view.

toolbar.xml

<android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/toolbar"     android:layout_width="match_parent"     android:layout_height="?attr/actionBarSize"     android:background="@drawable/toolbar_background"     android:theme="@style/ThemeOverlay.AppCompat.Dark"     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"     app:layout_scrollFlags="scroll|enterAlways"     app:layout_collapseMode="pin"> </android.support.v7.widget.Toolbar> 

toolbar_background.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">   <item>     <shape android:shape="rectangle">       <solid android:color="@color/colorPrimary" />     </shape>   </item>   <item>     <bitmap android:src="@drawable/logo" android:gravity="center" />   </item> </layer-list> 

That should ensure a centered logo, with a specific background colour.

like image 35
Blueberry Avatar answered Sep 30 '22 16:09

Blueberry