Kevin Murvie
Kevin Murvie

Reputation: 2642

Android - Using custom ActionBar

I have a custom ActionBar layout which I want to use in almost all of my Activities.

The problem is the text doesn't appear.

This is how I use it in an Activity

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        //getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.actionbar_theme);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Signup");

But the title "Signup" doesn't appear on the ActionBar..

The theme of the ActionBar

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/actionbar_theme_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:textColor="@color/white" />

</RelativeLayout>

Does this mean that I can't just do setTitle("asdf")?

Upvotes: 0

Views: 101

Answers (2)

silentsudo
silentsudo

Reputation: 6963

As of now you can use toolbar as ActionBar and that toolbar can have any custom layout you wish. Here is the reference link.

Declare this in activity xml

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

Then in java code use

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

for this to work perfectly your Activity should extends AppCompatActivit

Upvotes: 1

Virthuss
Virthuss

Reputation: 3213

This is not how it works. If you set a custom layout to your action bar, you need to change the textview of it like if it was a normal view.

If you are using this layout several time, keep a reference of your action bar or of the viewgroup.

ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.your layout containing your text view,null);
mActionBar.setCustomView(actionBarLayout);

(TextView)findViewById(R.id.actionbar_theme_title).setText(...)

Upvotes: 0

Related Questions