Custom Title Bar Without Padding (Android)

Custom title bar without padding (Android)

I was struggling with this same issue and now I have the answer!

As you probably have seen several places, you need to create a themes.xml resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">44dip</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
</resources>

Note it is not often mentioned in the sites talking about custom title bars you need to select this theme at the view, activity, or application level. I do it at the application level by adding android:theme property to the application:

  <application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/MyTheme">

You also need a styles.xml to define that WindowTitleBackgroundStyle. Unlike the various versions of this file I have seen floating aroung the web, I have added one additional line to the file that sets the padding to 0 which gets rid of the padding you are complaining about:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">@android:color/transparent</item>
<item name="android:padding">0px</item>
</style>
</resources>

Android can't remove padding around custom title bar

It turns out I had to modify the background color of the title container itself :)

private void setNavigationAndTitle() {
setTitle("Random Title");
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
LinearLayout root = (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
titleContainer.setBackgroundColor(Color.BLUE);
TextView title = (TextView) titleContainer.getChildAt(0);
title.setTextSize(20);
title.setGravity(Gravity.CENTER);
title.setBackgroundColor(Color.BLUE);
}

Custom titlebar has unwanted black border

For anyone interested.
I got around this by declaring no title bar in the theme and adding the title bar to the main activity; and using a viewpager with fragments. So that the "title bar" is always visible.

New Style file

<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<item name="android:windowTitleSize">35sp</item>
<item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>
<item name="android:buttonStyle">@style/My.Button</item>
</style>

The New activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainView">

<include layout="@layout/titlebar"/>

<android.support.v4.view.ViewPager
android:id="@+id/contentViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

Thanks for the effort guys.

Android: Custom title bar doesn't fit on page

You need to change the height of your titlebar.xml to make sure it gets the amount needed so other Views that this layout is included in doesn't take up room that it needs. So change the height to wrap_content instead of a fixed dp.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- this guy here -->
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:padding="@dimen/small_8dp" >

Unwanted Padding/Margin at the beginning of a custom toolbar

You need to add app:contentInsetStart="0dp" to your Toolbar if you want it to be flush with the starting (left in LTR) edge. There's also contentInsetEnd for the other side.



Related Topics



Leave a reply



Submit