Question

In my app I have a HorizontalScrollView that get ImageButtons added dynamically to it. I'd like to add some kind of margin to the right side of the scrollview or some way to "scroll past" for lack of a better word, the added items by ~100dp. I've tried adjusting margins and padding of the scrollview and its child layout to no avail.

I made a quick mockup that simulates what I want by using a blank View to push the scrollview farther out, which I guess would work, only it would be a pain to deal with.

As always I'm very grateful for any suggestions.

Activity:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends Activity {

    LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.panel);
        LayoutParams params = ll.getLayoutParams();
        params.width = 100;
    }

    public void addClicked(View view){
        Button btn1 = new Button(this);
        btn1.setText("Button");
        ll.addView(btn1, ll.getChildCount()-1);
    }

}

main xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="15" >

        <LinearLayout
            android:id="@+id/panel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <View
                android:id="@+id/view1"
                android:layout_width="100dp"
                android:layout_height="40dp" />

        </LinearLayout>
    </HorizontalScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="addClicked"
        android:text="Add" />

</LinearLayout>
Was it helpful?

Solution

You need to set a right padding on the inner panel:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="15" >

        <LinearLayout
            android:id="@+id/panel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="1000dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>
    </HorizontalScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="addClicked"
        android:text="Add" />

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top