Palak
Palak

Reputation: 2215

How can i set Listview bottom end dynamically

I want to develop functionality like this

enter image description here

When any item is added then bottom RelativeLayout will VISIBLE If none of item is selected then it GONE

Problem : I have problem like this : enter image description here

Image 1 : Code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="50dp"
    android:background="@android:color/white" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"  >
    </ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true" >

        <View
            android:id="@+id/sprator"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/black" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_margin="10dp"
            android:src="@drawable/icon" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:src="@drawable/icon" />
    </RelativeLayout>

</RelativeLayout>

When none of item is selected then bottom RelativeLayout will GONE like Image 2

When any item is added then bottom RelativeLayout will VISIBLE like Image 3

Upvotes: 0

Views: 73

Answers (5)

Shailesh Lakum
Shailesh Lakum

Reputation: 137

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/productList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <RelativeLayout
        android:id="@+id/relLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_alignParentBottom="true" >
        <View
            android:id="@+id/sprator"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/black" />
        <ImageView
            android:id="@+id/icoShopping"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_margin="10dp"
            android:src="@drawable/icon" />

        <ImageView
            android:id="@+id/icoCheckout"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:src="@drawable/icon" />
    </RelativeLayout>
</LinearLayout>

Upvotes: 1

Kelo
Kelo

Reputation: 463

Add layout_above attribute to the list. In xml it has to be below the bottom layout, because otherwise the id you give to that layout (resultLayout in my example) isn't declared.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp"
android:background="@android:color/white" >

<RelativeLayout
    android:id="@+id/confirmLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true" >

    <View
        android:id="@+id/sprator"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/black" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="10dp"
        android:src="@drawable/icon" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:src="@drawable/icon" />
</RelativeLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/confirmLayout"  >
</ListView>
</RelativeLayout>

Upvotes: 2

JM Lord
JM Lord

Reputation: 1197

First, give an id to your inner relative layout.

Then, use setOnItemClickListener to capture the click events.

Find the layout in your activity code, using :

RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_id);

You can then change the visibility with :

layout.setVisibility(RelativeLayout.VISIBLE);

Upvotes: 0

sonic
sonic

Reputation: 1904

Place the ListView above the RelativeLayout

Implement OnItemClickListener in your activity. Then when a user select ro deselect an item change the visibility of the RelativeLayout to View.VSIBLE or View.GONE

Upvotes: 0

Budius
Budius

Reputation: 39836

change it to LinearLayout, like following pseudo XML:

<LinearLayout>
   <ListView layout_weight="1" layout_height="0dp" />
   <RelativeLayout layout_height="wrap_content" /> // bottom layout
</LinearLayout>

Upvotes: 1

Related Questions