10

I have the following setup:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:subtitleTextAppearance="@style/TitleTextStyle"
        app:theme="@style/ActionBarStyle.Light"/>

and

<style name="TitleTextStyle">
    <item name="android:maxLines">2</item>
    <item name="android:textAllCaps">false</item>
</style>

and

getSupportActionBar().setSubtitle( targetString );

the @textAllCaps is applied, but @maxLines is ignored (I tried also `@minLines).

How to enable multiline text in subtitle?

TIA

UPDATE:

I added the code into the onStart:

@Override
public void onStart() {
    super.onStart();
    try{
      Field field = Toolbar.class.getDeclaredField( "mSubtitleTextView" );
      field.setAccessible( true );
      subtitleTextView = (TextView)field.get( toolbar );
      subtitleTextView.setSingleLine( false );
      subtitleTextView.setMaxLines( 2 );
      subtitleTextView.setEllipsize( TruncateAt.END );
    }catch( Exception e ) {
      Logg.e( this, "", e );
    }
}

the problem is now, that the ellipsize values are applied, but setMaxLines() call is ignored...

UPD2:

setSingleLine( false ) did the trick.

injecteer
  • 20,038
  • 4
  • 45
  • 89

3 Answers3

5

Unfortunately, maxLines is not a part of TextAppearance so changing it will not affect the subtitle appearance. Moreover, there's no legal way to access Toolbar.mSubtitleTextView, but after setting a subtitle text you can access it via reflection and change its appearance as you wish.

UPDATE:

That's how you can access mSubtitleTextView via reflection.

public class SubtitleAccessor {
    private static final Field sSubtitleTextViewField = getSubtitleTextViewField();

    private static Field getSubtitleTextViewField() {
        try {
            Field field = Toolbar.class.getDeclaredField("mSubtitleTextView");
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException exception) {
            return null;
        }
    }

    public static TextView getSubtitleTextView(Toolbar toolbar) {
        if (sSubtitleTextViewField == null) {
            return null;
        }

        try {
            return (TextView) sSubtitleTextViewField.get(toolbar);
        } catch (IllegalAccessException exception) {
            return null;
        }
    }
}
Michael
  • 53,859
  • 22
  • 133
  • 139
  • actually I want to set the text only programmatically, so reflection or alike would do – injecteer Jul 23 '15 at 15:48
  • how exactly can I access the subtitle's `textView` via reflection? what would be the id of it? – injecteer Jul 28 '15 at 12:02
  • @injecteer I added a code sample to the answer. You cannot find `mSubtitleTextView` by id because it's created at runtime and doesn't have and id. So the best thing you can do besides rethinking your UI, or writing your own `Toolbar` class, or using a custom view instead of title and subtitle is accessing `Toolbar`'s private fields directly via reflection. – Michael Jul 28 '15 at 12:29
  • 1
    Although I ended up with single-lined subtitle with ellipsizing, as 2 lined subtitle looks ugly, but your answer gave valuable tips on the topic – injecteer Jul 30 '15 at 09:20
0

Please try to add:

<item name="android:singleLine">false</item>

If it doesn't work, you may try to use a custom view:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.your_layout_file);

Where R.layout.your_layout_file is your custom layout designed the way you want it.

thiagolr
  • 6,909
  • 6
  • 44
  • 64
-2

To use the Toolbar and the Appcompat 21, you have to use an ActionBarActivity and use:

((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("About");
Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29