Savita
Savita

Reputation: 747

How to get custom view for Toolbar

I have created a custom view for my Toolbar for fargments where I have added to two ImageButtons to the view and I have removed the those items from the menu resource.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:id="@+id/tool_bar"
    android:background="@color/white">

    <ImageButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/video_camera"
        android:icon="@drawable/camcorder_pro_96px"
        android:layout_gravity="left|center_horizontal" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/photo_camera"
        android:icon="@drawable/compact_camera_96px"
        android:layout_gravity="right|center_horizontal"
        />

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

I have attached this view to Toolbar in my MainActivity

toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

Screenshot of Toolbar

Upvotes: 1

Views: 300

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

android:icon

It is displayed to users when a representation of the activity is required on-screen.

In that you should use android:src instead of android:icon .

android:src

Sets a drawable as the content of this ImageView or ImageButton.

Upvotes: 2

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

The problem is that you are setting the image on the wrong attribute in the Imagebutton and I dont think that android:icon exist either.

You need to set the image to the android:src attribute to set the image to the view

Upvotes: 1

Related Questions