Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Toolbar logo in XML

Is it possible to set Toolbar logo in xml? I have tried this so far:

1.

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    android:logo="@drawable/my_logo"
    />

2.

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    app:logo="@drawable/my_logo"
    />

EDIT

I'm using toolbar as an actionbar in my AppCompatActivity:

setSupportActionBar(findViewById(R.id.toolbar));

I'm able to set logo from code:

((Toolbar) findViewById(R.id.toolbar)).setLogo(R.drawable.my_logo);

But I'd like to set logo either from layout XML or from Style/Theme XML.

like image 569
Denis Kniazhev Avatar asked Jun 15 '15 17:06

Denis Kniazhev


4 Answers

Do Change

app:logo="@drawable/my_logo"

to

app:navigationIcon="@drawable/my_logo"

UPDATE

Add Linearlayout into your toolbar xml tag.

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/grey">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/media_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="new post image"
            android:scaleType="centerCrop"
            android:visibility="visible" />
        <TextView
            android:id="@+id/toolbar_subtitle"
            android:textSize="14sp"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</android.support.v7.widget.Toolbar>
like image 148
Karan Maru Avatar answered Nov 06 '22 09:11

Karan Maru


Using xml only

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="ActionbarLogo">
    <item name="android:logo">@drawable/ic_menu_camera</item>
    <item name="android:displayOptions">useLogo</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:actionBarStyle">@style/ActionbarLogo</item>
    <item name="windowNoTitle">true</item>
</style>

Now apply AppTheme.NoActionBar theme to your activity( and if you want to remove toolbar title use getSupportActionBar().setDisplayShowTitleEnabled(false); after setSupportActionBar(toolbar);)

<activity
  android:name=".MainActivity"
  android:theme="@style/AppTheme.NoActionBar">
  <intent-filter>
      <action android:name="android.intent.action.MAIN"/>

      <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity>
like image 22
HASSAN MD TAREQ Avatar answered Nov 06 '22 07:11

HASSAN MD TAREQ


Actually you can, but it will not work well with name on Toolbar. You can just put ImageView into the body of Toolbar, like you do this with Layout.

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

   <ImageView
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   app:logo="@drawable/my_logo"/>

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

Better if you will do this from the code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_logo);
like image 5
tarasmorskyi Avatar answered Nov 06 '22 09:11

tarasmorskyi


To display logo icon set this icon at runtime in ActionBar.

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(@drawable/my_logo);
like image 3
strike Avatar answered Nov 06 '22 07:11

strike