chuckice
chuckice

Reputation: 33

How to get customized actionbar?

i'm just looking for a solution in my simple 1-Activty-Project How to get the actionbar in android?

if i use the following, everything works but not layouted:

actionBar = getActionBar();
[..] register viewpager, add tabs dynamically etc [...]

I could add layout parameters in java now..

But, i want to to define an .xml like this for my actionbar by function override onCreateOptionsMenu():

@override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity, menu);
    return super.onCreateOptionsMenu(menu);
}

main_activity.xml:

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_main_activity"
          android:icon="@drawable/logo"
          android:title="@string/app_name"
          android:showAsAction="ifRoom|withText" />
</menu>

If i do this combined i get my actionbar which is doubled... But how to do it the right way? What am I doing wrong..?

Upvotes: 0

Views: 130

Answers (1)

codeMagic
codeMagic

Reputation: 44571

You need to change how it looks by creating a style for it in values/styles like

<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@drawable/bar</item>
    <item name="android:textColor">@drawable/white</item>
</style>

Then create a custom application theme to use the actionbar

 <style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">  // this is telling the app to use your custom themes for the actionbar
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

or you can use.

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(v);

after inflating the layout you want to use. Either way, you still override onCreateOptionsMenu() to get the functionality for your menu

Upvotes: 1

Related Questions