Set image on Toolbar

| |
This post show how to set image on Toolbar:

Modify from last post "Add OptionsMenu to Toolbar".

To set background image of Toolbar, edit layout/activity_main.xml to set android:background of <android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidtoolbar.MainActivity">

<android.support.v7.widget.Toolbar
android_id="@+id/toolbar"
android_layout_width="match_parent"
android_layout_height="56dp"
android:background="@mipmap/ic_launcher"/>
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Hello World!" />
</LinearLayout>



Another method, add ImageView inside <android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidtoolbar.MainActivity">

<android.support.v7.widget.Toolbar
android_id="@+id/toolbar"
android_layout_width="match_parent"
android_layout_height="56dp"
android_background="#FFA000">
<ImageView
android_id="@+id/toolbarimage"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@mipmap/ic_launcher"/>

</android.support.v7.widget.Toolbar>
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Hello World!" />
</LinearLayout>



In this examples, also modify menu/menu_main.xml to set all app_showAsAction="ifRoom".
<?xml version="1.0" encoding="utf-8"?>
<menu
>
<item
android_id="@+id/menu_camera"
android_icon="@android:drawable/ic_menu_camera"
android_orderInCategory="100"
android_title="camera"
app_showAsAction="ifRoom"/>
<item
android_id="@+id/menu_compass"
android_icon="@android:drawable/ic_menu_compass"
android_orderInCategory="100"
android_title="compass"
app_showAsAction="ifRoom"/>
<item
android_id="@+id/menu_agenda"
android_icon="@android:drawable/ic_menu_agenda"
android_orderInCategory="100"
android_title="agenda"
app_showAsAction="ifRoom"/>
<item
android_id="@+id/menu_gallery"
android_icon="@android:drawable/ic_menu_gallery"
android_orderInCategory="100"
android_title="gallery"
app_showAsAction="ifRoom"/>
<item
android_id="@+id/menu_calendar"
android_icon="@android:drawable/ic_menu_my_calendar"
android_orderInCategory="100"
android_title="calendar"
app_showAsAction="ifRoom"/>
</menu>

Next:
- Implement checkable items in OptionsMenu of Toolbar

Related Posts by Categories

0 comments:

Post a Comment