Vijay
Vijay

Reputation: 156

Custom action bar does not fill parent

Custom action bar does not fill parent.

I am using custom action bar for navigation drawer purpose. But My action bar keep some space left at left side of action bar.

I used code like this.

// to inflate custom view

            LayoutInflater inflator=   (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v=inflator.inflate(R.layout.custom_action_top_bar, null);
            getActionBar().setHomeButtonEnabled(false);
            getActionBar().setDisplayShowTitleEnabled(false);
            getActionBar().setDisplayShowCustomEnabled(true);
            getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            getActionBar().setCustomView(v);

            View v1 = getActionBar().getCustomView();
            LayoutParams lp = (LayoutParams) v1.getLayoutParams();
            lp.width = LayoutParams.FILL_PARENT;
            v1.setLayoutParams(lp);

this is action_custom_action_top_bar file for reference

this xml file contains main linear layout with left and right imageviews for opening two sided drawers respectively. but addition space get occured on left side of the action bar can any one help me out?

this is custom action bar i have used in that some background colour i have given to the action bar and two image for each side for drawers

   <LinearLayout                 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <ImageView
            android:id="@+id/IV_leftIcon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".2"
            android:background="@color/light_green"
            android:src="@drawable/header_menu_btn" />

        <TextView
            android:id="@+id/actionText"
            style="@style/action_bar_top_text_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".6"
            android:background="@color/light_green"
            android:drawableRight="@drawable/header_dropdown_arrow"
            android:gravity="center"
            android:text="Sample"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/IV_rightIcon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".2"
            android:background="@color/light_green"
            android:src="@drawable/plus_btn" />

    </LinearLayout>

Upvotes: 4

Views: 4608

Answers (2)

Md. Sajedul Karim
Md. Sajedul Karim

Reputation: 7075

Just add these 2 line code into your Toolbar xml code

app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"

It will solve your problem.

Here is my working code:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:contentInsetStart="0dp"
    app:contentInsetEnd="0dp">
    <include layout="@layout/your_custom_toolbar_layout"/>
</android.support.v7.widget.Toolbar>

Upvotes: 5

Mina Fawzy
Mina Fawzy

Reputation: 21452

how make action bar fill width

UPDATED ANSWER

remove android:layout_gravity="center" in root linear to be like this , make height wrap_content

<LinearLayout                 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal"
        android:weightSum="1.0" > 

      ......
</LinearLayout>

Prefer use getSupportActionBar() as default activity extends ActionBarActivity

import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.widget.Toolbar;


public class MainActivity extends ActionBarActivity {



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


      getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));



    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    getSupportActionBar().setCustomView(v, layoutParams);
    Toolbar parent = (Toolbar) v.getParent();
    parent.setContentInsetsAbsolute(0, 0);

}
}

Summary

I understand why you have a problem , you using eclipse with old support library that dosenot support ToolBar use appcompat-v21 or later

my advice to you , use Android studio as they stop support eclipse

this code working 100% , prove of concept

enter image description here

Upvotes: 11

Related Questions