Suman’s Blog

Android Application Development

  • February 2009
    M T W T F S S
     1
    2345678
    9101112131415
    16171819202122
    232425262728  
  • Categories

  • Archives

  • Blog Stats

    • 45,471 hits
  • Top Posts

Archive for February, 2009

Dynamic List Between Two Static Views

Posted by Suman on February 7, 2009

I love Android

How to show a dynamic ListView between two static views?

One day I was trying to show top and bottom menu (requirement – both should be visible all the time) on the screen and a dynamic ListView in middle.

Here is what I did and it’s sample code.

main.xml contains top menu, placeholder for list and bottom menu. This placeholder is used to populate the list items.

main.xml

<?xml version=“1.0” encoding=“utf-8”?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:orientation=“vertical”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent” >


<!– top menu –>

<LinearLayoutandroid:id=“@+id/top_menu”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content” >

<TextView android:id=“@+id/top”

android:layout_width=“fill_parent”

android:layout_height=“40px”

android:background=“@drawable/red”

android:text=“top static view” />

</LinearLayout>

<!– placeholder for list –>
<ListView android:id=“@android:id/list”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_weight=“1” />

<!– bottom menu –>

<LinearLayout android:id=“@+id/bottom_meunun”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content” >

<TextView android:id=“@+id/bottom”

android:layout_width=“fill_parent”

android:layout_height=“40px”

android:background=“@drawable/yellow”

android:text=“bottom static view” />

</LinearLayout>

</LinearLayout>

To achieve our objective most important attribute is weight. So let us talk about weight attribute. Weight attribute allows the widget to fill the remaining space in parent. LinearLayout supports the weight attribute. Default value for weight is 0. In above example, ListView has an attribute layout_weight=“1”. That’s why top menu will take 40px, bottom menu will take 40px and the middle Listview will take the remaining space. But if ListView and bottom menu both have weight 1, then each of them will expand equally in the remaining space but the top menu will take only top 40px.

Now suppose I have a list with 20 items and each item’s height is 30px. When I populate this list in above ListView, it will be shown in a ScrollView.

Screen Shot

If I will reduce the number of items in list to 3, even then the ListView will take whole middle space and both menu remains well placed.

Screen Shot

This concludes our solution for dynamic list with top bottom menu. Please post your comment below.

Posted in Android | Tagged: , , , , , , , | Leave a Comment »