injecteer
injecteer

Reputation: 20699

Setting SupportActionBar subtitle multiline

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.

Upvotes: 10

Views: 2254

Answers (3)

Darshan Mistry
Darshan Mistry

Reputation: 3372

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

((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("About");

Upvotes: -2

Michael
Michael

Reputation: 54705

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;
        }
    }
}

Upvotes: 5

thiagolr
thiagolr

Reputation: 7027

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.

Upvotes: 0

Related Questions