0

I want to make a scrollable RelativeLayout where I can create Buttons with a x and y positions and custom width and height. Here is the code i got so far XML:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/buttonDel"
    android:fillViewport="true">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/RL">
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

Java:

RelativeLayout linearLayout;

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

    linearLayout = (RelativeLayout) findViewById(R.id.RL);
    addButton();
}

private void addButton() {

    for(int i = 0; i < 20; i++)
        for(int j = 0; j < 4; j++)
        {
            Button but = new Button(this);
            but.setX(j * 200);
            but.setY(i * 200);
            but.setText("B" + i);
            linearLayout.addView(but, 200, 200);
        }
    }

It result in something that i want except the scrolling part. I don't know why the ScrollView isn't working.

Screen so far.

After I changed ScrollView layout_height="match_parent" to "warp_content"

RamenChef
  • 5,557
  • 11
  • 31
  • 43
Robert
  • 7
  • 3

2 Answers2

1

Change the ScrollView height from "match_parent" to "wrap_content" this should fix the problem:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttonDel"
    android:fillViewport="true"
    >

...
Pooya
  • 6,083
  • 3
  • 23
  • 43
1
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</ScrollView>

However the right way to build the requisite structure is to design a gridview or listview . Use single child for scroll view to behave correctly.

khakishoiab
  • 9,673
  • 2
  • 16
  • 22
  • No luck, is the same result as mine. – Robert Nov 16 '16 at 16:09
  • try wrap content instead and close all tags,definitely works@Robert – khakishoiab Nov 16 '16 at 16:21
  • does your first relative layout specify the coordinates properly,I can see that your relative positions are overlapping,see thread http://stackoverflow.com/questions/6674341/how-to-use-scrollview-in-android to get an idea [official doc here](https://developer.android.com/reference/android/widget/ScrollView.html) – khakishoiab Nov 16 '16 at 16:40
  • also try combinations of wrap content and match parent to get what u want – khakishoiab Nov 16 '16 at 16:47
  • nice good you can further customise it and beautify it,bt keep in mind that relative layouts work irrespective of order and linear one after another like 1,2 etc.cheers – khakishoiab Nov 16 '16 at 16:53
  • The problem is't solved, it was suppose to be 20 buttons rows with a scroll bar but there is no scroll bar, the buttons are there but i cannot get to them. – Robert Nov 16 '16 at 16:58
  • add wrap content to both hieht n width for buttons – khakishoiab Nov 16 '16 at 17:21