Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the width of linear layout in android

I am having this xml layout for a list view :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu"
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent">

  <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none">
  </ListView>
</LinearLayout>

The linear layout occupies the full screen width by default.

enter image description here

I want to change it programatically.I want to make something like this :

enter image description here

But when I change width of linearlayout nothing happens,it still occupies full screen width

 LayoutInflater inflater = LayoutInflater.from(this);
 View menu = inflater.inflate(R.layout.xml_layout, null);
 menu.setLayoutParams(new LayoutParams(20,LayoutParams.WRAP_CONTENT));

after hardcoding layout width in list view as :

 <ListView
  android:id="@+id/list"
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:background="#2f4f4f"
  android:cacheColorHint="#2f4f4f"
  android:scrollbars="none" >

</ListView>

I get this :

enter image description here

Please suggest a way to do this.

like image 415
vishesh Avatar asked Dec 29 '12 04:12

vishesh


1 Answers

make changes in listview like shown below, do hard code for layout width in XML.

<?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="vertical" android:background="@android:color/white">


<ListView
    android:id="@+id/list"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="#2f4f4f"   >

</ListView>

</LinearLayout>

you were doing Wrap_content in linear layout and in listview fill_parent, therefore linearlayout which is wrapping listview i.e. full screen.

like image 157
Rahul Baradia Avatar answered Sep 19 '22 03:09

Rahul Baradia