5

I am using CollapsingToolbarLayout:

I am using the following code to show the title:

collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Udupi Sri krishna Temple");

The text is shown as below. It shows only partial and shows .. at the end. Is there a way to control the size so that it shows full text.

enter image description here

Mike
  • 4,550
  • 4
  • 33
  • 47
Santhosh
  • 9,965
  • 20
  • 103
  • 243

1 Answers1

11

First define your text styles in styles.xml

<style name="TextAppearance.MyApp.Title.Collapsed" parent="android:TextAppearance">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">11sp</item>
</style>

<style name="TextAppearance.MyApp.Title.Expanded" parent="android:TextAppearance">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">14sp</item>
</style>

Note that the values are just examples; you need to change them to fit your app. Also, you may want a different TextAppearance style as a parent.

Then in XML :

<android.support.design.widget.CollapsingToolbarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    .
    .
    .
    app:collapsedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Collapsed"
    app:expandedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Expanded"


/>

in code:

collapsingToolbar.setCollapsedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Collapsed);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Expanded);

EDIT: In the comments there is a discussion about multi-line text. CollapsingToolbarLayout does not support multi-line text. Please ignore my suggestion to use setCustomView() on the toolbar. According to docs:

Do not manually add views to the Toolbar at run time. We will add a 'dummy view' to the Toolbar which allows us to work out the available space for the title. This can interfere with any views which you add.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • The text color changes after doing that. how to change the text color to white. Also `?android:attr/textAppearanceMedium` looks too small. can i mention the size of the font manually – Santhosh Dec 09 '15 at 18:53
  • can i have multiple line text in expanded mode, So that i can use bigger font. Also add padding on right (because i may have to add a button on the right), so that it will not overlap – Santhosh Dec 10 '15 at 09:29
  • When i put the code in xml it didnt work. but when i put in the code it worked. Should i be putting in both. – Santhosh Dec 10 '15 at 09:30
  • For multiple line text, check this answer: http://stackoverflow.com/questions/11259311/action-bar-title-with-more-than-one-line You would need to call `setSupportActionBar()` on the Toolbar and then call `setCustomView` on the action bar, using a TextView that you have configured for multiple lines. – kris larson Dec 10 '15 at 15:12
  • The XML by itself should have worked, you shouldn't need to use both XML and code. I find it hard to get the styles to work sometimes; also, it's possible there's a bug in the support library around this. But as long as the code works, you can go with that. – kris larson Dec 10 '15 at 15:15