blackbox
blackbox

Reputation: 1

ScrollView is not working properly with the ImageButtons

I want to have several ImageButtons in my homepage so it is supposed to be in a ScrollView to allow users scroll through all of them. However, it is not working as expected.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    tools:context=".Activities.HomeFragment">

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

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/lunchimg" />

        <ImageButton
            android:id="@+id/lunch"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/lunchcate" />

        <ImageButton
            android:id="@+id/dinner"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/healthyfood" />

    </LinearLayout>
</ScrollView>

when I run the code, only the first one button appears and the others are behind it for some reason.

Upvotes: 0

Views: 54

Answers (3)

MMG
MMG

Reputation: 3268

This code is your response:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="150dp">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button One" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Two" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Three" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Four" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Five" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Six" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Seven" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Eight" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Nine" />
    </LinearLayout>
</HorizontalScrollView>

Upvotes: 0

MMG
MMG

Reputation: 3268

You need to use recyclerview not scroll. Scroll is used when you want your screen to be fit with different devices.

Upvotes: 0

Deeksha
Deeksha

Reputation: 576

Please give orientation to LinearLayout. If you are not giving any orientation then by default it will take its value as "horizontal".

So that's why you are not able to see your ImageButtons.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.HomeFragment">

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

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/lunchimg" />

    <ImageButton
        android:id="@+id/lunch"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/lunchcate" />

    <ImageButton
        android:id="@+id/dinner"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/healthyfood" />
</LinearLayout>


 </ScrollView>

Hope this will work

Upvotes: 1

Related Questions