9

Hello I would like to apply a custom Backgroundlayout just for my Main Activity. I couldn't find a solution. Can somebody give me some tips?

would like to have just a simple TextView with a background in the Actionbar. Is this possible?

I managed to remove the icon and set a backgroundimage. But how can I set the gravity of the text to center and change the typefont?

getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(myimage)); 

It should look like this: enter image description here

thanks!

Ahmad
  • 69,608
  • 17
  • 111
  • 137

1 Answers1

16

You have to set a custom layout for the Actionbar like this:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.yourlayout);

In your layout you can then add a TextView and set its gravity to center. E.g.:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Header!"
        android:id="@+id/mytext"
        android:textColor="#ffffff"
        android:textSize="25dp" />

</LinearLayout>

Then you can set your custom typefont inside your Activity from your assets like this:

 TextView txt = (TextView) findViewById(R.id.mytext);  
 Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");  
 txt.setTypeface(font);  
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 3
    If you have any tabs, this goes horribly wrong. The tabs remain, but the title goes BENEATH htem – Adam Apr 15 '13 at 16:04