1987akam
1987akam

Reputation: 43

How can i put my logo on Center of Android Action Bar?

This is my menu-main.xml.

The picture below the code shows where I want to put the logo on my app.

<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="com.example.akam.billettogram.MainActivity">**strong text**
    <item
        android:id="@+id/logo"
        app:showAsAction="always"
        android:title=""
        android:layout_gravity="center"
        android:icon="@drawable/logo"/>
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>
</menu>

Position that I want to put my logo in.

Upvotes: 0

Views: 4300

Answers (1)

Evin1_
Evin1_

Reputation: 12866

So, the easiest way for you to do it is creating a new Empty Activity in Android Studio, make sure it is Empty and not Blank!

This template will build all the code needed:

  • It will create your Activity.java.
  • Create the layout with an AppBarLayout, Toolbar and CoordinatorLayout.
  • It will create the needed styles in your res/values.
  • It will set the Activity with the proper style in the Manifest (NoActionBar).

So... you will just have to add your icon and maybe a TextView inside the automatically created Toolbar (in the layout.xml) as seen in the following snippet:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:src="@android:drawable/btn_star_big_off"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Hello world"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>

Below is an image with the result:

enter image description here

Upvotes: 1

Related Questions