Ajay
Ajay

Reputation: 59

Setting custom ActionBar leaving space at left and right side

I want to set a custom ActionBar in my mainActivity. I made custom layout for actionbar named custom_header. And also width is match_parent in custom_layout and also set Layoutparams in Activity is also MATCH_PARENT. But still it is leaving margin from left and right side.

Here is My Main Activity

Main Activity

ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT
    );

    View viewActionBar = ((Activity) MainActivity.this).getLayoutInflater().inflate(R.layout.custom_header,null);
    actionBar.setCustomView(viewActionBar,params);

custom_header.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:background="@color/md_white_1000"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:src="@drawable/logo"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_width="40dp"
    android:layout_height="40dp" />
<TextView
    android:id="@+id/txtHead"
    android:text="DIGIM"
    android:textColor="@color/colorPrimary"
    android:textSize="20sp"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:text="KEY"
    android:textSize="20sp"
    android:textColor="@color/appdefault"
    android:layout_toRightOf="@+id/txtHead"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageButton
    android:src="@drawable/notification_icon"
    android:background="@android:color/transparent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:layout_width="30dp"
    android:layout_height="30dp" />

   </RelativeLayout>

mainactivity.xml

<android.support.constraint.ConstraintLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

   </android.support.constraint.ConstraintLayout>

style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/appdefault</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>


 </resources>

Upvotes: 3

Views: 440

Answers (2)

Anisuzzaman Babla
Anisuzzaman Babla

Reputation: 7470

Just add this

Toolbar toolbar=(Toolbar)viewActionBar.getParent();
toolbar.setContentInsetsAbsolute(0,0);

after below mentioned line

actionBar.setCustomView(viewActionBar,params);

Upvotes: 2

salih kallai
salih kallai

Reputation: 879

Remove android:padding="10dp" from RelativeLayout

Upvotes: 0

Related Questions