Search code examples
androidandroid-actionbarappcompatactivityandroid-actionbaractivity

How to remove space in left and right side of Action Bar when used custom layout for action bar?


I'm new to android development and I am designing an app in which I use a custom Action Bar from a layout xml file. I had done with the following code and successfully got the layout into action bar. But the problem I am getting is extra space on left and right side of the Action Bar. enter image description here

I'm using the following codes.

    // java code
public class SimpleQns extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questions);

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar_qns);
  //other codes for app    }

xml layout file with name actionbar_qns.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5"
    android:background="@drawable/actionbar_qns"
    android:padding="0dp"
    android:layout_margin="0dp">

    <Button
        android:id="@+id/play"
        android:layout_width="50dp"
        android:layout_weight="1"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:text="P"
        />
    <TextView
        android:id="@+id/tv_actionbar_questions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Simple Questions"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/stop"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="S" />
</LinearLayout>

Style.xml file

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

</resources>

How can I remove the extra space in Action Bar..?


Solution

  • If you want more customization on ActionBar, Use the ToolBar as an Actionbar which is more customizable than the default ActionBar.