When making the android application layouts we have to define the layout width , what is the meaning of the android:layout_width="wrap_content" ?
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
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.
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.
see the official docs for more details!
android:layout_width="wrap_content" means that the layout is fixed it expand up to exactly as your content size .If your content is more it expand up to that limit and same as in short content.
Here is an example..
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button ABC"/>
</RelativeLayout>
wrap_content Image:
wrap_content means that width is determined by the width of the widgets, respectively not the whole display' width but only how widgets need.
Referring to the official Android documentation:
android:layout_width
Specifies the basic width of the view. This is a required attribute
for any view inside of a containing layout manager. Its value may be a
dimension (such as "12dip") for a constant width or one of the special
constants. May be a dimension value, which is a floating point number
appended with a unit such as "14.5sp". Available units are: px
(pixels), dp (density-independent pixels), sp (scaled pixels based on
preferred font size), in (inches), mm (millimeters). This may also be
a reference to a resource (in the form "#[package:]type:name") or
theme attribute (in the form "?[package:][type:]name") containing a
value of this type. May be one of the following constant values.
fill_parent -1 The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
match_parent -1 The view should be as big as its parent (minus padding). Introduced in API Level 8.
wrap_content -2 The view should be only big enough to enclose its content (plus padding).
android:layout_width="wrap_content"
means that layout width is not determined or fixed, it will take space according to its components.
Suppose in a textField u have only one word then your textField will take space for a word only and then it will take space for two word if you have two word in your textField when you will declare your width as wrap_content.
Same thing is applicable for layout_height also.
Related
I was following an Android tutorial about RelativeLayout which just made a basic layout of two EditTexts (first name and last name fields) and a Button(not important). The first name EditText view had a layout_width of 0dp, but was still visible on the screen. I messed around with the width value and it seemed that no matter what value I gave it, the size of the EditText stayed the same. Why is that? Does it have something to do with the layout_toLeftOf statement? Below are links to the tutorial along with a picture of my xml code and the resulting output.
Tutorial Link: http://tinyurl.com/jjnfw7z
The width of the EditText with id first_name is completely specified by these two constraints:
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/last_name"
android:layout_alignParentLeft="true" implies that the left edge of this EditText must match the left edge of the parent view.
android:layout_toLeftOf="#+id/last_name" implies that the right edge of this EditText must match the left edge of the sibling view with id last_name.
By design, these take priority over any specified layout_width value, which is why you can change that dp width with no visible effect. It is common to set the width to 0dp in such cases; since the layout_width attribute is required, a value of zero is the clearest possible indicator that the view's width is being determined elsewhere (by other attributes; in code, etc.)
I have the following requirement in Android:
Given a horizontal LinearLayout, I have five buttons in it. All the buttons should have equal width and height. The height of each button is same as their parent LinearLayout and spacing between them should remain constant. The height of LinearLayout is not constant and depends on form factor and other layouts sizing. Therefore, I can not assign fixed with/height to each button.
While I can easily achieve that very easily in iOS with the help of constraints, I am not sure how to achieve this in Android at design time. Is there some way to achieve this or is it possible programatically only?
The height of each button is same as their parent LinearLayout
Set the height to match_parent
For your width, you'll have to calculate the screen size programmatically and set the widths accordingly. See this question.
Try this . As you have a horizontal LinearLayout with 5 buttons, for equal spacing of all buttons, you must first allocate the space to each button of of the total space available like this..
<LinearLayout
android:weightSum="5"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="allocate it programatically"
/>
This button shown above has to be for all 5 buttons u have. The idea allocating weight sum of 5 to LinearLayout and then dividing it as 1 to each button. Note the width here for the button should be 0dp.
Then dynamically change your button Height and Width per your requirement
Unfortunately no typical layout in Android seems to have the concept of this kind of layout constraint (square like elements).
It's the layout that ultimately determines the sizes of their children. Therefore if you want that you have to write your own layout implementing this constraint.
For this extend ViewGroup and in onLayout enforce that width of the children or a specific children equals height. You could even invent your own LayoutParams for this task. See the documentation of ViewGroupfor a general example. The exact implementation very much depends on which other requirements you have for your layout.
I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.
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
I want to move all the contents of my page down by about 10 pixels. I have tried to select all and drag down but the layout gets all messy. This is due to the relative layout. Is it possible to just shift everything down by a bit without ruining the layout? Thanks
You can use the android:paddingTop attribute of the RelativeLayout. It accepts values in px, dp, and sp (so you're looking at android:paddingTop="10px" though the convention is to use dp for "density-independent pixels"). If it doesn't work, you can try adding another View at the top of your layout whose android:layout_height is 10 pixels.