4

Below code makes the Title bar to go red and Roboto-Regular font, code work properly but when orientation changes then Title will go to default properties, White color and default font. if i remove config changes in "activity" of manifest then onCreate would be called and font would be Red even after orientation change but i need to have config changes listener.

@Override
protected void onCreate(Bundle savedInstanceState) {

....

  Typeface Roboto_Regular = Typeface.createFromAsset(getAssets(),"fonts/Roboto-Regular.ttf");
  int titleId = getResources().getIdentifier("action_bar_title", "id","android");
  TextView title = (TextView) findViewById(titleId);
  title.setTypeface(Roboto_Regular);
  title.setTextColor(getResources().getColor(android.R.color.holo_red_dark));

}


@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  Typeface Roboto_Regular = Typeface.createFromAsset(getAssets(),"fonts/Roboto-Regular.ttf");
  int titleId = getResources().getIdentifier("action_bar_title", "id","android");
  TextView menuTitle = (TextView) findViewById(titleId);
  menuTitle.setTypeface(Roboto_Regular);
  menuTitle.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
  invalidateOptionsMenu();
}

And in manifest

<activity
android:name="com.example.fontexperiment.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name" >

------------- updated with more info

android source code

if we read above link, it can be found that android:id="@+id/action_bar_title" is what android has in layout file for title. I checked in layout-land, there is not separate layout used for case of landscape. So the id of action bar title is same.

in my code i do get a null exception when i do

getResources().getIdentifier("action_bar_title", "id","android")

so the action bar title is proper, but the effect is not happening.

Does onConfigurationChanged() gets called after at end or very beginning? if at beginning then android might be setting title to defaults.

harshal
  • 592
  • 6
  • 25

1 Answers1

0

If you need to maintain the same action bar title color on orientation change , I would suggest to create a Custom Theme with your own text color and set to the Activity in manifest.

Check this answer on Changing ActionBar Title Text Color

Community
  • 1
  • 1
Libin
  • 16,967
  • 7
  • 61
  • 83
  • changing typeface is important one the purpose of changing color is that it lets you know that typeface change part of code got executed, i don't have to be debug mode for all devices. – harshal May 01 '14 at 05:02
  • You can still change the font with custom text style – Libin May 02 '14 at 01:53