Ellipsize within word possible? - android

I have a list of items where the words may be longer then the width of the textView. I would like the text to be ellipsized.
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="* SomeVeryLongWordWhichIsLongerThanWidth" />
The problem is, that the output is *... instead of * SomeVeryLongWordWhi.... How can I achieve the latter?

You could use \u00A0 instead of normal space sign
\u00A0 its non-breakable space
android:text="*\u00A0SomeVeryLongWordWhichIsLongerThanWidth"

Related

The last character of Button's text is partially visible

I have a FrameLayout which contains an EditText and a Button.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/edittext_hint"
android:inputType="textPassword"
android:maxLength="25" />
<Button
android:id="#+id/forgottenPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#null"
android:fontFamily="#font/sf_pro_display_lightitalic"
android:padding="16dp"
android:marginRight="16dp" // still, it gets cropped
android:text="#string/password_button"
android:textSize="18sp" />
</FrameLayout>
I gave padding=16dp to the Button but somehow the question mark is partially visible. If I make the phrase short like "Forgot it?" then the '?' mark fully visible. I didn't understand. I gave it even marginRight=16dp, still...
But when I don't set fontFamily attribute, again it is fully visible.
The confusion I have is that how come the last character gets cropped even though it has padding and marginRight. I even tried adding space after the question mark but nothing changed. Am I missing something?
Edit: I am actually using strings.xml for the button's text. When I add space after the '?' in the strings.xml it doesn't effect the text in the button, however, when text is hard coded and added space after the '?', question mark is fully visible again. But as it is said over and over Hardcoded string is bad.
I can't make my phrase short like "Forgot it?", because the app is in Turkish and the original phrase is relatively long. And also I have to use that font.
That is because your font is italic. If you want to insert a space character in XML file use   after ? like this : ? .
That's because of the padding on your button. Remove the padding and try it out.
If all you want is some clickable text, there's no need for a Button:
<TextView
android:id="#+id/forgottenPassword"
android:clickable="true"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:fontFamily="#font/sf_pro_display_lightitalic"
android:padding="16dp"
android:marginRight="16dp"
android:text="#string/password_button"
android:textSize="18sp" />
Notice that I added clickable and focusable.

In edittext hint need to scroll horizontally

In my app i have Edit text with fixed size but hint size is more than edit text so hint is not displayed fully.I want to display hint fully by scrolling horizontally but i don't have any idea how to do this.
If reduce the size of hint font it will help but i don't want to reduce font size.
Following is the example code which i use.
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:id="#+id/test_etx"
android:hint="#string/hint"
android:layout_below="#+id/bt3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/etx"
android:layout_alignEnd="#+id/etx"
android:maxLength="10"
/>
Make your EditText width match_parent. Put that inside a horizontal scroll view. I tried and it works. If you want fixed width, give fixed width to your horizontal scroll view. Like this :
<HorizontalScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bt3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/etx"
android:layout_alignEnd="#+id/etx">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:id="#+id/test_etx"
android:hint="#string/hint"
android:maxLength="10"/>
</HorizontalScrollView>
First you can add a scroll in editText then put text in edit Text with grey font color instead of hint and then put a OnClickListener on the editText. If input text matches hint then set it to "" else process the data.
Make two arrays of EditText id and its corresponding hint in values res. Then fetch both arrays using getResource().
Use
String str = view.getResources().getResourceName(id);
str = str.substring(str.lastIndexOf(":id/") + 4);
to get view id.
match hint from stored array's hint to input text. I think it will be generalised for all the layouts. Hope it helped.

Android: Cut off to long multiline text in TextView

I have a TextView with a height depending on previous content, but there might be a long Text in it. How can I cut it off at the right point and concatenate three dots or something similar to the new end?
Currently it looks like this:
I found some solutions for Single Line Text, but how does it work with more than one line? I also do not know the number of lines, because this depends on the screen size.
Are there other typical ways on Android to show that the text can be extended? E.g. a colour gradient in the last line?
Edit:
When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:
<TextView
android:id="#+id/podcastShortDesc"
android:text="Long text"
android:layout_below="#+id/podcastTitle"
android:layout_toRightOf="#+id/podcastLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:layout_above="#+id/podcastMoreAction" />
When I do specify maxLines I can have luck an it will work:
But if the title is too big, it does not work:
You should add following code for "3 dots" at the end.
android:ellipsize="end"
You should remove fixed height property
android:layout_height="50dip"
instead you should add number of lines
android:maxLines="4"
android:layout_height="wrap_content"
Android will take care everything else. In this way, even if text is smaller than 4 lines, android will take care size. If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.
Try this. Hope it will work.
<TextView
android:id="#+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>
I have tested 50dp can show two line in normal font size. So in 50dp height you should add maxLines 2.
Try this fix layout_height and add scroll in your textview
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>

How to limit lines on a TextView

I'm trying to limit a TextView to 1 line, the length of the screen. I'm having trouble keeping it from expanding.
I've tried adding the
android:minLines="1"
android:maxLines="1"
attributes to the TextView xml. This does keep the view from expanding, but this does not limit the characters of the text. For example, if I have entered the maximum number of characters in a line and proceed to add a '(' to the string, I can see the top of it printed below the first line.
Basically, I need a way to limit the amount of characters to the width of the screen. I've tried this:
while(txt.getLineCount() > 1) {
txt.setText(txt.getText().subSequence(0,txt.getText().length()-1));
}
which doesn't work at all. I've also tried that same method with comparing the txt.getHeight() to the txt.getTextSize().
I'm out of ideas. What can I do?
If I use a textview this way it will ocupy only 1 line:
<TextView
android:id="#+id/textView"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Try this. This will limit textview to single line. , if the text was more than one line it will display ... at end.
<TextView
android:id="#+id/textView"
android:singleline="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Why does TextView in single line elipsized with "end" show boxes?

I'm using a TextView in Android, what I want to show 1 line in TextView ending with ". " but this give [] type box at the end. I don't know why? I just want to remvoe this box and only to show text ending with "... "
Update code for the list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="85dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:cacheColorHint="#4C7B8D"
android:background="#4C7B8D">
<ImageView
android:id="#+id/videoListImage"
android:src="#drawable/audio_thumbnail60x60"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:layout_width="75dp"
android:padding="4dp"
android:background="#color/light_gray" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/next_arrow"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft = "5dp">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#color/app_background_color"
android:textSize="18sp"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:id="#+id/row_dis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#color/color_black"
android:layout_marginRight="2dp"
android:textSize="15sp"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:text="$7.50"
android:id="#+id/audio_price_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#color/color_white"
android:textStyle = "bold"
android:paddingLeft = "12dp"
android:paddingRight = "12dp"
android:background="#drawable/blue_round_cornor_background" />
</LinearLayout>
<ImageView
android:id="#+id/next_arrow"
android:src="#drawable/next_arrow"
android:layout_toLeftOf="#+id/saved_purchased"
android:scaleType="fitXY"
android:layout_height="25dp"
android:layout_width="18dp"
android:layout_centerVertical="true"
android:visibility = "gone"/>
<ImageView
android:id="#+id/saved_purchased"
android:layout_alignParentRight="true"
android:layout_alignParentTop ="true"
android:scaleType="fitXY"
android:layout_height="25dp"
android:layout_width="25dp"
android:layout_marginRight="2dp"/>
</RelativeLayout>
Here is the images of "next_arrow"
Here is the code I am using the getView() in adapter.
String discription = listData.getDescription();
if (discription != null && discription.length() > 0) {
if (textViewDis != null) {
textViewDis.setTypeface(titleFont);
Log.e("data", ""+discription);
discription.replaceAll("\r\n", "");
textViewDis.setText(discription);
}
}
Here is the actual String of description to be display.
Andrew and Stephanie Tidwell candidly share their success story in this business. This story will help everyone listening realize that no one is perfect, even in a second generation business. This is a streaming audio file.
Still have some issue? I can update question more.
Quoting myself from one of my books:
Android's TextView class has the built-in ability to "ellipsize" text,
truncating it and adding an ellipsis if the text is longer than the available
space. You can use this via the android:ellipsize attribute, for example.
This works fairly well, at least for single-line text.
The ellipsis that Android uses is not three periods. Rather it uses an actual
ellipsis character, where the three dots are contained in a single glyph.
Hence, any font that you use that you also use the "ellipsizing" feature will
need the ellipsis glyph.
Beyond that, though, Android pads out the string that gets rendered on-screen, such that the length (in characters) is the same before and after
"ellipsizing". To make this work, Android replaces one character with the
ellipsis, and replaces all other removed characters with the Unicode
character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). This means the
"extra" characters after the ellipsis do not take up any visible space on
screen, yet they can be part of the string.
However, this means any custom fonts you use for TextView widgets that
you use with android:ellipsize must also support this special Unicode
character. Not all fonts do, and you will get artifacts in the on-screen
representation of your shortened strings if your font lacks this character
(e.g., rogue X's appear at the end of the line).
I have bumped into the same problem when was trying to use custom "MetaPro-Medium.otf" as font for TextView with a
android:singleLine="true".
The box at the end of the string was really annoying.
I found no way how to solve this problem in Android, but at the same time found following work around.
I have installed "FontLab Studio v5.04"
Opened my font
Selected one of symbols I was not going to use
Menu->Glyph->Rename Glyph
Changed the name and unicode index from it`s original value to "FEFF" (Thank CommonsWare)
Double click on selected symbol and then remove all lines this symbol was created from.
Menu->File->Generate Font->save as type otf
As a result I got updated font and problem gone away.
Arslan, your layout is working fine in my case, I have tested the same with:
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="18sp"
android:ellipsize="end"
android:maxLines="1"
android:text="This is the demo testing demo testing This is the demo testing demo testing"/>
... and getting the exact output as you want "one line ended with ..." and box should be removed. So I think there may be a something wrong with any character or text you are setting.
I face the same issue (show boxes([])) for some special symbols when I try to show content which is coming from webservice url even, I don't use android:ellipsize. then I replace code from
textview.settext(content);
to
textview.settext(Html.fromHtml(content));
working fine.
Actually I was running into this issue and instead of changing the font or using setText I just added scrollHorizontally as false and it fixed the extra box character
android:singleLine="true"
android:scrollHorizontally="false"
android:ellipsize="end"
There was a similar problem which i was facing in my project where in i was using a font type for the text view. few font types have these problem of making the ... appear as [] at the last. The problem would be solved if u try changing the font.

Categories

Resources