StuartDTO
StuartDTO

Reputation: 1031

Add icons Toolbar and TextView

I'm trying to add 4 icons on my Toolbar and a TextView that I want to act as a Title this is my xml

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <ImageView
            android:layout_width="@dimen/_40sdp"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignBottom="@+id/toolbar"
            android:id="@+id/imageView" />
        <ImageView
            android:layout_width="@dimen/_40sdp"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageView"
            android:layout_toStartOf="@+id/imageView"
            android:layout_alignBottom="@+id/toolbar" />        
        <ImageView
            android:layout_width="@dimen/_40sdp"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/imageView3"
            android:layout_alignBottom="@+id/toolbar" />
        <ImageView
            android:layout_width="@dimen/_40sdp"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/imageView2"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageView3"
            android:layout_toStartOf="@+id/imageView3"
            android:layout_alignBottom="@+id/toolbar" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvToolbar"
            android:text="Title"
            android:textSize="@dimen/_17sdp"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </android.support.v7.widget.Toolbar>

    </RelativeLayout>

But the output isn't what I'm expecting, I'd like to get a Toolbar like this, but it shows like this.

And when I launch the application it appears a ToolbatTitle that I think it's the name of the APP how do I quit this and use the TextView one?

Upvotes: 0

Views: 695

Answers (1)

mcp
mcp

Reputation: 2066

Take a look at the official design guidelines. You should not have icons to the left of the title other than the up action.

Instead of inserting the ImageView's into the activity's xml create an xml menu resource and implement it by overriding onCreateOptionsMenu() in your activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_resource_you_created, menu);
    return true;
}

Take a look at this and more specifically this for more info.


EDIT: If you really want to do this regardless of the guidelines I would probably just put a LinearLayout or similar in the xml, make sure it's at the top of the parent and set these attributes:

android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" 

Again, I do not recommend this nor can I guarantee it as a legitimate workaround, but I'm pretty sure it's what you're looking for.

Upvotes: 2

Related Questions