For a specific design I want to have Toolbar title text centered.
I haven't been able to get it working from within a fragment, although I have managed to do this in an activity by following these instructions where I create a custom toolbar widget with 2 textviews for the toolbar and set this as the SupportActionBar adding the toolbar's xml as an include in the activity's layout xml. Something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarCouponDetailed);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowCustomEnabled(true);
TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_coupons_title);
TextView toolbarTitleDate = (TextView) findViewById(R.id.toolbar_coupons_title_date_text);
toolbarTitle.setText(StringUtils.onlyFirstLetterUpperCase(mCoupon.getRetailerName()));
toolbarTitleDate.setText(mCoupon.getDateTxt());
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
I tried to use the same approach but somehow I just can seem to get it working. Does anyone have an idea or a code sample solving this issue?
Edit: I've actually created another toolbar layout for the fragment that I include in the fragments xml. In onCreateView() I've created a member variable to create a reference to the toolbar, that I then can use onActivityCreated() where I call:
((MainActivity) getActivity()).setSupportActionBar(mToolbar);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
But the activities toolbar is still used this way