I have custom row for data representation but in first text when text is long it is in two lines but second line is half cut by height (like text view doesn't wrap content)
<TextView
android:id="#+id/txtDate"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="3"
style="#style/row"
android:layout_weight="1"/>
<TextView
android:id="#+id/txtTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
style="#style/row"
android:layout_weight="1"/>
<Button
android:id="#+id/imbDetail"
android:layout_width="36dip"
android:layout_height="36dip"
android:background="#drawable/selector_detail_arrow" />
How to make that has height enough to show all content ?
your height is wrap content which means it'll wrap them to give the shortest view of text. Use fill_parent to display everything.
android:layout_height="fill_parent"
Either attribute can be applied to View's (visual control) horizontal
or vertical size. It's used to set a View or Layouts size based on
either it's contents or the size of it's parent layout rather than
explicitly specifying a dimension.
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and
higher)
Setting the layout of a widget to fill_parent will force it to expand
to take up as much space as is available within the layout element
it's been placed in. It's roughly equivalent of setting the dockstyle
of a Windows Form Control to Fill.
Setting a top level layout or control to fill_parent will force it to
take up the whole screen.
wrap_content
Setting a View's size to wrap_content will force it to expand only far
enough to contain the values (or child controls) it contains. For
controls -- like text boxes (TextView) or images (ImageView) -- this
will wrap the text or image being shown. For layout elements it will
resize the layout to fit the controls / layouts added as its children.
It's roughly the equivalent of setting a Windows Form Control's
Autosize property to True.
check this answer: Here
Related
I want the height of my TextView to auto-size its layout_height when max/min text size is hit.
Problem: In order to auto-size my text, I am forced to add a layout_height. There are cases where the text is one or two words and cases where there are many words. When there are only one or two words my text hits the autoSizeMaxTextSize but the layout_height remains at 200dp and then my view looks like there is a big space between my TextView (the title) and the lower view (the body) because I set a static height for layout_height. When I have less text I do not want my TextView's height to take up the full 200dp but to scale down to the minHeight instead.
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:ellipsize="end"
android:gravity="start"
android:maxLength="70"
android:maxLines="4"
android:text="#{title.trim()}"
android:textAlignment="textStart"
android:textColor="?android:textColorPrimary"
android:textStyle="bold"
android:textSize="42sp"
app:autoSizeMaxTextSize="42sp"
app:autoSizeMinTextSize="8sp"
app:autoSizeTextType="uniform"/>
I have tried using android:layout_height="wrap_content" in conjunction with android:minHeight="100dp" and android:maxHeight="200dp" and the auto text resizing. The result was to wrap content with no text resizing and no maxHeight.
Does anyone know how to accomplish this?
Because when the view's parent lays it out and figures out how much space to give it, it doesn't know about those fields. It only knows about the layout_ fields. It sizes it completely based on that and on the layout rules of the parent. min and max text size is only used by the view itself when deciding what font size to use.
Ah, you asked a different question in the body. If you want the size of the text view to change based on length of text, use wrap_content for height. To put a max height, put it inside a view with layout_height 200, or use contraint rules in a ConstraintLayout to set a max height.
I have a layout (can be relative, linear or constraint)
with TextView aligned to parent left and then ImageView (fix width) aligned that start right to the textView.
I want the image to be rendered first and only then to render the text view.
Meaning I want the text view to be truncated according to the left space after the image was rendered.
<RelativeLayout
android:id="#+id/account_name_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="#dimen/account_menu_account_name_layout_bottom_margin">
<TextView
android:id="#+id/account_name"
style="#style/AccountDataAccountName"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:lines="1"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
tools:text="emailisverylongaswellwewantittogettruncated#gmail.longdomain.com"/>
<ImageView
android:id="#+id/account_name_chevron"
android:layout_width="#dimen/account_menu_chevron_size"
android:minWidth="#dimen/account_menu_chevron_size"
android:layout_height="#dimen/account_menu_chevron_size"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/account_name"
android:layout_marginTop="#dimen/account_menu_chevron_top_margin"
android:layout_marginLeft="#dimen/account_menu_chevron_left_margin"/>
</RelativeLayout>
I have tried few options:
1) telling the text to be left to the image view
2) putting weight on the text view - made gap between the two elements.
3) setting minWidth to the image view - didn't help just made the imageView scale smaller.
Any idea how to render the image first and how to limit textView's width according to left width?
You can force the width on the imageView. That will prevent the textview from pushing it off the space. If you are saying you did this, please post the resulting image as that wouldn't make any sense.
Your above example has no constraints to each other, no enforcement to not overlay or push off. You need some constraints, such as "toTheLeftOf" or "Weight" or LinearLayout to enforce it as Weight only works in LinearLayout.
The easiest way is to just give the imageView a hard coded DP width and height, then set the text to 0 width with a weight of 1 inside a Linear Layout.
You can also use percentages if you want, use a LinearLayout then put a weight sum of like 100 for example (representing 100%). Then assign your image whatever percentage it needs like layout_weight=30 and give the textview 70.
Any of these options will work fine for you. If you try it, and it does not, then post your tried code as it will work unless you are doing something goofy that is not visible in your current example. As I do this all the time, every time you make a row, you typically have an image on the left fixed and text on the right to grow.
I am using layout_weight to specify the ratios of various views in a specific viewGroups in android.
From this question's answer I have got clear concept about what layout_weight is. I calculate the size of all viewsin a viewGroup using normal mathematics (i.e I have 3 views of 1,2 & 3 layout_weights & all of them have layout_height="0dp" then they have 1/(1+2+3), 2/(1+2+3), 3/(1+2+3) spaces in their viewGroup for Verical alignment).
But, what does layout_weight="0" mean? How can I determine the view's size having layout_weight="0"?
For all the view which have layout_weight must have layout_height or layout_width as 0dp depending on the orientation and requirement of layout.
layout_weight="1" and layout_width="0dp" ==> that particular view will be stretched horizontally if there is not other layout adjacent to it.
layout_weight="0" and layout_width="100dp" ==> that particular layout will behave as it is there is no meaning of layout_weight in this scenario.
Best use of weight is when you need two views having same height/width that are adjacent to each other you can add width/height as "0dp" for both layout and weight as "1" for both the layout.
layout_weight = "0" no mean in xml there should be android:layout_width="0dp" so time if you want to provide same space to all control in Linarlayout orientation we use this e.g:- if we want to take 3 button in horizontally we use below code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"/>
</LinearLayout>`
So here we put
android:weightSum="1"
And do equal parts in control's weight. In all devices, it'll show in a proper manner except Imageview.
For all the view which have layout_weight must have layout_height or
layout_width as 0dp depending on the orientation and requirement of
layout.
That's not correct. Firstly, "layout_width" and "layout_height" parameters are applied and views will be at least this size. Secondly, remaining space in the ViewGroup will be divided among views proportionally depending on their weight. So weight "0" means that view will not be given some additional size during that phase.
why should i put android:layout_width="0px" when i use android:layout_weight property? For example following is my main.xml file , and in that i used android:layout_width="wrap_content", and everything works fine, so why android:layout_width="0px" should be used when i am using the layout_weight property?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText" android:hint="enter your name"
android:layout_weight="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/button"
android:layout_weight="0"/>
</LinearLayout>
and this is how my layout looks:
You certainly don't have to. Additionally weight=0 doesn't make much sense. The weight parameter controls what part of the remaining space in the layout the widget occupies. So setting width=0 effectively tells it to take up only the remaining space. If you set width=30, it will occupy 30 px|dp + all the remaining space. Setting 0 as the width makes it easier to get a predictable result on different screen sizes.
A common pattern is to have two widgets with width=0 and equal weight to make them equally sized inside the parent container, where you don't care about the actual size (width or height).
Layout weight itself is used to give appropriate width as per weight property.
Check this
This attribute assigns an "importance" value to a view in terms of how
much space is should occupy on the screen. A larger weight value
allows it to expand to fill any remaining space in the parent view.
So eclipse suggests to give width as 0px
layout_weight you can specify a size ratio between multiple views.
E.g. you have a Tabelview and a image which should show some additional information to the layout. The tabel should use 3/4 of the screen and image should use 1/4 of the screen. Then you will set the layout_weight of the tabelview to 3 and the layout_weight of the image to 1.
To get it work you also have to set the height or widthto 0px.
http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
In Android, when layout out widgets, what's the difference between fill_parent (match_parent in API Level 8 and higher) and wrap_content?
Is there any documentation where you can point to? I'm interested in understanding it very well.
Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher)
Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.
Setting a top level layout or control to fill_parent will force it to take up the whole screen.
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.
Online Documentation
There's some details in the Android code documentation here.
fill_parent (deprecated) = match_parent
The border of the child view expands to match the border of the parent view.
wrap_content
The border of the child view wraps snugly around its own content.
Here are some images to make things more clear. The green and red are TextViews. The white is a LinearLayout showing through.
Every View (a TextView, an ImageView, a Button, etc.) needs to set the width and the height of the view. In the xml layout file, that might look like this:
android:layout_width="wrap_content"
android:layout_height="match_parent"
Besides setting the width and height to match_parent or wrap_content, you could also set them to some absolute value:
android:layout_width="100dp"
android:layout_height="200dp"
Generally that is not as good, though, because it is not as flexible for different sized devices. After you have understood wrap_content and match_parent, the next thing to learn is layout_weight.
See also
What does android:layout_weight mean?
Difference between a View's Padding and Margin
Gravity vs layout_gravity
XML for above images
Vertical LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=wrap height=wrap"
android:background="#c5e1b0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=match height=wrap"
android:background="#f6c0c0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="width=match height=match"
android:background="#c5e1b0"/>
</LinearLayout>
Horizontal LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WrapWrap"
android:background="#c5e1b0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WrapMatch"
android:background="#f6c0c0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="MatchMatch"
android:background="#c5e1b0"/>
</LinearLayout>
Note
The explanation in this answer assumes there is no margin or padding. But even if there is, the basic concept is still the same. The view border/spacing is just adjusted by the value of the margin or padding.
fill_parent will make the width or height of the element to be as
large as the parent element, in other words, the container.
wrap_content will make the width or height be as large as needed to
contain the elements within it.
Click here for ANDROID DOC Reference
fill_parent :
A component is arranged layout for the fill_parent will be mandatory to expand to fill the layout unit members, as much as possible in the space. This is consistent with the dockstyle property of the Windows control. A top set layout or control to fill_parent will force it to take up the entire screen.
wrap_content
Set up a view of the size of wrap_content will be forced to view is expanded to show all the content. The TextView and ImageView controls, for example, is set to wrap_content will display its entire internal text and image. Layout elements will change the size according to the content. Set up a view of the size of Autosize attribute wrap_content roughly equivalent to set a Windows control for True.
For details Please Check out this link : http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html