1

Hi I have a ScrollView and a vertical LinearLayout inside that. Inside each LinearLayout slot I have a horizonal LinearLayout that holds 2 things a word and a number. The problem is some of the words are hidden?? and it takes up half the screen. Thanks for any help.

Layout bounds

Problem


for (int i = 0; i < words.size(); i++) {
    LinearLayout horizontal = new LinearLayout(context);
    horizontal.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    horizontal.setLayoutParams(LLParams);

    btnWord.add(i, new Button(context));
    btnWord.get(i).setHeight(60);
    btnWord.get(i).setWidth(120);
    btnWord.get(i).setTypeface(montFont);
    btnWord.get(i).setBackgroundColor(Color.WHITE);
    btnWord.get(i).setTag(i);
    btnWord.get(i).setGravity(Gravity.CENTER);
    btnWord.get(i).setText("  " + words.get(i));
    btnWord.get(i).setOnClickListener(btnClicked);
    horizontal.addView(btnWord.get(i));

    wordWeight.add(i, new Button(context));
    wordWeight.get(i).setHeight(60);
    wordWeight.get(i).setWidth(40);
    wordWeight.get(i).setTypeface(montFont);
    wordWeight.get(i).setBackgroundColor(Color.WHITE);
    wordWeight.get(i).setTag(i);
    wordWeight.get(i).setGravity(Gravity.CENTER);
    wordWeight.get(i).setText("  " + wordWeights.get(i));
    wordWeight.get(i).setOnClickListener(btnClicked);
    horizontal.addView(wordWeight.get(i));

    linearLayout.addView(horizontal);
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


    <ScrollView
        android:layout_width="match_parent"
        style="@android:style/Widget.ScrollView"
        android:layout_marginTop="106dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout" />

    </ScrollView>
</RelativeLayout>

1 Answers1

2

You could try to set the weight of each "item" to 1 so they will equally divide the space on the screen. Something like the snippet below:

LinearLayout ll;
LinearLayout.LayoutParams lp;
lp.weight = 1;
ll.setLayoutParams(lp);

You could also ditch that logic and use a ListView with a custom adapter like I did on this answer, or setup a RecyclerView as you can see on this blog post. It's way easier and more efficient to do either one of those.

More on ListView vs. RecyclerView here.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • 3
    This is the "correct" answer to the incorrect solution the OP is using. :) Using Weights and creating this list programmatically is less efficient and harder to maintain than dropping a simple ListView or RecyclerView. But +1 for fixing the problem :) – Martin Marconcini Mar 03 '17 at 19:14