Android TextView ellipsize - android

I'm using Android TextView to display the name of the CATALOG.
I added the specific width for TextView outer layout (150dp).
I wants to display "..." when text has more length.
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20dp"
android:singleLine="true"
android:ellipsize="end"
/>
</RelativeLayout>
Catalog Name: Furniture and Books
My result showing:
Furniture an...
But my expectation result is
Furniture and... (or) Furniture...
I wants to show the "..." when there is no space for word in catalog name.

Please check whether this works
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Furniture and Books"
android:textColor="#android:color/holo_blue_dark"
android:textScaleX="1.1"
android:textSize="20dp" />
</RelativeLayout>

You can try this in java code, if it's really necessary.
String str = "Furniture and Books";
str = str.substring(0,8)+"...";
text_view.setText(str);

try to set the EMS for your text view, like :
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="4"
/>
by the way, using "sp" in fonts is a good practice ... any ways
where EMS is the size of characters your textview should hold, in practice, 4 EMS makes the text view holds 8 charachters (you can test it with your devices), and if you want to give it a fixed width based on the numer of charachters, you can use
android:ems="4"
instead

Related

Auto resize Textview

My goal is to create Auto rotate textView.Here is my xml code
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="#+id/u_major_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/u_common_margin_left"
android:maxLines="1"
android:paddingRight="#dimen/u_common_margin_left"
android:singleLine="true"
android:text="0.00"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12dp"
android:autoSizeMaxTextSize="70dp"
android:autoSizeStepGranularity="2dp"
android:textColor="#000000"
android:textSize="70dp"
/>
<TextView
android:id="#+id/u_minor_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/u_major_text"
android:layout_marginLeft="-12dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/u_major_text"
android:layout_toRightOf="#+id/u_major_text"
android:gravity="center"
android:singleLine="true"
android:text="₾"
android:textColor="#000000"
android:textSize="30dp" />
</RelativeLayout>
my goal is to my minor_textview can to be auto resize function.first time text size is 70dp but I want to decrease size depends on a text. What am I doing wrong ?
Apparently, since Android 8.0 and below (thanks to the Support Library) you can use an attribute to auto resize your Textview:
TextView minorText = findViewById(R.id.u_minor_text);
TextViewCompat.setAutoSizeTextTypeWithDefaults(text, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Check this link:
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

How to make TextView in Android wrap and adjust text beautifully?

When text is small then I get this:
But when text is big I get this:
Is it possible that text adjust itself by maybe better using spaces or decreasing little size automatically?
This is my TextView xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/seaGreen"
android:gravity="center_vertical"
android:padding="10dp"
android:textColor="#color/white"
android:textSize="35sp" />
For that you need to use external library for that, else you can set gravity as center
Library :
https://github.com/grantland/android-autofittextview
For Text Justify
https://github.com/bluejamesbond/TextJustify-Android
https://github.com/programingjd/justified
OR
you can check this answer
this work for me:
<TextView
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAlignment="center"
android:gravity="center"
android:padding="5dp"
android:text=""
android:textSize="14sp"/>

Android: Need Ellipsis for Multiple TextViews in Relative Layout

I am new to Android, i have below task.
I want to align three TextViews adjacent to each other with ellipsis if the text is too long . Currently i am using below code which aligns my TextView adjacent to each other, but my problem is if the text is too long, the below code not putting the ellipsis. i got some trick to use both toleftof and torightof from here, but in my case it going to circular dependency as mentioned here.
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/reminderText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagOne"
android:ellipsize="end"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagTwo"
android:ellipsize="end"
android:singleLine="true"
android:layout_toRightOf="#+id/noteTagOne"
android:layout_marginLeft="5sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noteTagThree"
android:layout_toRightOf="#+id/noteTagTwo"
android:ellipsize="end"
android:singleLine="true"
android:layout_marginLeft="5sp"/>
</RelativeLayout>
I want output as,
Tag1... Tag2... Tag3...
Basically, i want some ideas, whether in xml i can achieve this or need to do it programmatically, since TextView text's are dynamic i cannot fix maxLength, sometimes only single tag may exist, at that time it should take full text.
Note: I am developing Android project using C# in Xamarin.
Added code is for the above highlighted part. I fixed overlapping of tagsicon & text. Now only two issues.
1) Last tag is overlapping with right aligned image.
2) If TextView texts are short, gap is shown in-between the tags
How to fix it?
try this solution
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:id="#+id/noteTagOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag111111111111111111"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/noteTagTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_toRightOf="#+id/noteTagOne"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag2222222222222222"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/noteTagThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_toRightOf="#+id/noteTagTwo"
android:ellipsize="end"
android:maxWidth="100dp"
android:singleLine="true"
android:text="Tag3333333333333333"
android:textAppearance="?android:attr/textAppearanceLarge" />
Use this android:ellipsize="marquee" it will work
android:ellipsize="marquee"
or
android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

a layout with ellipsis then a counter

I'm new to Android, please help me to design below layout. Many thanks.
I want to implement a layout, it shows a text followed by a counter. If the text is too long, show ... followed by counter. like below:
Expected layout:
Short text (3) // when screen width is enough
Long text blah bl...(2) // when screen width isn't enough
Below are my xml. It shows normally for above 1, when text is short. When text is long, the problem are:
1) it doesn't show "..." at the end of text
2) counter (2) isn't shown on screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
>
<TextView android:id="#+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="very long body safbadaf asfasf safasfd"
android:layout_gravity="left"
android:paddingLeft="4dp" android:paddingTop="8dp"
android:paddingRight="4dp" android:paddingBottom="8dp"
android:textStyle="bold" android:singleLine="true" android:ellipsize="end"/>
<TextView android:id="#+id/count" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="count"
android:layout_gravity="center_horizontal"
android:paddingLeft="4dp" android:paddingTop="8dp"
android:paddingRight="4dp" android:paddingBottom="8dp"
android:textStyle="bold" android:singleLine="true" android:layout_weight="1" />
</LinearLayout>
William
set android:layout_weight="1" for the first textview and not the second one. Also set layout_width="0" for the first textview instead of wrap_content

Android Textview Italic and wrap_contents

I am using 3 italic textviews with different colors
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:id="#+id/submittedBy" android:paddingTop="10dip">
<ImageView android:id="#+id/subByImg"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left" android:layout_gravity="bottom" android:src="#drawable/submitted_by_arrow"/>
<TextView android:id="#+id/submitLabel"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left" android:text="Submitted by" android:textStyle="italic"
android:textSize="12sp" android:textColor="#color/gray" android:paddingLeft="5dip"/>
<TextView android:id="#+id/submitName" android:textStyle="italic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="12sp" android:textColor="#color/maroon_dark" android:paddingLeft="10dip"/>
<TextView android:id="#+id/submitByDate" android:textStyle="italic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="left"
android:textSize="12sp" android:textColor="#color/gray" android:paddingLeft="10dip"/>
</LinearLayout>
I wonder every last character is not displaying properly specially name displayed in the middle is "Dan Buckland" and it it is missing last character looks like "Dan Bucklano"
Also tell me pls how can have textview italic and bold both..
alt text http://www.freeimagehosting.net/uploads/953d573113.jpg
I had the exact same problem. I got around it by simply adding a space to the end of any string that needs to be in italics.
It may not be the most long-term-correct solution but it worked for me.
Seems like the bounding box is not correctly calculated when using italic.
Have you tried to use paddingLeft=6 and paddingRight=6 for the elements?
(less chance of overlap).
For multiple styles in a TextView see Is it possible to have multiple styles inside a TextView?
You can use   with the text in string.xml file.

Categories

Resources