How to wrap text to next line in an Android TextView? - android

I am using following TextView to display some data in it:
<TextView android:id="#+id/notificationText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="18sp"
android:inputType="textMultiLine"
android:maxLines="2"
android:layout_paddingRight="20dip"
android:textColor="#ffffff"/>
I want to wrap text to next line. How can I do this?

you must set android:scrollHorizontally="false" in your xml.

In Some case It works by setting width to 0dp on text views.
android:layout_width="0dp"
here is a link to original answer.

You could also try
android:inputType="textMultiLine"
in your XML. This worked for me.

You need to set
android:minLines="2"

Related

TextView's ellipsize not working on maxLines = 1

I really cannot figure out why, but I am only able to get ellipsize working on maxLines=2 and more. I am displaying a few words of description and then a long string with no spaces.
This is how the TextView looks like:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#757575"
android:text="#string/gcm_not_registered"
android:maxLines="1"
android:ellipsize="end"
android:id="#+id/login_gcmRegistrationTextView"/>
I then programatically set a text to it, but depending on the maxLines limitation, I get two different results:
The only thing that changed was the maxLines, why isn't the line filled in the first picture as well?
There are two ways to fix it:
Try to change android:ellipsize="end" attribute to android:ellipsize="marquee".
Try to remove android:maxLines="1" android:ellipsize="end" attributes and add android:singleLine="true" attribute.
This code works for me:
In the xml add:
Attribute ellipsize: marquee
Attribute lines: 1
In java:
<yourTextView>.setHorizontallyScrolling(true);
<yourTextView>.setSelected(true);
If there is another item that request the "focus" you lose the marquee effect. The textView need the state selected to prevent this.
Actually the problem is with the spannable text, if you set spannable text this wouldn't work. Other than this below code works for me
<TextView
android:id="#+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_margin="5dp"
android:padding="5dp"
android:fontFamily="sans-serif-condensed"
android:textColor="#b3277b"
android:background="#f7ecff"
android:layout_below="#id/tv"
android:text="This is first line\nThis is second line\nThis is third line"
android:ellipsize="end"
android:maxLines="2"
/>

Android TextView text changes color on touch, but it shouldn't

I've encountered a strange problem where I have a TextView that I have made scrollable through XML android:scrollbars="vertical" & programmatically with
.setMovementMethod(new ScrollingMovementMethod());
When I touch the TextView, the white text changes to light gray.
If I remove the .setMovementMethod(new ScrollingMovementMethod()); I don't get that response.
What is causing this and how do I prevent it?
Thanks
<TextView
android:id="#+id/questionView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:autoLink="none"
android:clickable="false"
android:fadingEdge="horizontal"
android:fadingEdgeLength="#dimen/Fading_Edge"
android:includeFontPadding="true"
android:linksClickable="false"
android:longClickable="false"
android:padding="4dp"
android:scrollbars="vertical"
android:text="Question"
android:textSize="25px"
android:typeface="sans" />
Try using solid colors for your textview attributes as follows
android:textColor="#000000"
android:textColorHighlight="#000000"
Use to extend your class with scrollview in java code for scrollablitiy hope the textview color does not change
try this it may help you:
android:focusable="false"
and/or
android:focusableInTouchMode="false"

How to end TextView with 3 dots by using maxLength

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.
I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size in dp, something that I don't want to do.
I know it is possible programmatically by trimming the string after the 15th character and then adding the 3 dots, but I prefer to do that by XML.
Any idea how to end the text with 3 dots and leave it wrap_content?
<TextView
android:id="#+id/inbox_contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
android:maxLength="15"
android:textColor="#0670b4"
android:textSize="16sp" />
This will solve your problem, Use ellipsize Property in your XML Code
android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->
Edit: singleLine is deprecated. Use maxlines="1" instead.
You cannot use both maxLength and Ellipsize although you can define Maximum EMS see the example below
<TextView
android:id="#+id/tv_hist_source_lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="8"
android:maxLines="1"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium" />
I gather (from comment) that #Yaniv already solved it using code - but this is the right way to do it (with xml). May help other users who land here. Trick is to use both toleftof and torightof.
<RelativeLayout>
...
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="true"
android:layout_toRightOf="#id/some_element1"
android:layout_toLeftOf="#id/some_element2"/>
...
<RelativeLayout>
You can use
android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"
Only this works for me.
One of the easiest way is to add Right Padding + Ellipsize
<TextView
android:id="#+id/txtvw_contentcell_subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#color/caption_black_color"
android:textSize="#dimen/caption_size"
android:paddingRight="40dp"/>
Here is an example Subtitle Text with char limit.
use this android:ellipsize="end"
Tried most of the solutions above. Using maxWidth was the key for me:
android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"
Very Important: ellipsize = "end" only works when maxLength is set to a value that is more than the number of characters on one line.
You can use wrap_content if you align the TextView's end and start to any other view(s).
I needed to do this with Radio Button and I wanted to limit the size to one line. When I used Android:singleline="true", radio button's check circle disappeared. Here's the code that finally worked:
android:ellipsize="end"
android:maxLines="1"
There's no need to set ems. This will probably work also in TextView and other UI components. Hope this helps to someone.
You can just modify the String when it's length is above 20 to add the ellipsize.
remove the line from xml
android:lines="1"
android:maxLines="1"

How do I center the hint text within an EditText in Android?

I need to center the Hint text within an EditText in Android. How do I do this?
In order for centering of hint text to work with EditText you have to make sure android:ellipsize="start" is defined. I don't know why this makes it work, but it does.
Example pulled from personal code:
<EditText
android:id="#+id/player2Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="#string/player2_name"
android:inputType="textCapWords|textPersonName"
android:singleLine="true" />
Actually, android:gravity="center_horizontal" creates the centering effect you're looking for. Similarly, you can use android:gravity="start" to put hint text at the beginning of the EditText view, and so on.
Use this xml attribute:
android:gravity="center"
use attribute
android:gravity="center"
I used this code in every circumstances, and it works perfectly without using android:ellipsize="start" to make a hint in center.
<EditText
android:id="#+id/player2Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="robi"
android:inputType="text" />
I think the answer should be :
android:textAlignment="center"
textAlignment worked for me.
textAlignment="center"
Unfortunately, neither answer helped me aligning hint, written in LTR language, while the layout orientation was RTL. I needed such layout in a kind of translation application to make overlay icons not interfere with RTL text. But this trick didn't work with hints while there was no any text yet (icons appeared above the hint) and I came to the following java solution to layout problem:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
. . .
if (isRightToLeft()){
EditText guess = (EditText) rootView.findViewById(android.R.id.text1);
CharSequence hint = "\u200F" + guess.getHint();
guess.setHint(hint);
}
. . .
}
The trick was in right-to-left mark to prepend a left-to-right string. And it seems to work, but does anyone know a more elegant solution?
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="ENTER PIN"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
Note: in the tag TextInputEditText,the property
android:gravity="center"
is what makes the deal of aligning the text in the center including the hint text
use this: android:gravity="center"
I use this and worked for me
android:gravity="Left|center_vertical"
use android:textAlignment="center" in EditText , it work for me
This worked for me:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nombreslayoutinput">
<EditText
android:id="#+id/nombreslayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/nombres"
android:layout_centerInParent="true"
android:gravity="center|center_vertical"
android:ellipsize="start"
android:inputType="textCapWords|textPersonName"
/>
</android.support.design.widget.TextInputLayout>
The correct answer is
android:gravity="center_horizontal
hint gravity
android:textAlignment="center"
text gravity
android:gravity="left"
My problem was that the EditText wasn't big enought to fit exactly inside its parent (FrameLayout in my case), so using just android:gravity="center" (center_vertical, horizontal, start, or whatever) would just fit it inside the EditText, and not in its parent, so I had to center the EditText inside its parent using:
android:layout_gravity="center"
pd: I used android:background="#android:color/transparent" to hide the ugly underline in the EditText hint

EditText hint doesn't show

My EditText configured as follows won't show the hint:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:hint="The hint..."
android:scrollHorizontally="true"
android:singleLine="true" />
It works if I set android:gravity="left" or if I remove android:scrollHorizontally and android:singleLine attributes, which is not desirable. Any suggestions?
using android:ellipsize="end" fixed it for me
Weird bug !! (but Android has a lot of these weirdo bug)
In Lollipop version the default text and hint text color is white for EditText.
So we have to change like this in EditText
android:textColorHint="#color/grey"
I wanted my single-line EditText box to scroll but keep the hint on the right also.
I had the same issue and got the hint to stick by keeping gravity="right", and setting singleLine="true" and ellipsize="end".
You need to give text color to hint
android:textColorHint="#000000"
No need of android:scrollHorizontally attribute. Remove it.EditText is a fixed item on the screen. we want scroll the layout contains the EditText is enough. that is the best design too. you have put android:ellipsize="end" instead of android:scrollHorizontally.
Using android:ellipsize="end" resolves the obvious platform bug. Unfortunately, Xperias still misbehave :(
I found no other solution than to:
if (android.os.Build.MANUFACTURER.matches(".*[Ss]ony.*"))
editText.setGravity(Gravity.LEFT);
else
editText.setGravity(Gravity.CENTER);
The below worked for me:
<EditText
android:id="#+id/UserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/UserPassword"
android:layout_marginTop="85dp"
android:ems="10"
android:hint="#string/UserHint"
android:inputType="textPersonName"
android:singleLine="true"
android:text="#string/UserName" >
<requestFocus />
</EditText>
This is how, I did for by EditText to have hint in it.
<EditText
android:id="#+id/productQuantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:hint="#string/quantity"
android:inputType="numberSigned"
android:ellipsize="end"
android:singleLine="true" >
</EditText>
Try changing the hint text color, sometimes the hint color is the same as the background color
android:textColorHint="#color/colorBlack"
I changed the style to "#style/Base.WidgetMaterialComponents.TextImputEditText". For me it wasn't gravity or color.

Categories

Resources