I am working on demo app in which I want to apply click listener on action bar title textview. My below code is working fine below Android 5.0, it finds correctly action bar title textview reference and click listener is also working fine but on android lollipop I am getting null here TextView abTitleTV = (TextView) findViewById(abTitleId);
How to get action bar title reference in android lollipop ?
public class HomeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// find the view of actionbar title
// set the onclick listener so when you click on it
try {
int abTitleId = 0;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
}
else {
// This is the id is from your app's generated R class when ActionBarActivity is used for SupportActionBar
abTitleId = R.id.action_bar_title;
}
TextView abTitleTV = (TextView) findViewById(abTitleId);
abTitleTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HomeActivity.this, TestActivity.class));
}
});
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
abTitleTV.setHeight(TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()));
}
abTitleTV.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics()));
// setting the gravity to center
abTitleTV.setGravity(Gravity.CENTER_VERTICAL);
} catch(Exception e) {
Logger.e(TAG, e.getMessage());
}
}
}