Gowsik Raja
Gowsik Raja

Reputation: 732

how to reduce the space between icon and home up key in custom action bar in android

I'm using custom action bar in AppCompatActivity. I give the my code below as well as the pitcher. i tried all solution available in stack over flow. But till i can't fix this issue.please assist me.

action_toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
style="@style/toolBarTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:theme="@style/AppTheme.AppBarOverlay">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/appLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tvToolbarTitle"
        style="@style/textViewTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/appLogo"
        android:layout_toEndOf="@+id/appLogo"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp" />
</RelativeLayout>

enter image description here

Upvotes: 1

Views: 1066

Answers (2)

AskNilesh
AskNilesh

Reputation: 69689

you can use toolbar property for this

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"

like this use in toolbar

<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"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp" />

Upvotes: 4

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

if you are using Toolbar then add the following line in Toolbar in xml

app:contentInsetStart="0dp"

Update:

You mean you want toolbar like Whatsapp. but it's not done by using App-compat Support Library Toolbar you have to use Custom Action Bar.

You can get the following result as shown in the Picture below

enter image description here

by using following code.

activity.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_chats"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
            <include layout="@layout/custom_toolbar"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?attr/selectableItemBackgroundBorderless"
android:layout_width="fill_parent"
android:layout_height="?actionBarSize"
>
<!-- android:background="?attr/selectableItemBackgroundBorderless" will cause this Custom View to make ripple effect -->

<LinearLayout
    android:id="@+id/conversation_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/abc_action_bar_up_description"
    android:orientation="horizontal">

        <ImageView
            android:id="@+id/conversation_contact_photo"
            android:layout_width="35.0dip"
            android:layout_height="35.0dip"
            android:src="@drawable/icon"
            android:scaleType="fitCenter" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/conversation_image"
    android:orientation="vertical"
    android:paddingBottom="2.0dip"
    android:paddingLeft="4.0dip"
    android:paddingRight="0.0dip"
    android:paddingTop="0.0dip" >


    <TextView
        android:id="@+id/action_bar_title_1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="6dp"
        android:layout_weight="0.6"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:textSize="18sp"
        android:text="shanraisshan"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/action_bar_title_2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="6dp"
        android:layout_weight="0.4"
        android:ellipsize="end"
        android:text="last seen 1 hour ago"
        android:maxLines="1"
        android:textSize="12sp" />


</LinearLayout>

YourActivity.java

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_chats);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.e("Toolbar","Clicked");
    }
});

Upvotes: 0

Related Questions