android textview right of a multiline textview - android

i am suppose to place a TextView right of a multiline TextView.
please check the code below .
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rlanswer"
android:layout_marginLeft="#dimen/grdiviewspacing"
android:layout_marginRight="#dimen/grdiviewspacing"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/lblanswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:lines="1"
android:text="#string/strbookmarkmessage"
android:textColor="#color/black"
android:textSize="#dimen/listviewlocationsize"
android:textStyle="bold" />
<TextView
android:id="#+id/btnyesiwillcollectitby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#id/lblanswer2"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/stryesiwillcollectitby"
android:textColor="#color/linkcolor"
android:textSize="#dimen/listviewlocationsize" />
</RelativeLayout>
i have tried the problem with the above code is its not showing the second TextView. where us if i try layout_below - its showing the second text view. can anyone please assists me..
i want something like this - blue textview text to read textview.

Use LinearLayout instead of RelativeLayout and give layout_weight=1 to both the TextView you will get result whatever you want.

If you want to keep using a RelativeLayout, you will have to set the width in dip (instead of wrap_content) for the TextView "lblanswer2", otherwise it will always take the whole place if your text is long.
If using a RelativeLayout is not a necessity, you can follow pratik's answer and implement a LinearLayout, this way your 2 TextViews will share the width equally.

Related

Android: How to align an ImageView to the right of a TextView and make sure the ImageView is always visible

I need to have an Android layout with two views. The first view is a TextView while the second is an ImageView. The ImageView should always be aligned to the right of the TextView. The TextView should be able to expand to fill any remaining space depending on the size of the text. The ImageView should never be hidden by the TextView if the text is too big. The text should be tail truncated in this case.
Here's a visual of what I'm trying to accomplish:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom | center_vertical">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:ellipsize="end"
android:singleLine="true"/>
<ImageView
android:id="#+id/myImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="#drawable/myImageDrawable"
android:layout_toRightOf="#id/myText"/>
</RelativeLayout>
The above XML does not work because TextView hides the ImageView when its text is too big. How can I fix this code? I'm willing to use a different layout as well. Note that the TextView must be a single line.
The below xml is enough to achieve what you want. Just set singleLine to true and use drawableEnd to set your image. Also, replace the text in my code with yours. That's all.
<TextView
android:singleLine="true"
android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
android:layout_width="wrap_content"
android:drawableEnd="#drawable/myImageDrawable"
android:layout_height="wrap_content"/>

Android TextView chopping off text

Having fiddled with this for longer than I care to admit, how does text alignment work within a TextView?
Obviously I'm trying to get the 'N' to centre itself vertically in the TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/titleN"
android:id="#+id/textView"
android:height="114dp"
android:textSize="120dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/textView3"
android:layout_gravity="center"
android:gravity="center"/>
When I remove height it looks like:
The problem is that the TextView's height(114dp) is smaller than the size of the text (120dp) itself, remove the height attribute from the TextView, you don't need this line
android:height="114dp" // you need to remove this attribute from the TextView
What you need is android:gravity="center_vertical"
when the gravity of the textview is set for center_vertical text will remain in the center irrespective of the textView's height
Example:-
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="N"
android:textAppearance="?android:attr/textAppearanceLarge" />
Output
as per #Keshav's link to https://stackoverflow.com/a/6594320/2346980, setting android:layout_marginTop to a negative value corrected the text position inside TextView.
Try using maring attributes, like margin-top or margin-bottom. If that doesnt work try using padding, like padding-top or padding-bottom

Avoid TextView resizing if text is too long

I have a TextView that when it has a long text, it resizes to make the text fit inside, breaking the UI.
Is there a way to use a XML attribute to make the TextView not resizable?
I was thinking about using the TextView inside a ScrollView, is there other options?
For this to work, set specific width to textview (It will display textview without scroll)
android:layout_width=""
If you want to show scroll, add scrollview with specific size and inside scroll layout, add textview like
<ScrollView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/scrollView"
android:fillViewport="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
100dp is dummy size here.
Solution :
<TextView
android:elipsize="marquee"
</TextView>
check out this Answer for more options :
Elipsize Examples
this will slide the textView
Try this code:
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:singleLine="false"
android:text="Long multiline text"/>

Multiline TextView with width "wrap_content"

I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any thought welcome.
EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also add
android:minLines="2"
android:scrollHorizontally="false"
You could try
android:inputType="textMultiLine"
in your TextView XML. This worked for me.
I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.
I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.

Automatically truncate TextView text so as not to overlap another TextView

I have a ListView that displays a bunch of homework assignments. The ListView items use a FrameLayout to position two TextViews. The first TextView is aligned to the left, and the second is aligned to the right. (Both are aligned in the center vertically.) The first displays a snippet of the assignment description, and the second displays the due date.
What I want to do is make it so that the due date takes up as much space as it needs and the description fills up the remaining space, like so:
|----------------------------------------------------|
| Read pgs 15-35, update tim... Fri, May 4|
|----------------------------------------------------|
Right now the description text will continue on to overlap the date. It will truncate at the end of the line though.
Is there anyway I can do this in XML, or do I have to do it in code by shortening the string before I set the TextView value (presumably in my getView call)? If I did it in code, I'd have to calculate the amount of horizontal space the strings would take up figure out how short the description needs to be. That seems like it could get messy...
Any other suggestions on how to accomplish this are greatly appreciated!
Try using the ellipsize attribute like :
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"/>
Note that Android, or at least some versions, require both the "ellipsize" and the "singleline" attributes in order for the system to actually do the truncation and add the ellipsis.
Instead of a FrameLayout, this is the perfect place for a LinearLayout or RelativeLayout in combination with ellipsize:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
...
android:width="0dp"
android:height="wrap_content"
android:layout_weight="1"
android:ellipsize="end" />
<TextView
...
android:width="wrap_content"
android:height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
Or alternately
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
...
android:id="#+id/secondTV"
android:width="wrap_content"
android:height="wrap_content"
android:layout_weight="0" />
<TextView
...
android:width="0dp"
android:height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/secondTV"
android:ellipsize="end"/>
</RelativeLayout>
Change the FrameLayout to a LinearLayout or RelativeLayout.
LinearLayout: Make the due date width "wrap_content" and the description width 0dp, then add layout_weight="1" to the description
RelativeLayout: Layout the due date first with width wrap_content, then layout the description with a rule that it should be to the left of the due date.
Both Anton and JRaymond were pretty much on (JRaymond helped me figure it out with his example). This is what I came up with:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/due_date"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:singleLine="true" />
<TextView android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/due_date"
android:singleLine="true" />
</RelativeLayout>
(I needed to declare my due date label first so that I could reference it in the description. I also just realized that the android:ellipsize seems to be optional -- I guess it defaults to "end".)
Thanks a bunch!

Categories

Resources