In android ellipsize text view between buttons - android

I've looked at many posts on similar subject but couldn't find the answer to this problem... I use a RelativeLayout to have a button on the left, a button on the right and text in the middle. If the text is too long I use ellipsize (see XML below). The problem is that when the text is long it also cuts the BEGINNING of the code (see image). I can fix that by using android:gravity="center_vertical" instead of "center", but then when the text is short it becomes left-justified. I want it centered. Any hint ?
<RelativeLayout
android:layout_height="60dp"
android:layout_width="match_parent" >
<ImageButton
android:id="#+id/leftbutton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:src="#drawable/back_chevron_icon"
android:background="#color/transparent" />
<ImageButton
android:id="#+id/rightbutton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:src="#drawable/back_chevron_icon"
android:background="#color/transparent" />
<TextView
android:id="#+id/sometext"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:layout_toRightOf="#id/leftbutton"
android:layout_toLeftOf="#id/rightbutton"
android:maxLines="1"
android:ellipsize="end"
android:textSize="22dp"
android:text="Long Long Long Long Long Long Long Long Text" />
</RelativeLayout>
What I want is shown in the 2 pictures below. I can achieve that only by changing the gravity from center to center_vertical and vice versa.

Simple. Use android:gravity="center" on your TextView instead of android:gravity="center_vertical" and also use layout_toEndOf / layout_toStartOf attributes with layout_toRightOf and layout_toLeftOf

I Just change few tages from your code
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<RelativeLayout
android:layout_height="60dp"
android:layout_width="match_parent" >
<ImageButton
android:id="#+id/leftbutton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"
/>
<ImageButton
android:id="#+id/rightbutton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"
/>
<TextView
android:id="#+id/sometext"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:layout_toRightOf="#id/leftbutton"
android:layout_toLeftOf="#id/rightbutton"
android:maxLines="1"
android:ellipsize="end"
android:textSize="22dp"
android:text="Long " />
</RelativeLayout>

you can add multiple attribute using pipe
like this
android:gravity="center_horizontal|center_vertical"

It turns out that the code in the initial post WORKS !
It is the Design simulator in Android Studio that actually does not work and shows the (wrong) text layout as in my initial picture. Thank you ##*%! Android Studio developers... I wasted several hours of my time thanks to you...

You can add maxWidth at your target TextView.

Related

TextView Vertical Gravity not working in ScrollView

First time posting here. Sorry if this has already been asked. I searched for days, and while some posts seemed relevant, they did not seem to address my exact issue.
I have a RelativeLayout inside a ScrollView. Inside the RelativeLayout, I have some TextViews. I have set the TextView gravities to center_vertical. Everything works fine when the page's contents fit on a single page without the need to activate the ScrollView. However, whenever the content exceeds the page and the ScrollView gets activated, the center_vertical gravity gets ignored. The funny thing is, the left/right (start/end) gravities are not being ignored, only the center_vertical ones.
Here's the main part of my code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fillViewport="true" >
<RelativeLayout
android:id="#+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/cup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/cup1"
android:textColor="#21386f"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:id="#+id/Space1"
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_below="#id/cup1" />
<ImageView
android:id="#+id/canadienslogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/Space1"
android:contentDescription="#string/logo"
android:src="#drawable/canadienslogo" />
<TextView
android:id="#+id/score1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/canadienslogo"
android:layout_alignTop="#id/canadienslogo"
android:layout_below="#id/Space1"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="16sp"
android:paddingRight="16sp"
android:text="#string/score1"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/team_canadiens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/canadienslogo"
android:layout_alignTop="#id/canadienslogo"
android:layout_toEndOf="#id/canadienslogo"
android:layout_toLeftOf="#id/score1"
android:layout_toRightOf="#id/canadienslogo"
android:layout_toStartOf="#id/score1"
android:gravity="center_vertical|start"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:text="#string/team_canadiens"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/rosebudslogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#id/Space1"
android:contentDescription="#string/logo"
android:src="#drawable/rosebudslogo" />
<TextView
android:id="#+id/team_rosebuds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/rosebudslogo"
android:layout_alignTop="#id/rosebudslogo"
android:layout_toEndOf="#id/score1"
android:layout_toLeftOf="#id/rosebudslogo"
android:layout_toRightOf="#id/score1"
android:layout_toStartOf="#id/rosebudslogo"
android:gravity="center_vertical|end"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:text="#string/team_rosebuds"
android:textSize="14sp"
android:textStyle="bold" />
Below this, there is another Space followed by a TableLayout. If the table is too big to fit on a single page, the center_vertical gravity above gets ignored. The gravity does not appear to be affected within the table.
Here are two screenshots of the issue. The first has the issue, the second does not (because all the contents fit on a single page)...
Issue
No Issue
Notice how the text "Montreal Canadiens" "3 - 2" and "Portland Rosebuds" gets moved up. The center_vertical gravity is not being applied.
For the record, I only took screenshots in landscape mode because it would force ScrollView to kick in and show the issue. If I were to have bigger tables, the same issue occurs in portrait mode as well, so it's not an orientation issue. I've modified my code countless times but nothing seems to fix it. I'm sure it's something simple, I just can't figure it out. It might have something to do with the overall density of the height, since that's really the only thing that changes when ScrollView kicks in. I enabled the "Show layout bounds" under Developer Options and the actual containers are fine, it's just the contents that get pushed up. The weird thing is, both Eclipse and Android Studio do not show the issue in Design Mode. FWIW, I've tested it on my Galaxy Nexus and a Galaxy Tab 3 Lite, same thing occurs on both.
Hopefully one of you can make me look foolish and resolve it promptly.
Thanks in advance for all the help.
Regards,
EDIT: New working code...
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/cup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/cup1"
android:textColor="#21386f"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:id="#+id/Space1"
android:layout_width="match_parent"
android:layout_height="24dp" />
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/canadienslogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/logo"
android:src="#drawable/canadienslogo" />
<TextView
android:id="#+id/team_canadiens"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|start"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:text="#string/team_canadiens"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/score1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="#string/score1"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/team_rosebuds"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|end"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:text="#string/team_rosebuds"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/rosebudslogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/logo"
android:src="#drawable/rosebudslogo" />
</LinearLayout>
cannot say that it will solve your problem but you have a few quirks:
android:id="#+id/score1"
android:layout_alignBottom="#id/canadienslogo"
android:layout_alignTop="#id/canadienslogo"
You cannot align both bottom and top. if you want to match the height of 2 Views - then give them an height
another layout with the same issue:
android:id="#+id/team_canadiens"
android:layout_alignBottom="#id/canadienslogo"
android:layout_alignTop="#id/canadienslogo"
android:layout_toEndOf="#id/canadienslogo"

Resize TextView to wrap content using XML

This question has been asked tonnes of times, but I didn't get any of the answers work from me.
How do i get the textview to resize itself to accommodate the text that is larger than single line(may be around 5 lines)? Please note that I am looking at solutions to work with XML ONLY.
<TextView
android:id="#+id/item_comments_textview_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/comments_comment"
android:textAppearance="?android:attr/textAppearanceSmall" />
Here are the attributes that i tried without any effect.
android:singleLine="false"
android:maxLines="5"
android:scrollHorizontally="false"
android:layout_height="0dip"
android:layout_weight="1"
android:inputType="textMultiLine"
Can someone let me know how to make textview grow in size, without specify a constant height?
My total XML:
<?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:background="#android:color/white"
android:orientation="horizontal"
android:paddingBottom="24dp"
android:paddingTop="20dp" >
<ImageView
android:id="#+id/item_comments_imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:contentDescription="#string/empty"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="16dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/item_comments_author"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="0.50"
android:ellipsize="end"
android:maxLines="1"
android:gravity="top"
android:text="#string/comments_author"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="#+id/item_comments_comment_date"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="right"
android:text="#string/comments_date"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<TextView
android:id="#+id/item_comments_textview_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/comments_comment"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
I want the textview with id "item_comments_textview_comment" to grow to accommodate the comment given by the user, to upto a maximum of 5 lines. Currently it only displays 1 line and cuts the rest of the text.
I think your comment textview (item_comments_textview_comment) is being limited by its parent LinearLayout height attribute. Change that height value from match_parent to wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:orientation="vertical" >
...
You should try using RelativeLayout for the design of the row elements. It can help you to designate position of each of the elements relative to components to manage your design.
Not only that it can also help in boosting the performance of your application. There is an analysis available on this and this one particularly inspects a design similar to yours. Check here: Optimizing Layout Hierarchies

Android ListFragment: background under each item

I'd like to create a ListFragment in Android, and a background for each element, like this:
I'll want to set the width of theese background's from the code of my ListAdapter. I thought to create the list element's layout something like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:id="#+id/surveyListRow_upperLayout"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp">
<RelativeLayout
android:layout_marginLeft="23dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/surveyListRow_ringLayout"
android:background="#FFFFFF"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/textColor"
android:gravity="center"
android:id="#+id/surveyListRow_surveyName"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="55dp"
android:layout_marginRight="105dp"/>
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:textSize="18sp"
android:gravity="center"
android:id="#+id/surveyListRow_surveyPrize"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="57dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="30"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="70"/>
</LinearLayout>
</RelativeLayout>
And I'll set the weight property of the LinearLayout's children, to set the background width.
My problem is, when the text in the width is too long (as you can see in the third element). Then the background isn't scales to the bottom of the element.
Can you help me, why? I've tried almost everything I can imagine.
Thanks!
Try to add this to your TextView
<TextView
...
android:ellipsize="end"
android:ems="2"
android:maxLines="2" />
This don't let your textview has more than two lines and put an ellipsis on the end.
Maybe this would be a solution.

Android Button stop "ellipsis" its test when singleLine="true"

As the title says, I want to stop the "ellipsis" from occurring and for it to show the whole string associated to that button. I can´t figure out why it is not displaying the string, but instead it is trimming it.
I know that "singleLine" forces a "trimming", but I want it in a single line, and not to wrap. I need the button to show it´s small string in one line, that´s all.
I have also tried using minWidth and maxWidth to no avail.
The problem occurs when viewing the layout in an Android 2.3.6 phone (Samsung Galaxy Mini).
<RelativeLayout 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"
tools:context=".MainActivity"
android:background="#drawable/background2" >
<ImageButton
android:id="#+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="24dp"
android:contentDescription="#string/quote"
android:background="#00000000"
android:padding="30dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/makeAMemory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginRight="70dp"
android:text="#string/button_addMemory"
android:singleLine="true"
android:textColor="#E4F5ED"
android:background="#drawable/button_shape_shadow"
android:padding="10dp"/>
<Button
android:id="#+id/fixMemory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/makeAMemory"
android:gravity="left"
android:layout_marginTop="12dp"
android:text="#string/button_fixMemory"
android:textColor="#E4F5ED"
android:background="#drawable/button_shape_shadow"
android:padding="10dp"
android:onClick="startListViewMemoryActivity"/>
<Button
android:id="#+id/getMemory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fixMemory"
android:gravity="left"
android:layout_marginTop="12dp"
android:text="#string/button_getMemory"
android:textColor="#E4F5ED"
android:background="#drawable/button_shape_shadow"
android:padding="10dp"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The string is "Make memory". It´s not long at all! Any help is much appreciated.
EDITED
try adding:
android:ellipsize="none"
EDITED
In the end this is what worked:
Took out the ScrollView
Changed RelativeView from "wrap_content" to "match_parent" for the android:layout_width
The rest stays the same
It looks like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
>

Android layout clipping

I am trying to make a layout as described below. I don't understand how to exactly implement the layout.
The layout and the above hexagon will have some text that i need to change dynamically from code.
I have tried a similar way mentioned here
But the hexagon is still clipping. I'm using the following code.
<RelativeLayout
android:id="#+id/Llfirstamount"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/layoutcolor1"
android:clickable="true"
android:clipChildren="false" >
<TextView
android:id="#+id/my_first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:android:layout_alignParentBottom="true"
android:android:layout_alignParentLeft="true"
android:padding="15dp"
android:text="Amount"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/my_second_text"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="-20dp"
android:background="#drawable/hexagon"
android:clipToPadding="false"
android:contentDescription="#string/contentdesc_peso_logo"
android:gravity="center"
android:text="x5" />
</RelativeLayout>
I don't know is this the only way or the right way or there is a much better way to implement this.I'm very confused.
Please Help!!! thank you..
EDIT:
Ok thanks for your answers using two text views with custom background is is a good and clean idea. Now I'm using the edited code but the hexagon is clipping like below..
Am i missing something i have also added
android:clipToPadding="false"
and
android:clipChildren="false"
in my code.
This will do it..
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#ccc"
android:gravity="center_vertical"
android:layout_centerInParent="true"
android:padding="5dp"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_marginLeft="-25dp"
android:layout_marginBottom="-25dp"
android:layout_toRightOf="#+id/textView2"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
OUTPUT:
Good Luck :)
If you have to change the text from code you probably don't want to use an ImageView.
try with two TextViews (the green one and the hexagonal one which will take your image as background)
<RelativeLayout
android:id="#+id/Llfirstamount"
android:layout_width="wrap_content"
android:layout_marginTop="40dp"
android:layout_height="wrap_content"
android:background="#drawable/layout"
android:clickable="true"
android:clipChildren="false"
android:orientation="horizontal" >
<TextView android:id="#+id/my_first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:android:layout_alignParentLeft="true"
android:android:layout_alignParentBottom="true"
>
</TextView>
<TextView android:id="#+id/my_second_text"
android:layout_width="25dp"
android:layout_height="25dp"
android:android:layout_alignParentTop="true"
android:android:layout_alignParentRight="true"
android:contentDescription="#string/contentdesc_peso_logo"
android:background="#drawable/hexagon" />
</RelativeLayout>
Then you can use Activity.findViewById() to get the references on your textviews and change their texts.
Adding android:clipToPadding="false" and android:clipChildren="false" to the top-most layout view will do the trick, if you can bear all sub-views having the same effect.

Categories

Resources