I have a Toolbar that when in landscape mode isn't as wide as usual, but it has more height than usual and for that reason I want to set the title to be multiline (2 lines to be precise) when it is in landscape. I have tried some things where I put a TextView inside of the Toolbar, but when I try to access the TextView to set it's title (since the the title is variable) I get a NullPointer after using findViewById.
Here is some code of the current situation (without multiline):
@Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
}
if (executeOnResume) {
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
// A lot of irrelevant code...
//...
//...
} else { loadStuff();}
}
And here is the toolbar xml:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:title="@string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="@style/toolbarTitleText"
android:background="@color/greyLight"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay" />