This question already has answers here:
How can I show ellipses on my TextView if it is greater than the 1 line?
(7 answers)
Closed 1 year ago.
i have a simple question but i didn't how to solve i tried some stuff but didn't helpso i wanna know if i display a long text into a textview and it doesn't fit they just cut the last word i want to put "..." in the end if the text if it is too big for the textview.
this is the textview code
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="65dp"
android:layout_weight="0.5"
android:autoSizeTextType="uniform"
android:clickable="true"
android:gravity="left"
android:text="TextView"
android:textColor="#000"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
enter image description here
You can add below code in your TextView
android:ellipsize="end"
android:maxLines="1"
Related
I know there are many similar questions, and I have read many answers, but none of them are what I want. I have tried almost all of them. The following are my attempts (I set ellipsize=end for each attempt):
singleLine=true (works)
lines=1 (not work)
maxLines=1 (not work)
set width of textView to specific value (not work)
ScrollHorizontally=true (not work)
Only the first one works, but I want multiple lines of text instead of one line
Is there any other way to achieve this
Any help will be appreciated
Edit:
And here is my xml layout(in attempt 2):
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="#dimen/x150"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:ellipsize="end"
android:lines="1"
android:text="hello hello hello hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>
I used ellipsize=true in the project I developed before, and it worked well in that project, I compared the code of the current project with the previous one. There is no difference between each other, so I am confused with the current project, so I pulled the previous project from github and ran it, and found that it NOT WORK in my xml renderer, but it WORKS on my phone.
Eventually I found it is the problem of the renderer. I found the button to upgrade the renderer. Now I use the latest renderer, and it also works fine in the redener now.
This is working fine for me
<TextView
android:id="#+id/textView8"
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="parent"
android:ellipsize="end"
android:lines="3"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="TextVie mbdfdsvffmnsvfbnsddfdfvfndvfnbdvfbndsvfnsdvfnsdvfnsdvfnsbfjnbdsvjfvdsjhfvsdjhvsdsdvfjdvfjsdvfjhsdvfjsdvfjdhsvfhjdvfjdvfhjdvfjdsvfjsvfhjsdvfjsdvfjsdvfjsdvfjsdvfnsdfvbnsdvfbnsdvfnsdvfnsdbvfnbsdvnsdvsdvbfsdnbfw"
android:textSize="16dp" />
try adding maxLines with the lines count you want
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:ellipsize="end"
android:maxLines="4"
android:text="hello\n hello\n hello\n hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image" />
Please note that the ellipsis will only be shown if the text size exceeds the number of lines allowed or the text size width; otherwise, it will not be shown.
If what you want is that at the end of the text the ellipsis is always shown, you must create a method which concatenates the (text + "...").
something like this:
TextView textView = findViewById(R.id.textView);
String valueText = textView.getText().toString();
if (!valueText.isEmpty()) valueText = String.format("%s...", valueText);
textView.setText(valueText);
The following example shows the ellipsis mentioned above, the first text exceeds the size of the text view and the second does not.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="hello hello hello hello hello hdsafsfdfdel sdsdasdsadasdsadsadasdasdsadsaadssadsadsadsadsasadsadsadsadsadsadsaadsalo hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
android:textColor="#android:color/black"
android:textSize="16sp"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ellipsize="end"
android:maxLines="2"
android:text="hello hello hello hello hello hel sfdsafsafsfasfsafsafsahhhhhhhhhhhhhh"
android:textColor="#android:color/black"
android:textSize="16sp"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
Set property in java file
yourTextView.setEllipsize(TextUtils.TruncateAt.END);
yourTextView.setMaxLines(3);
yourTextView.setText("long text");
This question already has answers here:
Android Material Button with Icon on top of Text
(2 answers)
Closed 2 years ago.
Using Material design, I need to create a button with text and Image aligned vertically.
<com.google.android.material.button.MaterialButton
android:id="#+id/btn"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="160dp"
android:layout_height="160dp"
android:clickable="true"
android:elevation="4dp"
android:fontFamily="#font/roboto_bold"
android:onClick="myfunction()"
android:text="BUTTON"
android:textColor="#android:color/black"
android:textSize="20dp" />
Use android:drawableBottom="..." or android:drawableTop="..." to align text and an image vertically.
dependencies {
implementation "com.google.android.material:material:1.2.0-alpha01"
}
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_android_black_24dp"
android:drawableTint="#color/white"
app:backgroundTint="#429835"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:text="Hello world" />
This question already has answers here:
Inserting imageview inside edittext android
(4 answers)
How can I add an image on EditText
(10 answers)
Closed 4 years ago.
I am using the EditText with drawableLeft property and set ImageView in Left side for displaying mobile icon. My Question is how to put Imageview in EditText?
Please help me.
Actually i want like this.
I have tried using linear layout with horizontal. below is xml code.
<LinearLayout
android:weightSum="2"
android:layout_below="#id/welcome"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_weight="1"
android:layout_gravity="center"
android:id="#+id/image_mobile"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:src="#mipmap/ic_launcher" />
<EditText
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_30sdp"
android:layout_toRightOf="#+id/image_mobile"
android:hint="Mobile number"
android:inputType="text" />
</LinearLayout>
But am not able to adjust the image view and edittext.
You should use android:drawableStart
The drawable to be drawn to the start of the text.
DEMO
<EditText
......
android:drawableStart="#drawable/your_icon"
android:drawablePadding="5dp"
/>
This question already has answers here:
How to add drawable image inside textview
(2 answers)
Closed 4 years ago.
I have a textView with an image and text, I want to align text with the baseline of the image. I am trying but unable to do this. How can I do this? Thanks in advance.
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:drawablePadding="4dp"
android:text="Hello"
android:drawableStart="#drawable/date"
android:gravity="bottom"
android:textColor="#color/black"
android:textSize="10sp" />
I want to text and image like this, they share the same baseline:
Try Below:
Use android:drawableTop to set Image at top.
<TextView
android:id="#+id/news_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:drawableTop="#drawable/progress_image"
android:textColor="#color/black"
android:text="Below Text"
android:textSize="10sp" />
This question already has answers here:
android ellipsize multiline textview
(23 answers)
Closed 9 years ago.
How can I make a multiline TextView that truncates the text properly? My TextView is part of a TableRow, so its width is a weight. The following does not work. The result is simply truncated text without the ellipses. I'm using API 10 (Android 2.3.3).
<TextView
android:id="#+id/name_recipe"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:text="Fried pork with mushrooms and potatoes and cilantro"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="10dp" />
Reducing the two lines to a single line with the following does work, but it's not ideal:
android:singleLine="true"
I don't like this for two reasons:
android:singleLine is deprecated
I'd much prefer to have the text be on two lines than one.
I've seen discussion about this like from here but I'm hoping a new solution has been found by now.
Try this, it's from my project and works on phone but unfortunately not on tablet emulator:
<TextView
android:id="#+id/myItem"
android:layout_gravity="bottom|center_vertical"
android:textSize="10sp"
android:maxLines="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="end"
android:lines="2" />
Use lines="1" as all the other methods are deprecated. As well as set scrollHorizontally.
<TextView
android:id="#+id/myid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
/>