Setting SupportActionBar subtitle multiline
Asked Answered
C

3

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.

Catania answered 20/7, 2015 at 11:4 Comment(0)
N
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;
        }
    }
}
Nicotinism answered 23/7, 2015 at 15:45 Comment(4)
actually I want to set the text only programmatically, so reflection or alike would doCatania
how exactly can I access the subtitle's textView via reflection? what would be the id of it?Catania
@Catania 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.Nicotinism
Although I ended up with single-lined subtitle with ellipsizing, as 2 lined subtitle looks ugly, but your answer gave valuable tips on the topicCatania
P
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.

Peruke answered 23/7, 2015 at 15:38 Comment(1)
what about support-actionbar?Catania
D
-2

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

((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("About");
Domineering answered 29/7, 2015 at 13:40 Comment(1)
Hi, please read and understand the question, before answering. Thanks.Mucker

© 2022 - 2024 — McMap. All rights reserved.