Home > Net >  How to center the toolbar's subtitle under title
How to center the toolbar's subtitle under title

Time:02-10

I am wondering how it's possible to center a toolbar's title and subtitle programatically in android. I use a custom toolbar with custom margins and setting the gravity there only results in both title and subtitle being side by side like this:

enter image description here

Since the subtitle is optional, I am trying to avoid using linear layout. Is there any other way to set the subtitle underneath the title in the middle?

This is the part where I set it in the else part of a condition (only if there is a subtitle):

 else {
            Toolbar.LayoutParams lpT = new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            Toolbar.LayoutParams lpS = new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            verticalMargin = (int) (0 * factor);

            lpT.setMargins(margin, verticalMargin, margin, verticalMargin);
            lpT.gravity = Gravity.CENTER;
            centeredTitleTextView.setLayoutParams(lpT);

            lpS.setMargins(margin, verticalMargin, margin, verticalMargin);
            lpS.gravity = Gravity.CENTER;
            centeredSubTitleTextView.setLayoutParams(lpS);
        }

This is what I am trying to achieve: enter image description here

CodePudding user response:

  •  Tags:  
  • Related