Strange issue with android:ellipsize="end" - android-layout

I am using android:ellipsize="end" in android xml file, & surprisingly I am not getting the layout that I want, the 3 dots(...) are showing but after that dots there is another word truncated. Also this is a "not-always" behavior, check the ListView attached, sometimes, the behavior is normal & sometimes not.
Here's the screenshot of the layout from my device,
I don't have any idea why this is happening. Here's my xml file, having problem with the tv_news_content TextView -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/white" >
<ImageView
android:id="#+id/iv_next_tier"
android:layout_width="18dp"
android:layout_height="21dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/right_arrow" >
</ImageView>
<TextView
android:id="#+id/tv_news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_toLeftOf="#+id/iv_next_tier"
android:ellipsize="end"
android:maxLines="2"
android:text="News Title"
android:textColor="#color/black"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_news_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_news_title"
android:layout_below="#+id/tv_news_title"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/iv_next_tier"
android:ellipsize="end"
android:maxLines="2"
android:text="News Contents"
android:textColor="#color/black_light"
android:textSize="15dp" />
<View
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="#+id/tv_news_content"
android:layout_marginTop="5dp" />
To make things clear, tv_news_title is the topmost bold TextView, & iv_next_tier is the small arrow-type ImageView at the right. & tv_news_content is the TextView that I am facing problem with.
Any solution why I am not getting desired output? Desired output means the always normal behaviour - I want those 3 dots at the end of second line of tv_news_content TextView, not before a truncated word.
Any suggestion is appreciated.

Finally found what was causing this issue in my app!!
While RuAware's answer led me on the right path, I found that it didn't fix the problem for all of my text blocks. Given that it worked for some, I felt it was clearly due to some malicious character. So I went through one broken text block and just started removing characters until it stopped breaking.
It turns out it was the new line character '\n' that was causing it. Even if the new line was after the ellipsize, the new line character was [inconsistently] causing this problem as long as it was somewhere within the string.
The issue is solved by doing something similar to the following before setting the text to your view:
text = text.replace('\n',' ');

When setting the text for the textview make sure the text is not greater than 750 characters so use txt = theText.substring(0, 750) or something like that before calling settext. This works on the emulator with your feed. and should be enough characters for a 10inch tablet too

Take a look at this post. There is a custom EllipsizingTextView which solves ellipsizing problem for multiline view. However it may solve you problem too.

I was experiencing the same problem when displaying a String containing html formatted text.
Removing all html tags with myText.replaceAll("\\<.*?>","") solved the problem.
You can also remove html tags using Html.fromHtml(myText).toString(), but notice that you must use .toString() and not apply the fromHtml output directly to the TextView. If some special characters are html encoded (&amp, &gt, &lt...) you can safely apply again Html.fromHtml() to the final String.

i used :
if (myModel.mDataText().length() > 150) {
mTextView.setText(Html.fromHtml(myModel.mDataText().substring(0, 150) + "..."));
} else {
mTextView.setText(Html.fromHtml(myModel.mDataText() + "..."));
}

Related

how to add stroke on text

I can't find a way to put a stroke on text (on the text, not on the whole text view box) , the stroke should be around the letters.
is there any custom text view or library or drawing can be implemented to draw a stroke
<TextView
style="#style/Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:text="HELLO DROID"
/>
i expect the text to look like so , (the stroke color is red)
I tried below two ways for adding stroke to text :
First is add shadow to your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#android:color/holo_blue_light"
android:text="Hello"
android:shadowRadius="10"
android:shadowDx="1"
android:shadowDy="1"
android:shadowColor="#android:color/holo_red_dark"/>
The output of above code is:
Second I found GitHub project android-outline-textview.
Please follow this Link
For this you need to add StrokedTextView and related files to your project
after that add below Code in XML file:
<com.skd.stackdemo.StrokedTextView
android:id="#+id/stroke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="Hello"
android:textColor="#android:color/holo_blue_light"
android:layout_marginTop="30dp"/>
Add below code in Java file:
StrokedTextView stroke = findViewById(R.id.stroke);
stroke.setStrokeColor(Color.RED);
stroke.setStrokeWidth(1f);
The output of above code is:
I hope it works for you.

Android - findViewById is partially working

I have an xml with two siblings view as follows:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="12sp"
android:textColor="#ffffffff"
android:layout_weight="20"
android:gravity="center_horizontal"
android:layout_column="0"
android:layout_columnWeight="45"
android:layout_rowWeight="10"
android:id="#+id/type"
android:text="First\nLow" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="12:16 AM"
android:textSize="20sp"
android:textColor="#ffffffff"
android:layout_weight="40"
android:gravity="center_horizontal"
android:layout_columnWeight="45"
android:layout_rowWeight="10"
android:layout_gravity="center_vertical"
android:id="#+id/time" />
And here is my code:
TextView type_textView = (TextView) inflatedLayout.findViewById(R.id.type);
TextView time_textView = (TextView) inflatedLayout.findViewById(R.id.time);
The object time_textView is working fine. The problem is that type_textView is null!
Both TextViews are in the same layout and they both have the same parent layout (siblings).
This is actually happening with 10 other TextViews while 20 other siblings TextViews are working fine.
I brought only two siblings to make the question straight forward.
I searched and tried all related findViewById solutions:
1. Clean and Rebuild.
2. Delete XML and and add it again.
3. Cache Invalidate and Restart.
4. SetContentView().
5. Fixing all errors in all XML files (I still have tons of silly warnings like must use #dimens and #strings).
6. Make the ID's unique among all XML files.
If it matters, I'm doing all of that inside AsyncTask doInBackground. I tried and moved it to onPostExecute and still the same problem.
Thank you.

align word wrap text in android button

How can I align wraped text in a button ?
this is my button layout :
<Button
android:id="#+id/buttontest"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#color/white_botton"
android:drawableLeft="#drawable/message_button"
android:gravity="left|center_vertical"
android:text="Testing newline"
android:textSize="16sp" />
this is how it looks :
I want the new line to be aligned with the first line.
( If I try with a textview I dont have this issue )
<Button
android:id="#+id/buttontest"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#android:color/white"
android:drawableLeft="#drawable/message_button"
android:drawablePadding="5dp"
android:gravity="left|center_vertical"
android:text="Testing newline"
android:textSize="16sp" />
You can set width according to your need.
My image size isnt perfect using your image will be fine I guess
remove
android:gravity="left|center_vertical"
and Check
hope it will help you
Just add a android:drawablePadding to get some space between the icon and the text.
I guess there is something wrong somewhere else in my code, in a new clean project the alignment works. thank you for all your answers.

TextView text cut off on the left instead of getting ellipsis-shortened

Look at the part of the layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:gravity="center_vertical|right"
android:layout_weight="1">
<TextView
android:id="#+id/second_team_code"
android:layout_marginRight="6dp"
style="#style/TextView.TeamCode"/>
<ImageView
android:id="#+id/second_team_flag"
android:layout_marginRight="1dp"
style="#style/FlagImageView"/>
</LinearLayout>
For some reason, when small size of enclosing views causes text to overflow, text is not shortened by adding "...", but the beginning of the text is truncated. Swap the TextView and ImageView - and the text is ellipsis-shortened as needed.
Why does it work so? And how to fix this?
Style:
<style name="TextView.TeamCode">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">end</item>
</style>
LinearLayout doesn't handle this situation very well... I mean if you have first child like that (with wrap_content) and you want it to stop expanding when it reaches {parent_width}-{next_child_width}. Unfortunately it will stop with width={parent_width}. So, if the text will be bigger, the left bound of view will move outside of it's parent (to the left, but keeping the right bound in place) and the beginning of text will be cut (because view is drawn partially outside od parent left bound). If the text will also not fit the parent width - text will also be cut from the right side, but this time with ellipsize (...)
You can see the results here:
Try something like that:
<TextView
android:id="#+id/second_team_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="6dp"
android:text="Super long text"/>
By applying the weight TextView will occupy all the remaining space (with fixed width) and will not be pushed outside because text won't fit inside. To keep the right alignment you can also add:
android:gravity="right"
android:singleLine="true" is obsolete.
Instead of
android:singleLine="true"
android:ellipsize="end"
you have to use
android:gravity="right"
android:inputType="text"

Android text input - field name shown in field itself

I am trying to achieve a log-in screen like the following:
Where the field's name is displayed in the field itself. I am using the ADT plugin in Eclipse. I tried setting the android:text attribute of the text input, but I don't think this is correct because through the password field, only the dots show (the redacted text), and not the text itself. What is the attribute then. I have looked in the documentation for the EditText widget.
The XML I currently have for the two EditText fields are:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/editText1" android:inputType="textEmailAddress" android:text="#string/username">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/editText2" android:inputType="textPassword" android:text="#string/password" ></EditText>
You should be able to set default values for the fields with android:hint or android:sethint I think its the first.
android:hint="Please enter your password"
its the first one, thanks to a search on SO. Just for additional info it came up with this.
SO Question about android:hint
Share Java code for when click on Submit Button then Show the Name
<EditText
android:id="#+id/name_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Name"
android:inputType="textCapSentences|textAutoCorrect"
/>

Resources