Andrew
Andrew

Reputation: 227

Button over a ListView

How is it possible to place a Button over a ListView? Like this image?

I have tried that:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:text="Button" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <ListView 
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    </ListView>
    </LinearLayout>


</FrameLayout>

But the Button is inaccessible... Both the ListView and the Button must be accessible.

Upvotes: 0

Views: 10249

Answers (6)

Arjun Singh
Arjun Singh

Reputation: 724

Just copy paste this. Tested solution.

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="3">

<LinearLayout
    android:id="@+id/linearLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
 >

    <Button
        android:text="Status"
        android:textSize="20sp"
        android:textStyle="bold"
        style="?android:attr/buttonBarButtonStyle"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textColor="@color/black"
        android:layout_height="wrap_content"
        android:id="@+id/textView10" />

    <Button
        android:text="Date"
        android:textSize="20sp"
        android:textStyle="bold"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textColor="@color/black"
        android:layout_height="wrap_content"
        android:id="@+id/textView6"
        />

    <Button
        android:text="Subject"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        style="?android:attr/buttonBarButtonStyle"
        android:textColor="@color/black"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/textView9"

        />

</LinearLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/linearLayout"
/>

  </RelativeLayout>

Upvotes: 0

Adeel Pervaiz
Adeel Pervaiz

Reputation: 1356

Use Relative Layout, and then you can set your button position by gravity use this example add layout_align with different values, will do the trick for you. .....

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:text="Button" />

<ListView 
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</RelativeLayout>
......

Written Below are the margin properties you can use in your button code.

layout_alignParentBottom
layout_alignParentLeft 
layout_alignParentRight
layout_alignParentTop
layout_alignRight 
android:layout_alignTop 

Upvotes: 0

0gravity
0gravity

Reputation: 2762

Add android:focusable="false" to your button attribute and see if that helps.

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Button" />

Upvotes: 0

Doğancan Kefeli
Doğancan Kefeli

Reputation: 1

Here sample code for how to using relative layout with listview and it shows like your image.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/listView1"
        android:layout_alignParentRight="true"
        android:text="Button" />

</RelativeLayout>

Upvotes: 0

Cat
Cat

Reputation: 67522

Firstly, you should be using RelativeLayout; FrameLayout is used to hold a single child element optimally.

Second, the layout is drawn in order from top to bottom -> back to front. Therefore you want your Button below it in the XML file.

Lastly, instead of gravity you should be using align.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <ListView 
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </ListView>
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Button" />

</RelativeLayout>

Upvotes: 13

Michael
Michael

Reputation: 3332

Use a RelativeLayout and use either android:layout_below="@+id/yourIdHere" or android:layout_above="@+id/yourIdHere"

Upvotes: 0

Related Questions