Robin
Robin

Reputation: 372

How can I make a button move with a horizontal scroll bar?

I have a picture of a map in the background with a button on the bottom and I want to make this scroll with the horizontal scroll bar. It is aligned to the right of the screen at the start and I would like to keep it there. Is this possible?

<?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:id="@+id/mapBackground">
<HorizontalScrollView android:id="@+id/horizontalScrollView1"
    android:layout_height="fill_parent"
    android:scrollbars="none" android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1"
    android:background="@drawable/large_map" android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom">
<LinearLayout android:id="@+id/linearLayout4" android:layout_width="fill_parent"
    android:layout_height="400dp">
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout5"
   android:layout_width="320dp" android:gravity="right">
<Button android:background="@drawable/bt_play_circle"
    android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/playButton"></Button>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

Upvotes: 0

Views: 1966

Answers (1)

Daniel Novak
Daniel Novak

Reputation: 2756

You want the button to stay on the right side over the map while you are scrolling? Place the button outside the HorizontalScrollView. You could for example use RelativeLayout instead of the top LinearLayout.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
    <HorizontalScrollView>
    ...
    </HorizontalScrollView>
    <Button 
    android:id="@+id/playButton"
    android:background="@drawable/bt_play_circle"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    />

</RelativeLayout>

Upvotes: 1

Related Questions