Josh Parinussa
Josh Parinussa

Reputation: 643

How to detect the title is too long in action bar Android?

enter image description here

I have a project, the problem is the title need 2 line but i want the title just 1 line like this

enter image description here

Am i have to calculate the character ?

Upvotes: 4

Views: 2478

Answers (3)

John Joe
John Joe

Reputation: 12803

  tv.setSingleLine();
  tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);

Source: How to limit a length of a text in android ListView?

Upvotes: 2

HaroldSer
HaroldSer

Reputation: 2065

Add these fields to the textview on your xml layout:

android:scrollHorizontally="true"
android:ellipsize="end" 
android:maxLines="1"

Upvotes: 2

Suhayl SH
Suhayl SH

Reputation: 1223

Method 1: Set singleLine property to true.

android:maxLines="1"
android:ellipsize="end"

Also, if you want to fit the entire title in one line.

Try setting font size as per screen density:

Create resource folders like:

values-ldpi
values-mdpi
values-hdpi

Create dimensions.xml file in each folder with appropriate text size:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_size">15sp</dimen>
</resources>

Set the text in xml or in java file as:

textView.setTextSize(getResources().getDimension(R.dimen.text_size));

Upvotes: -1

Related Questions