Android change width of view programmatically - android

I want to set the width of my listView to 40% of the mobile screen width. Since 40% will differ in terms of pixels in different mobiles I won't be able to set the width in the xml layout file. Is there any way to set the width programmatically for a listView

Actually you can usually do this in XML using layout_weight. For a description see the Linear Layout guide. A couple of things to keep in mind:
Make sure you set the size, (layout_width for a horizontal layout, layout_height for a vertical one) to 0dp so it doesn't interfere with the dynamic layout.
You would usually have weights on other views in the layout so they take up the remaining 60%. If not, though, you can define a weight sum.

Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth()*40)/100;
You have to get screen width size through above code after getting width u have to set into your code through setparam.

You have an error of concept in terms of views and its params. The different kinds of widths you can set programatically you can set it too in your xml layout. The advantage of setting layout params to views programatically is just the possibility of changing the views layout params during runtime.
The solution to your problem is easy, you just have to put your listView inside a LinearLayout and set its weight to .4

You can get screen width programmatically and then get 40% of screen width and set to to ListView.And if you wanna do this using xml then use LinearLayout and set android:weightSum for this and then use weight.Using this way you can do this easily.
To get 40% of screen weight
Display display= ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = (display.getWidth()*40)/100;
set weight for views in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:text="button1"/>
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:text="button2"/>
</LinearLayout>

Related

android- meaning of layout_weight="0"

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.

Equal width and height layout constraint

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.

Make TextView that has enough height to show all content?

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

android:layout_width along with layout weight usage

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

What's the difference between fill_parent and wrap_content?

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

Categories

Resources