vishesh
vishesh

Reputation: 2045

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.

Upvotes: 5

Views: 2503

Answers (2)

Don
Don

Reputation: 526

If you'd like it to fit multiple screen sizes I'd suggest adding an empty layout and using weights.

Like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="horizontal" android:background="@android:color/white">


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

</ListView>

<LinearLayout android:layout_width="0dp"
android:layout_height="fill_parent" 
android:layout_weight="1"
android:orientation="horizontal" android:background="@android:color/white">

</LinearLayout>

I just eyeballed the weights. Adjust them to the width that you need. If you do it this way then you won't have to hard code the size for every screen size.

Upvotes: 2

Rahul Baradia
Rahul Baradia

Reputation: 11951

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.

Upvotes: 4

Related Questions