Amit Pal
Amit Pal

Reputation: 11052

How to add new item in action menu bar in Android?

I have following menu_main.xml :

<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="Test">
<item
    android:id="@+id/action_chat"
    android:orderInCategory="200"
    android:title="Search"
    android:icon="@drawable/ic_chat"
    app:showAsAction="ifTest"
    ></item>
<item
    android:id="@+id/action_user"
    android:orderInCategory="300"
    android:title="User"
    android:icon="@drawable/ic_drawer"
    app:showAsAction="ifTest"></item>

</menu>

I can see two icon in App-bar (right aligned). Now I want to add an Icon on the left most place. How can i do that?

Upvotes: 1

Views: 96

Answers (2)

Aditya Chauhan
Aditya Chauhan

Reputation: 271

try it like this.

<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="..."
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="..."
        android:textColor="..."
        android:textSize="..." />

    <ImageView
        android:id="@+id/..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/..."
        android:padding="15dp"
        android:layout_gravity="start"
        android:src="@drawable/..." />

    <ImageView
        android:id="@+id/..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:padding="15dp"
        android:src="@drawable/..." />

</android.support.v7.widget.Toolbar>
....
....
..all other layout files
</RelativeLayout>

now in your mainactivity

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Set a toolbar to replace the action bar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
....

this will be your layout file for mainactivity 1st imageview will be at start

Upvotes: 1

Jin
Jin

Reputation: 6145

To add custom views for ActionBar, use setCustomView and provide it your own layout file or view. This will take over the space where the title of the actionbar would normally go.

Upvotes: 0

Related Questions