0

I'm using Google support-v7 to get ActionBar support on Android 2.3+, here's my code

public abstract class NavUpActionBarActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.title_back);
        actionBar.setDisplayOptions(
            ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP);
        actionBar.setTitle(getCustomTitleResId());
    }

    @Override
    public boolean onSupportNavigateUp () {
        this.finish();
        return true;
    }

    protected abstract int getCustomTitleResId();
}

All subclass of NavUpActionBarActivity never showed any title on 2.3.3, any idea?

UPDATED1

The above code works on every device 4.x

UPDATED2

this is a known issue for google

fifth
  • 4,249
  • 9
  • 45
  • 62

2 Answers2

0

Instead of

actionBar.setTitle(getCustomTitleResId());

You should do something like this:

ActionBar ab = getSupportActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");

If you want to get the String of the title from your resources, then you should have something like this

{
    /* ... */
    ActionBar ab = getSupportActionBar();
    ab.setTitle(getCustomTitleResId());
    ab.setSubtitle("sub-title");
    /* ... */
}

protected String getCustomTitleResId() {
    return getString(R.string.CustomTitle);
}

Remember that if you want this method to work, you should define it into your Activity, or pass to it the Activity Context:

 protected String getCustomTitleResId(Context c) {
    return c.getString(R.string.CustomTitle);
}
Gian Segato
  • 2,359
  • 3
  • 29
  • 37
  • tried setTitle(String), I tried 2.3.3 and another 2.3.4, both didn't work. In my code, the resource was in the same Context (the activity of subclass). NavUpActionBarActivity is an abstract class – fifth Jul 28 '14 at 01:51
0

try this ..

actionBar.setDisplayShowTitleEnabled(true);

Also if you need to create customized Action Bar then follow my post over here .. Android Action Bar: Can I replace a custom Title in appcompat v7

Update

to set title try this ...

   actionBar.setTitle(Html.fromHtml("<font color='#013145'>Sign in or Create an account</font>"));

Hope it helps ..

UPDATE

I already faced this problem .. that's why I am suggesting you to create custom view for Action bar. There's no surety for UI look and feel as you are trying ..

For your concern if you still need to go this way: For now as you are trying .. you need to get ID of title bar for lower version and then you need to update that title .. For Ex: you firstly need to have check for lower and higher version .. In higher version you simply need to define as I have mentioned above .. and for lower version you need to get ID of title Texview and then you need to set Title over it.

You can get ID of title like this ..

Grab the ID for the action_bar_title

int titleId = getResources().getIdentifier("action_bar_title", "id", "android");

Now you can use the ID with a TextView

TextView abTitle = (TextView) findViewById(titleId);
abTitle.setText("You title text");

But over all what I found that using Custom view is much more better way of accomplishing this task .. as this will allow you to make much more complex UI in very easy and handy way.

Hope you will go for this option.

For Lower and higher version check .. you may try like this ..

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

                    // Higher version

                } else {

                    // Lower version 

                }

Hope it sounds great .. Cheers!

Community
  • 1
  • 1
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
  • Can you please put what problem you faced after this so that I can help you on this .. – AndroidHacker Jul 28 '14 at 06:07
  • hi, I don't need to create custom actionbar, but just set a title. I tried your solution and mine, but the title never showed up, that's the problem. you can get my code snippet above and my updates, now it looks more like a known bug – fifth Jul 28 '14 at 07:19
  • Have you set this property .. actionBar.setDisplayShowTitleEnabled(true); – AndroidHacker Jul 28 '14 at 09:32
  • sure, set title display enabled – fifth Jul 29 '14 at 01:24
  • Bro this is exact way of implementing your requirement. Please check OR post your complete code so that we can check for error in it. Might be you are doing some thing wrong in it. – AndroidHacker Jul 30 '14 at 06:20
  • @AnroidHacker, I just found the problem that the title didn't get displayed has sth to do with my overridden ActionBar style, I'm trying to figure out why and will update – fifth Jul 31 '14 at 01:16
  • thanks for your patient explanation. I understood what you talked about. At last I can create custom view to avoid some compatibility issue. If you still have interests, you can play around my demo at https://github.com/luoqpolyvi/ActionBarTitle. Now I started trying custom view – fifth Jul 31 '14 at 05:53
  • That's great .. good luck! .. Let me know if you face any sort of problem :) – AndroidHacker Jul 31 '14 at 05:59
  • I've basically created my custom actionbar view, looks great except the height of actionbar. I didn't config anything about actionbar height, but it looked too high in 2.3.3. I checked out the original style values of ActionBar, which height is 48dp, this caused it's too high. How could I unify the height appearance across 2.3.x to 4.4.x? – fifth Jul 31 '14 at 08:13