Dusan
Dusan

Reputation: 3478

Android ActionBar height using a custom layout

I can set my ActionBar height in my styles.xml resource file:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarSize">40dp</item>
</style>

This way, my action bar is a bit smaller than the default one. But a white gap appears below the ActionBar. The content starts at the same position as if the ActionBar had the same height.

enter image description here

EDIT:

My layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainMenuLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top">

    <FrameLayout
        android:id="@+id/mmContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/mmBottomNavigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/mmBottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation_menu"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/btn_nav_itam_color"
        app:itemTextColor="@color/btn_nav_itam_color" />

</RelativeLayout>

EDIT 2: I tried it on a blank Application. The actionBarSize attribute works as expected. Also there's no white gap.

My app uses a custom layout for the action bar - custom_action_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp" android:layout_margin="0dp"
    android:background="@color/colorPrimary" >

    <TextView
        android:id="@+id/titleTvLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/app_name"
        android:textColor="@color/actionBarText"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/titleTvRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="@string/title_bar_loggedin"
        android:textColor="@color/actionBarDarkText"
        android:textSize="12sp"
        android:textStyle="bold" />
</RelativeLayout>

And this layout is set in the Activity:

public static void setCustomTitle(AppCompatActivity apc, String userName) {
    if (apc.getSupportActionBar() != null) {
        apc.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        apc.getSupportActionBar().setDisplayShowCustomEnabled(true);
        apc.getSupportActionBar().setCustomView(R.layout.custom_action_bar);
        View view = apc.getSupportActionBar().getCustomView();
        TextView tvLeft = (TextView) view.findViewById(R.id.titleTvLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.titleTvRight);
        tvLeft.setText(apc.getResources().getString(R.string.app_name));
        String rightTitle = apc.getResources().getString(R.string.title_bar_loggedin) + " " + userName;
        tvRight.setText(rightTitle);
    }
}

Upvotes: 0

Views: 1342

Answers (3)

Skylark
Skylark

Reputation: 113

This is a hack but for the parent container of the content area, you can add a top margin of -8dp.

android:layout_marginTop="-8dp"

This will pull the contents up.

Upvotes: 0

Umair Saeed
Umair Saeed

Reputation: 86

Add standard height in your style or layout, example added for style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:actionBarStyle">@style/CustomActionBar</item>
</style>

 <style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">  
    <item name="android:actionBarSize">40dp</item>
    <item name="android:background">@color/colorPrimary</item>
</style>

Upvotes: 1

shahooo
shahooo

Reputation: 593

Set the parent layout height to "match_parent" in your layout resource file. the resource file of the activity

Upvotes: 0

Related Questions