3

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);
Community
  • 1
  • 1
Toran Billups
  • 27,111
  • 40
  • 155
  • 268

4 Answers4

3

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.

Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
2

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);
}
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
  • damn thanks I am actually discovering that most internet info on android pretty much sucks – daniel Jul 30 '11 at 02:36
  • Just to be clear - this won't work on Android Tablets (the hard cast to FrameLayout blows up) - I'm yet to figure out what this _should_ be and I'll update the post if/when I get this working on tablet android devices – Toran Billups Nov 22 '11 at 01:35
0

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.

Birdnado
  • 156
  • 1
  • 7
-1

you could try making the setPadding values to negitive numbers

Johnston
  • 2,873
  • 8
  • 29
  • 39