Android - TextView height issue according to content (text) - android

I have a RelativeLayout whose layout_height is set to "wrap_content" and inside that I have a TextView whose text is set at runtime. I have set android:layout_height="wrap_content" to my TextView, but it doesn't seems to change the height of the Textview even if the content is larger than 1 line.
It only takes the height of single line and displays 1 line, and all the remaining lines appears to be marquee vertically which is seen when I drag it manually...
So plz anyone can help me out.

<TextView
android:id="#+id/someId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:layout_weight="1"
/>

Related

How to absolute position a text on a button programatically in Android

How can I absolute position a text on a button in android programatically(Defining Dynamically in Activity)
For Ex :
When I reduce the size of a button the text gets shrink ed which results in an additional height , so whether can I make the text (button text) to overflow so that 90% come in a single line even if the 90% comes out of the button it is fine but should get the value or can I add a textView and absolute position it with respect to button how can I achieve either of these dynamically whether is it possible?
Some Sample Code how I got the result
<LinearLayout
android:weightSum="1.0"
android:layout_weight=".75"
android:layout_height="match_parent"
android:layout_width="0dip"
android:background="#ffef1128"
>
<Button
android:id="#+id/textbox12"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:layout_width="0dip"
android:textSize="14sp"
android:text="1%"
android:layout_gravity="center_horizontal"
android:background="#color/fund_background_blue"
android:textColor="#color/gen_text_color"
/>
</LinearLayout>
So the buttons width is 1% and the text I'm trying to display is getting inside the width of the button which makes it invisible as that is the normal behavior of wrapping the content but I want the text to be displayed horizontally even if the text overflows after the button it is ok want to know the best possible solution.
This could help?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="#+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/findme"></ImageButton>
A View's minimum height and minimum with depends on its childView's height and width. thats why when you reduce the size of a button the text gets shrink which results in an additional height. If you want to to shrink it then you have to add a HorizontalScrollView and add TextView inside HorizontalScrollView.
Or you can try-
android:singleLine="true"
and to set absolute position of x you can try to set text view left align to Superview and set left margin of textView. at runtime -
so to set leftalign-
android:layout_alignParentLeft="true"
and to set left margin-
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) textView.getLayoutParams();
// work as x absolute position
rlp.leftMargin = x;

TextView would not horizontally center

My layout is the following:
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/pollenType"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/get_pollen_index"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="51dp"
android:textSize="40sp" />
As you can tell, Philadelphia, PA is left-aligned. No matter what I change, whether it's centering horizontal and vertical to true, it stays left-aligned.
I have tried changing the layout via the XML and graphical user interface. In the graphical user interface, it indeed is "centered", but it remains non-centered. My layout positions are consistent across all my textviews, so I am unsure as to why this particular TextView is not centered.
Two options...
One: Give the TextView android:layout_width="wrap_content"
Two: Keep the android:layout_width="match_parent" and use android:gravity="center_horizontal"
layout_gravity defines gravity of the view within its parent. Your textView already takes the full width because of match_parent, so centering it horizontally inside its parent does nothing.
gravity, on the other hand, defines the gravity of its contents.
You can center the text using the gravity xml attributes :
android:gravity="center"
Your width equals match_parent and therefore the edges of your TextView already touch the edges of the parent view. You should rather center the text in the TextView with android:gravity="center".
You're telling the display_city TextView to be as wide as the screen.
android:layout_width="match_parent"
However, its text is left aligned by default. That's why it looks this way.
You have two alternatives for what you want:
Keep the width as match_parent and center the text inside the TextView (with gravity).
Set the width to wrap_content and center the View inside its parent (with layout_gravity).

TextView with weight is failing to center text horizontally

I have a view with 2 TextViews in a horizontal LinearLayout, each with a weight of 1.
The second TextView was like this:
<TextView
android:id="#+id/info_button"
style="#style/TextBlue"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/bg_ten_percent_black_selector"
android:layout_height="#dimen/card_button_height"
android:text="#string/shop_info"
android:gravity="center"
/>
For some reason, the text would not center horizontally when rendered. However, if I performed a touch on it, it would center then.
I have a fix where I wrap it in an extra LinearLayout which is given the weight, and center the TextView within that, but I was wondering why that workaround is effective when this isn't.
Also, what extra information might be needed here, since this method of centered text usually works. The views are inflated and added as ListView header.
What you are looking for is textAlignment in your case:
android:textAlignment="center"
try with center_vertical and center_horizontal in the textview attribute
android:gravity="center_vertical|center_horizontal"
I suspect you mean layout_gravity, not gravity
See: Gravity and layout_gravity on Android

Layout troubles

I am having a spot of bother with a fairly simple layout. Here it is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
3 Questions:
How do I center vertical both TextViews (or perhaps better said, the text within those views) within the parent LinearLayout? The left view is vertically centered OK, but the right one (because it has a smaller font) is not. It seems to be centered vertically at the top. I obviously tried playing with layout_gravity of the second view but that makes no difference whatsoever. The only way I can solve it is to wrap the second TextView in a LinearLayout with its layout height parameter set to match_parent (but is this the correct way of doing this?)
Similarly, I want the View on the left horizontally centered on the left, and the View on the right horizontally centered on the right. Currently the right View is placed immediatly next to the left one. The only way I can solve this is by adding something like this inbetween the two text views:
<TextView
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="">
basically acting as a spacer which decreases in size depending on the legth of the text in both TextViews
I want the text in the left View to be truncated if the combined text of the Views does not fit horizontally. No wrapping onto a new line. Currently the text in the left View simply "pushes" the right one out of the parent. No idea how to achieve that (apart from adding android:maxLines="1" to stop the text from wrapping). I have tried android:ellipsize="end" but that does not seem to have any effect.
Best way is to use Relative layout , but still if you want to do the same thing in Linear layout than do some changes in your xml file
-First is set Linear layout hight as match parent :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizonatal">
-Second for making the views visible at centre vertical do
android:layout_gravity="center_vertical"
same property android:layout_gravity = "center_horizontal" , you have to add in second text view also.
It will make your both text view appear at centre vertical but one next to other.
To make the second view appear on right I think you can add
android:layout_marginLeft="xx dp"
put some value in place of xx.
For your third question about truncating your text, you should give some size to your TextView not wrap content..Like android:layout_width ="25dp"
and then use android:ellipsize="end".
I guess you will get that..Actually I am in hurry,time to leave the office.

Layout of type "<fill_parent><wrap_content>"

Not sure it is clear from this question title what I mean, so here are details.
What I'd like to achive is simple layout with two TextView controls aligned horizontally one after another. First TextView could probably contain several lines of text, second TextView will contain only 1 number.
Second TextView should always have widht as much as it is required to show its content, aligned to the right and height should be equal to height of first TextView so I can show data vertically centered. First TextView should take remaining space (for width) and stretch vertically as needed.
Example 1:
---------------------------------
|Small Text. 123|
---------------------------------
Example 2:
---------------------------------
|Long Text starts here .... |
|... continues here ....... 123|
|... and finishes here .... |
---------------------------------
LinearLayout with horizontal orientation is not good here since it places controls from left to right. While in this case I need first set wrap_content to second TextView and then fill_parent to first TextView.
Playing with RelativeLayout I also could not get described layout.
I could not use layout_weight approach here since I do not know length of data in second TextView: in one case it could be 1 character, in another - 8 characters. So in 1 character case I will have unused space.
So, do I have chance to build such layout?
A layout like the following should do the trick for you:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="false"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical|right"
android:text="TextView" />
</LinearLayout>
here will be useful to use the minWidth and maxWidth properties.

Categories

Resources