Set toolbar logo in XML

Can i set toolbar logo in xml? I have tried this so far:

one.

<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 use the toolbar as an action bar in my AppCompatActivity:

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

I can install the logo from the code:

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

But I would like to set the logo either from an XML layout or from an XML style / theme.

+8
android android-layout material-design android-toolbar
source share
6 answers

Change

 app:logo="@drawable/my_logo" 

to

 app:navigationIcon="@drawable/my_logo" 

UPDATE

Add Linearlayout to 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> 
+6
source share

Actually you can, but this will not work well with the name in the Toolbar. You can simply put the ImageView in the body of the toolbar, as you do with a 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 do this from code:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.my_logo); 
+4
source share

To display the logo icon, set this icon at run time in the ActionBar.

 getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setIcon(@drawable/my_logo); 
+2
source share

Using only xml

 <!-- 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 the AppTheme.NoActionBar theme to your activity (and if you want to remove the 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> 
+2
source share

This could be the answer:

  <Toolbar android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:id="@+id/atc_toolbar" android:background="?android:attr/colorPrimaryDark" android:elevation="4dp" android:navigationIcon="@android:drawable/ic_menu_compass" android:logo="@android:drawable/ic_menu_view" android:title="My application"> </Toolbar> 

In my case, the result is as follows:

Toolbar with logo and navigation

If for some reason the logo does not appear, try starting a new, fresh project, add only layout_width, layout_height, id and logo attributes. Remember to add the toolbar to onCreate through setActionBar or setSupportActionBar. Sincerely, R.

+1
source share

In your code. GetSupportActionBar () setLogo (R.drawable.icon); He will work

+1
source share

All Articles