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
-
i think you set listview's weight – Sardor Dushamov Feb 23 '13 at 06:01
-
no that isn't what i want. Because my listview will be initially out of screen and appears only when a button is clicked – user2041902 Feb 23 '13 at 06:04
-
You should restate your question and specify that you want to add the list view during run time, cause this differs a lot from what you initially asked. – Daniel Conde Marin Feb 23 '13 at 06:15
-
Sardor is right you can use "weight" to specify the width, and for requirement "my listview will be initially out of screen and appears only when a button is clicked": Initially you can keep you listView visibility to GONE and on Button click change it to "VISIBLE" – Rekha Feb 23 '13 at 06:31
-
The list view is there initially itself. It is kept with visiblitiy set as Gone when i press a button in my visible layout the entire visible layout should move out partially and the listview appears in that portion – user2041902 Feb 23 '13 at 06:34
4 Answers
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) to0dp
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.

- 2,328
- 1
- 15
- 12
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.

- 37,851
- 12
- 116
- 113
-
i tried setting but there seems to be a problem as the view cant be seen if the params are set programmatically. I am creating the view in xml itself and trying to change the params in xml .... can u post the code you used to set the params – user2041902 Feb 23 '13 at 06:31
-
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>

- 2,271
- 5
- 32
- 52
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

- 7,588
- 4
- 35
- 44