Toran Billups
Toran Billups

Reputation: 27399

Android can't remove padding around custom title bar

I'm adding a custom img to the header title but no matter what I do I still have a small gap on each side of the img (also shown in this question)

Here is the xml in my strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello"></string>
    <string name="app_name"></string>
    <style name="LargeTitleTheme" parent="android:Theme.Light">
        <item name="android:windowTitleSize">44dip</item>
    </style>
</resources>

Here is the code in my activity (ignore the slop - desperate coding at this point)

ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
    LinearLayout root = (LinearLayout) decorView.getChildAt(0);
    FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
    TextView title = (TextView) titleContainer.getChildAt(0);
    title.setGravity(Gravity.CENTER);
    Drawable drawable = getResources().getDrawable(R.drawable.nav);
    drawable.setBounds(0,0,0,0);
    title.setBackgroundDrawable(drawable);
    title.setPadding(0,0,0,0);
    title.setIncludeFontPadding(false);

Upvotes: 3

Views: 2829

Answers (4)

Birdnado
Birdnado

Reputation: 156

As of 4.2 and ADT 21 when creating a default layout standard dimensions are added to the parent container:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

</RelativeLayout>

Values are located in res/dimens.xml:

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

</resources>

Either remove them from the parent or change the dimens.xml values to your desired values.

Upvotes: 0

Pawan Maheshwari
Pawan Maheshwari

Reputation: 15366

you can override default padding by applying custom theme like

<style name="customTheme" parent="android:Theme">
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleStyle</item>
</style>

 <style name="WindowTitleStyle">
    <item name="android:padding">0px</item>
</style>

This will remove default padding from custom title bar.

Upvotes: 3

Toran Billups
Toran Billups

Reputation: 27399

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);
}

Upvotes: 2

Johnston
Johnston

Reputation: 2873

you could try making the setPadding values to negitive numbers

Upvotes: -1

Related Questions