When using android:ellipsize="end" property, 3 dots are vertically centered instead of appearing in the bottom of the view.
How should I make 3 dots appear in the bottom?
<TextView
android:id="#+id/widgetTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="#string/lorem_ipsum_short"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginStart="#dimen/spacing_4"
android:paddingEnd="#dimen/spacing_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#id/widgetTimeArrived"
app:layout_constraintStart_toStartOf="parent"
/>
How it looks
It's hard to tell without access to your full layout, but that's not the standard behavior, so you may be using a custom Font/Theme.
This is how it looks in a blank layout with a ConstraintLayout:
Notice I pinned the "end" to the parent, since I don't know what your widgetTimeArrived is.
I am getting your expected results by implementing your code. I think #Martin Marconcini is right about some custom theme or it might be some views alignment or constraint issue.Your full xml might be helpful to understand that.
Here are the results:
Related
I have two TextViews in a vertical LinearLayout, one serves as a display for a book's title and the latter as a display for the book's author(s).
I need the first to have wrap_content as its height, so it takes a good part of the linear layout. However, I want it to cap out at three lines max, so that there is still some space left for the second text view;
and I need the latter to fill the remaining space (0dp and layout_weight=0dp).
I want to use specific configuration so that the author view will be always right after the title view (on its bottom).
Something like this, however the max_lines do not kick in.
I tried to set max_lines to 3 and wrap_content for the first view height, but it seem that max_lines is ignored if height is set to wrap_content.
I also tried to circumvent the problem by sort of cheating and adding a max_height, but then the two views may be spaced apart from one another.
At last I tried to convert the linear layout to a constraint layout, to see if I could access some other layout settings to no avail.
Any help?
It seems to be working fine on my side with android:max_lines="3" even with wrap_content
This is just a test layout I created
<TextView
android:id="#+id/textview1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="#dimen/_13sdp"
android:textStyle="bold"
android:text="Very Very Very Very Very Very Very Very Very Very Very Very Long Text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Very Very Short Text "
app:layout_constraintStart_toStartOf="#+id/textview1"
app:layout_constraintTop_toBottomOf="#+id/textview1" />
Screenshot ->
Hope this helps! :)
Replacing app:max_lines with android:max_lines seems to have fixed the issue.
I'm working on a TextView which is contained in a ConstraintLayout.
I want ellipsize to add three dots at the end of text(in the TextView) if its length exceeds maxLength.
maxLines="1" and ellipsize="end" seemed to the best answer after a thorough research about my question. Unfortunately, it didn't work for my case. The three dots did't show at all.
Here's the snapshot :
The original string was "Test for long description"(The n was dropped). It's supposed to show "Test for long descrip...".
Here's my xml :
<TextView
android:id="#+id/txtDescription"
android:maxLength="24"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="DescriptionView"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.72" />
I feel like there's something override ellipsize in the XML, or did I miss something crucial in the XML file?
The problem is:
android:maxLength="24"
remove it since you want the TextView to be ellipsized.
A TextView gets ellipsized when it is not wide enough to show the whole text. I think you do not understand what this attribute android:ellipsize="end" is all about. If it is wide enough then you will not see any dots at the end because they are not needed.
If you set android:layout_width="40dp" then you will see the dots.
With android:layout_width="wrap_content" the TextView is wide enough and it is not ellipsized.
1) Add one more property android:singleLine="true" in your Textview
(But its Deprecated so use second option)
2) Use this three together
android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
Ellipsize is quite a pain in android, apply below on your textView :
txtDescription.setSingleLine(true);
Hope it helps.
The key for making three dots shows is constrain the width of the text view.
Suppose the name of image in your posted figure is left_image,
change the xml attributes of TextView as below:
....
android:layout_width="0dp"
app:layout_constraintStart_toEndOf="#+id/left_image"
....
above two lines make the width of TextView is constrainted(between left img and right border of parent).
In my case following line was the problem. I commented it out and it worked.
<item name="android:inputType">text|textNoSuggestions</item>
To fix this, you need to use a fixed width for the textview instead of maxLength attribute in your xml
In xml :
android:maxLines="1"
android:ellipsize="end"
And in java/kotlin program :[Important]
textDescription.setSelected(true) -> Java
or
textDescription.isSelected=true -> Kotlin
Why all my controls(buttons, textfields) are all in upper left of my emulator when I test run it?
When you first add widgets to your App, it automatically goes to the Top-Left of the screen. Try adding some Constraints to your widgets.
To make a View position in the center of your App, add these lines of code to the bottom of a widget in your XML:
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
/>
It Looks like you may be using a constraintLayout. Make sure that in your layout you have a vertical and horizontal constraint set on each control.
Your views are not setup up correctly in your layout. First of all, if you could give the code for your layout (activity_main.xml or something similar) it will be easier to solve your problem.
The likely culprit is either that you are using a RelativeLayout and haven't set the view to be in relative position to another or, more likely, that you are using a ConstraintLayout and haven't linked your views correctly.
If you are using constraint layout the easiest way to link your views is from the Design tab, looking at the UI of your app. Click your TextView/EditText/etc and drag from the little squares that appear on the sides to literally link to either the side of the screen or to another view.
I'm trying to achieve the following layout: a fixed width TextView aligned to the left of its parent, with the text inside it aligned to the right side of that TextView (that's why fixed width, can it be done other way?) and the rest of the parent is filled with a drawable (simple line). Like this:
It's a ListView containing 2 types of rows and the layout for the rows with lines is quite trivial - LinearLayout with TextView and ImageView (I can post the exact code later if needed). And I'm getting a warning that it could be replaced with a single TextView with compound drawable.
I'm all for optimization so I really tried to follow that advice. Unfortunately I wasn't able to get the same result - the line is either constrained to TextView's width or text is aligned to the right side of the ListItem, now to fixed position.
Am I missing something?
Edit: Apparently it is not actually possible and since there are some other complications (the drawable is now a level-list drawable, which is not always a line and sometimes it has a non-fixed height that I have to set) I will leave it as it is now - linear layout, containing one TextView and one ImageView.
I don't think that you're missing anything. The TextView compound drawable features are not very customizable and in general are not worth the time you spend trying to get them to look right. Some lint warnings are a little overzealous and premature.
The optimization that the lint refers to is something that is better attributed for a fixed size image. In your case, the line has to stretch the rest of the screen length and as such it is not something that can be done with a textview with compound drawable. This kind of lint warning is more of a suggestion rather than something that MUST be done and is detected by just checking for a linear layout with only a textview and an imageview rather than checking what would need to go in the image view. If you already have it working the way you did it I think you should leave it alone.
Your view create from this -
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/time"
android:layout_width="#dimen/today_time_width"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="right"
android:layout_marginRight="5dp" />
<ImageView
android:layout_gravity="center"
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="#dimen/today_current"
android:src="?attr/item_boundary" />
</LinearLayout>
There is no way to achive this using only standart TextView. If you really want to reduce view count you can create your custom TextView class, set layoutWidth to matchParent and draw line from text end to right border. But it's not worth to be doing. Some extra views won't slow your list.
I am not sure if you will be able to achieve what you really want to , but then you could change the linear layout in the link you posted to something like this:
<RelativeLayout
android:id="#+id/relTrial"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="12:45 AM"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:id="#+id/lnrSep"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_toRightOf="#+id/txtTime"
android:layout_alignParentRight="true"></LinearLayout>
</RelativeLayout>
This way the time text will be right aligned although being at the left side, and the line will also be visible.
Hope that helps.
If I got you right, you want to add bottom border to list view item?
What about to try this:
android:drawableBottom="#drawable/line"
I'm trying to create standard button in android with a background and some text in front but some fairly specific alignment. I want the text to be centered vertically and on the left with 20dp of padding. The alignment works but the padding doesn't. I know I could probably get the desired effect by putting a few spaces in the text but that seems like a hack and next I want to do a similar thing but with the text at the top so I would prefer a more elegant solution.
Here's what I have:
<Button
android:layout_width="312dp"
android:layout_height="95dp"
android:id="#+id/gv_music_button"
android:text="Music"
android:textSize="30sp"
android:paddingLeft="20dp"
android:gravity="left|center_vertical"
/>
My mistake, padding was working correctly. Just didn't appear to be.