0

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

Community
  • 1
  • 1
Rik van Velzen
  • 1,977
  • 1
  • 19
  • 37
  • Possible duplicate of [Android toolbar center title and custom font](http://stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font) – Emil Oct 20 '15 at 11:25
  • @Boss: Yes I read that post as well, but as explained I managed to solve the issue of centering title in the toolbar of an activity, but my issue here concerns centering the title of the toolbar from within a FRAGMENT – Rik van Velzen Oct 20 '15 at 11:30
  • Simply use another toolbar layout just for that fragment - would that work for you? – yennsarah Oct 20 '15 at 11:39
  • Are you able to access `toolbar` inside `Fragment` ? – Satyen Udeshi Oct 20 '15 at 11:39
  • I've actually created another toolbar layout for the fragment that I include in the fragments xml. In onCreateView() I store a specific reference to the toolbar, that I then can use onActivityCreated() where I call: .setSupportActionBar(mToolbar), .getSupportActionBar().setDisplayShowCustomEnabled(true); & .getSupportActionBar().setDisplayShowTitleEnabled(false) on ((MainActivity) getActivity()) But the activities toolbar is still used this way – Rik van Velzen Oct 20 '15 at 11:52

1 Answers1

0

No Need to create another toolbar for fragment because Toolbar is part pf Actvity and fragment too. there is only one Toolbar should be there

use below method in every fragment or create BaseFragment and call method from onCreate()

protected void setTitleFragment(String strTitle){
    Toolbar mToolbar = (Toolbar) ((AppCompatActivity)getActivity()).findViewById(R.id.toolbar);
    TextView txtTitle =((TextView)mToolbar.findViewById(R.id.toolbar_title));
    txtTitle.setText(strTitle);
}  
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51