2

I am using a custom toolbar in my app. Toolbar adds extra space at its left side. This is my Xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/customActionBar"
    android:layout_height="wrap_content"
    android:background="@color/titleBar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/leftIcon"
        android:src="@drawable/abc_btn_rating_star_on_mtrl_alpha"
        android:background="@android:color/transparent"
        android:layout_weight="0.2"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/actionTitleText"
        android:text="@string/app_name"
        android:textColor="@color/abc_primary_text_disable_only_material_dark"
        android:textSize="@dimen/text_22sp"
        android:padding="@dimen/size_12dip"
        android:gravity="center_horizontal"
        android:layout_weight="0.6"/>

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/rightIcon"
        android:src="@drawable/abc_btn_rating_star_on_mtrl_alpha"
        android:background="@android:color/transparent"
        android:layout_weight="0.2"/>
</LinearLayout>


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

Can you please help? it gives some margin from left of screen, but i have not given any margin attribute. This is code to inflate that into activity:

 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.activity_main);
        baseViewContent = (LinearLayout) findViewById(R.id.main_activity_content_view);
        viewController = new ViewController(this, baseViewContent);
        activity = this;

        // Set a toolbar to replace the action bar.
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.customActionBar);
        setSupportActionBar(toolbar);
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
Yogi
  • 59
  • 1
  • 8
  • Show us how you inflate that layout, and post the layout in which you inflate this (if any, for example the layout of the activity in which you are placing the toolbar). – lorenzo-s Aug 06 '15 at 07:24
  • @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); baseViewContent = (LinearLayout) findViewById(R.id.main_activity_content_view); viewController = new ViewController(this, baseViewContent); activity = this; // Set a toolbar to replace the action bar. android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.customActionBar); setSupportActionBar(toolbar); – Yogi Aug 06 '15 at 07:26
  • So, post the content of `activity_main.xml` layout file. – lorenzo-s Aug 06 '15 at 07:27
  • This is main Activity XML: – Yogi Aug 06 '15 at 07:30
  • Post your parent layout!. – capt.swag Aug 06 '15 at 07:55
  • Possible duplicated http://stackoverflow.com/q/26455027/1384010 – Adarsh Yadav Aug 06 '15 at 07:59

3 Answers3

12

Use

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

to remove the extra space.

Ref: https://stackoverflow.com/a/27309500

Community
  • 1
  • 1
Kedar Tendolkar
  • 474
  • 3
  • 9
0

The solution is

toolbar.setContentInsetsAbsolute(0,0);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

For other people like me: It was the title in the toolbar that I was not seeing because it had the same color as background. You can remove it if you want with getSupportActionBar().setDisplayShowTitleEnabled(false)

navid
  • 51
  • 1
  • 4