I am writing an android app in which I am trying to change the color of the title of Action Bar by clicking on Button. As soon as, I click the button , the title gets hidden though I am passing the color code of YELLOW. Here is my Code of MainActitvity.java
Button B;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B = (Button) findViewById(R.id.button1);
B.setOnClickListener(this);
}
public void onClick(View v) {
android.app.ActionBar actionBar = getActionBar();
Internal.setActionBarTitleColor(actionBar, 256);
}
Internal.java
public static void setActionBarTitleColor(android.app.ActionBar actionBar, int titleColor) {
if(actionBar == null)
return;
try {
Field actionViewField = actionBar.getClass().getDeclaredField("mActionView");
actionViewField.setAccessible(true);
Object actionView = actionViewField.get(actionBar);
if(actionView == null)
return;
Field titleTextField = actionView.getClass().getDeclaredField("mTitleView");
titleTextField.setAccessible(true);
TextView titleText = (TextView)titleTextField.get(actionView);
if(titleText != null)
titleText.setTextColor(titleColor);
} catch (Exception e) {
e.printStackTrace();
}
}