I've created a series of tabs that have a fixed width of 100px. A tab contains an image with some text below it. If the text is too long to fit, I want it to automatically scroll. I only want one line. I tried the following but it did not work. I am using Android 2.3:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#ff737373"
android:gravity="center"
android:minWidth="64dp"
android:orientation="vertical"
android:padding="3dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tabImage" >
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:singleLine="true"
android:maxWidth="100px"
android:tag="tabCaption"
android:textColor="#ffd9d9d9"
android:textSize="16sp" />
</LinearLayout>
Any idea why this does not work? I came across the solution from another posting and there the user indicated that it works.
In your activity you have to add if you want to marquee on text
TextView tv=(TextView)findViewById(R.id.textview1);
tv.setSelected(true);
You might want to try this:
https://github.com/kaeppler/ignition/blob/master/ignition-core/ignition-core-lib/src/com/github/ignition/core/widgets/ScrollingTextView.java
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:text="Auto text scroller"
android:textSize="50sp"
/>
Be aware that if the text is smaller in length than the width of the bounds defined for it it WILL NOT scroll. In this case you may want to extend the text with itself something like:
-. . . . .
String charsInBreak = " ";
while (bounds.width() < this.m_width)
{
m_text = (m_text + charsInBreak + m_text);
paint.getTextBounds(m_text, 0, m_text.length(), bounds);
}
And if you want your text to marquee forever:
m_textView.setMarqueeRepeatLimit(-1);
Related
Can someone help me get this text centered please,
I have tried changing the layout width, height and padding but only managed to make it worse,
<com.github.mmin18.widget.FlexLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/login_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_main"
app:layout_centerX="50%"
app:layout_centerY="30%" />
<EditText
android:textSize="18.0sp"
android:textColorHint="#ff4d4d4d"
android:gravity="center"
android:id="#id/edit_code"
android:focusable="true"
android:visibility="gone"
android:layout_width="341.32812dip"
android:layout_height="46.0dip"
android:hint="#string/active_code"
android:layout_centerHorizontal="true"
android:inputType="text"
app:layout_centerX="50%"
app:layout_top="logo.bottom + parent.height/20" />
<Button
android:textSize="18.0sp"
android:id="#id/btn_reset"
android:background="#drawable/btn_bg"
android:focusable="true"
android:visibility="gone"
android:layout_width="160.0dip"
android:layout_height="36.0dip"
android:text="#string/reset"
android:textAllCaps="false"
app:layout_left="edit_code.left + 2dp"
app:layout_top="edit_code.bottom + 8dp" />
<Button
android:textSize="18.0sp"
android:id="#id/btn_login"
android:background="#drawable/btn_bg"
android:focusable="true"
android:visibility="gone"
android:layout_width="160.0dip"
android:layout_height="36.0dip"
android:text="#string/login"
android:textAllCaps="false"
app:layout_right="edit_code.right"
app:layout_top="edit_code.bottom + 8dp" />
<ProgressBar
android:id="#id/pb"
android:visibility="gone"
android:layout_width="22.0dip"
android:layout_height="22.0dip"
android:indeterminateDrawable="#drawable/login_progress"
android:layout_alignEnd="#id/btn_login"
app:layout_centerY="btn_login.centerY"
app:layout_right="btn_login.right-0.0556*screen.height" />
<TextView
android:textSize="22.0sp"
android:textColor="#color/white"
android:id="#id/change_login"
android:focusable="true"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please click here to login"
android:paddingStart="3.0dip"
android:paddingEnd="3.0dip"
app:layout_right="btn_login.right"
app:layout_top="btn_login.bottom + 8dp" />
<android.support.v7.widget.AppCompatCheckBox
android:theme="#style/MyCheckBox" android:gravity="center"
android:id="#id/statement_check"
android:layout_width="#dimen/_36dp"
android:layout_height="#dimen/_36dp"
app:layout_height="btn_reset.height"
app:layout_left="btn_reset.left"
app:layout_top="change_login.bottom + 8dp"
app:layout_width="btn_reset.height" />
<TextView
android:textSize="#dimen/text_12"
android:textColor="#color/white"
android:id="#id/statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/state_all"
app:layout_left="statement_check.right + 8dp"
app:layout_right="btn_login.right"
app:layout_top="statement_check.top" />
</com.github.mmin18.widget.FlexLayout>
I thought removing the padding would help or changing it to fill content would help but the text just disappeared
Maybe you got a better answer if you post the XML code of your application.
Usually, to get the text of an item centered you just need to use the "gravity" property.
In your XML file, search for the component with the text you want to center and add the following to it's properties:
android:gravity="center"
There are two properties of TextViews that might help you.
First is the gravity attribute. This attribute would center your text within the boundary of your TextView. Therefore, no matter how large or how small your actual TextView is, the text would always be in the center of it. Here's an implementation:
android:gravity="center"
There's also the layout_gravity attribute, this attribute positions your TextView relative to the nearest parent view. It is important to note that this attribute does not center the text, rather, it centers the whole TextView. Here's an implementation:
android:layout_gravity="center"
Refer to the documentation to learn more.
I hope this helps. Merry coding!
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"
I have a multi-line edit text with a 100dp lineSpacingExtra.
Now the text is shown at the top of a line. And the cursor's height is same as line height , it's much taller than the text's height.
How can I make the text shown in the center of the line?
Or if I can adjust the padding top or padding bottom of the text in a line.
Note:
What I mean is NOT using android:gravity="center_vertical" to make the text center vertical in the TextView.
I want to show the text center vertically in the line.
The xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#android:color/transparent"
android:fadingEdge="vertical"
android:gravity="top"
android:inputType="textCapSentences|textMultiLine|textShortMessage"
android:lineSpacingExtra="100dp"
android:longClickable="false"
android:minLines="10"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="5dp"
android:scrollbars="vertical"
android:text="Text ABCDE\n Line2"
android:textAlignment="center"
android:textSize="16sp" />
</LinearLayout>
Any help is appreciated.
THX!
Attach my result here:
There's no way to do this in an application.
Because the TextView draws the text at the top of a line in framework.
If u want the visual effect that the text shown center vertically in a line, the framework code must be modified.
The file location is:
frameworks/base/core/java/android/text/StaticLayout.java
The change:
- lines[off + DESCENT] = below + extra;
+ lines[off + DESCENT] = below + extra/2;//Show the text center vertically in the line.
Please correct me if something is wrong or there's a better way to do this.
Edited Answer:
Output is:
Code (from my own xml tag, so change your id):
<LinearLayout
android:id="#+id/layout_feedback"
android:layout_width="401dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_six"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:background="#drawable/textfield_activated_holo_dark"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:hint="Em "
android:visibility="invisible"
android:textColor="#color/white"
android:textSize="24sp" />
<EditText
android:id="#+id/ed_feedback_know"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/textfield_empty_holo_dark"
android:focusable="true"
android:text="Text ABCDE\n Line2"
android:focusableInTouchMode="true"
android:inputType="textEmailAddress"
android:textColor="#android:color/black"
android:textSize="24sp" />
</LinearLayout>
I need to display two single-line TextViews horizontally. The left TextView (let's name it 1) may have a longer text which may shortened and finished with "...". The right text view (2) has a short text and should never get shortened.
I want the 1 to remain aligned to the left end of the parent. The 2 aligned to the right side of 1.
There are now 2 conditions that I have to meet
a) if the 1 has a short text then the 2 should get aligned to the right of 1 (none of the gets shortened).
b) but if the 1 has a too long text then the text of 1 should be shortened by '...' while the view 2 is moved maximally to the right of the parent but still remains fully visible (no ...)
My current solution is the following below. The scenario b) is fine with mine, but in case of a) the problem is that the view 2 is moved to the right side of the parent and the 1 to the left side - both are short and there's pretty much space in between which looks odd. I want 2 to move to the further left (next to 1) and leave this space on the right side.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:layout_weight="0.5">
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/ns_in_txt"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</RelativeLayout>
Try doing this
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_context"
android:layout_marginTop="1dp">
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:gravity="left"
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/ns_in_txt"
android:gravity="left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</RelativeLayout>
Apparently, you want two scenarios which require to set a different orientation to the parent layout: first horizontal, second vertical. Maybe I'm wrong (and I hope so but) in static xml, there will be difficult to do this.
Try the code below to test if I'm wrong:
Scenario 1: orientation horizontal = the text 1 is not big enough
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="This is a normal text not big" />
<TextView
android:id="#+id/textShort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="short text" />
</LinearLayout>
Scenario 2: orientation vertical = the text 1 is too big
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="This is a biiiggg loooonnng teeeeexxxxxtttt" />
<TextView
android:id="#+id/textShort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp"
android:text="short text" />
</LinearLayout>
To resolved your issue, you can try 2 solutions.
First, try to create a maxLenght limit, which calculate in your Activity and change the parent orientation of the LinearLayout. Get the number of chars that you have and display the orientation as well.
Second, customise your own class extend TextView. And create a getWidth method which return the width of the long TextView in comparison to it parent and change the orientation.
Maybe the following questions/answers could be useful (I think there are not solutions but more as inspiration):
In Android how to get the width of the Textview which is set to Wrap_Content
Get the size of a text in TextView
How to find android TextView number of characters per line?
Auto Scale TextView Text to Fit within Bounds
EDIT:
I found a solution with the last url that I writed above. See this answer where the dev wanted to make the same as you. So he decided to create a autoresizable textview. Take a look here: Move two side by side textviews to be one under another if text is too long
I hope this will help you.
May I suggest to use combination of LinearLayout and a little bit of coding. The idea is to have them side by side regardless of the size. and after the right textview is measured and laid out, set the max width of the left textview to whatever space left.
Here is the layout file, nothing special here:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/ns_in_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
<TextView
android:id="#+id/ns_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="none"
android:gravity="top|left"
android:maxLines="1"
android:singleLine="true"
android:textColor="#00a2ff"
android:textSize="18sp" />
</LinearLayout>
and add some codes to the activity/fragment:
final TextView tvLeft = (TextView) findViewById(R.id.ns_txt);
final TextView tvRight = (TextView) findViewById(R.id.ns_in_txt);
ViewTreeObserver obs = tvRight.getViewTreeObserver();
obs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tvLeft.setMaxWidth(SCREEN_WIDTH - tvRight.getWidth());
}
});
I have a TextView in which I must load a message. The TextView has maximum 2 lines (android:lines="2"). The message may contain an '\n' character.
How should I load the message in that TextView so that words would be wrapped and if in those 2 lines message does not fit, at the end of last visible word I must add three dots (...)? How can I detect the length of text that fits in that TextView?
My code for TextView is
<TextView
a:id="#+id/tv_message"
a:gravity="top"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_toRightOf="#id/iv_icon"
a:layout_marginLeft="2dp"
a:layout_marginTop="4dp"
a:paddingRight="7dp"
a:paddingBottom="5dp"
a:textSize="12sp"
a:typeface="sans"
a:ellipsize="marquee"
a:lines="2"
a:maxLines="2"
a:textColor="#android:color/black"
/>
But in application, text appears on two lines, even if there is a line containing the signature. The message is like: "message text" + "\nSignature" but the message can be on 1,2,3 lines, depending of message length.
In XML, use the property android:ellipsize="marquee"for the TextView.
Your TextView can be defined like:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:lines="2"
android:ellipsize="marquee"/>
Edit: I've tried the following code, adapted from yours:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlt_news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_background"
android:padding="5dip" >
<TextView
android:id="#+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:text="Name"
android:textStyle="bold" />
<ImageView
android:id="#+id/rss_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_title"
android:layout_marginLeft="2dip"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:src="#drawable/rss_cell_icon"
android:visibility="visible" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#id/rss_icon"
android:ellipsize="marquee"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
android:textColor="#android:color/black"
android:textSize="12sp"
android:typeface="sans" />
</RelativeLayout>
And here is the output:
Use android:ellipsize="end" instead of "marquee" that did it for me.
Gabi,
I was also having the same problem. To enable ellipsize on a textview with 2 lines I use the following parameters:
android:maxLines="2" // You already got this one
android:ellipsize="end" // also this one
android:singleLine="false" // This is the command that solves your problem.
Unfortunately I got a new scenario for my app. I have to use 3 lines with ellipsize and this configuration only works for 2 lines... :( Good luck
If you set text from code, than you must set options once again!
textView.setText(content);
textView.setEllipsize(TruncateAt.END);
textView.setLines(5);
textView.setMaxLines(5);
It works for me when I set it in code not in the XML layout
holder.txtDescription.setText(text);
holder.txtDescription.setMaxLines(3);
holder.txtDescription.setEllipsize(TruncateAt.END);
String s = "line1 \nline2 \nline3";
String [] lines = s.split("\n");
String twolines;
if (lines.length > 2) {
twolines = lines[0] + "\n" + lines[1] + "...";
} else {
twolines = s;
}
This should give you the desired result, just use
((TextView)findViewById(R.id.text)).setText(twolines);
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
android:autoSizeTextType="uniform" did the trick for me. Text size reduces to fit available space but complete text is displayed.
Refer this link
https://developer.android.com/develop/ui/views/text-and-emoji/autosizing-textview