1

I have what I believe to be a simple LinearLayout for an activity:

<LinearLayout 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="com.example.mille.shithead.firstPlay"
android:orientation="vertical">

<TextView
    android:id="@+id/fp_TextView_mainText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textStyle="bold"
    android:fontFamily="cursive"
    android:textSize="30sp"
    android:visibility="invisible"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
        android:id="@+id/fp_SV_placeHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>

</ScrollView>
<Button
    android:id="@+id/fp_nextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    android:textAlignment="center"
    android:visibility="visible"
    android:text="Let's Play!"/>

Inside of fp_SV_placeholder I inflate as many copies of the following layout as I need, depending on how many players are in the current instance of the game:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal|center_vertical">
<TextView
    android:id="@+id/fp_TV_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textStyle="bold"
    android:fontFamily="cursive"
    android:textSize="15sp"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal|center_vertical">
    <ImageView
        android:id="@+id/fp_IV_tableUp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />

    <ImageView
        android:id="@+id/fp_IV_tableUp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />

    <ImageView
        android:id="@+id/fp_IV_tableUp3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />
</LinearLayout>

This works as intended with my code as written, and the proper displays appear with the proper card images as expected. However, if I have more than two players in my game, the button to move to the next activity is curiously missing:

Button does not appear

Here is the code where I inflate the various player's cards and set the OnClickListener for the button:

String display = "Looks like " + players.get(firstPlayerIndex).getName() + " goes first!";
    if (!firstPlayer.isComputer()) {
        display = "Looks like You go first!";
    }
    textViews.get(0).setText(display);

    ViewGroup vg = (ViewGroup) findViewById(R.id.fp_SV_placeHolder);

    for (int i = 0; i < game.getPlayers().size(); i ++) {
        //Inflate Player display and find TV and IV's
        View v = getLayoutInflater().inflate(R.layout.fp_player_top_3, vg, false);
        TextView textView = (TextView) v.findViewById(R.id.fp_TV_player);
        ImageView iv_Card1 = (ImageView) v.findViewById(R.id.fp_IV_tableUp1);
        ImageView iv_Card2 = (ImageView) v.findViewById(R.id.fp_IV_tableUp2);
        ImageView iv_Card3 = (ImageView) v.findViewById(R.id.fp_IV_tableUp3);

        //Set TextView display
        String textViewDisplay = "Your Top 3 Cards";
        if(players.get(i).isComputer()) {
            textViewDisplay = players.get(i).getName();
        }
        textView.setText(textViewDisplay);
        //set ImageView Displays
        int resID1 = getResources().getIdentifier(players.get(i).getTableUp().get(0).getImageIDString(),"drawable",getPackageName());
        iv_Card1.setImageResource(resID1);
        int resID2 = getResources().getIdentifier(players.get(i).getTableUp().get(1).getImageIDString(),"drawable",getPackageName());
        iv_Card2.setImageResource(resID2);
        int resID3 = getResources().getIdentifier(players.get(i).getTableUp().get(2).getImageIDString(),"drawable",getPackageName());
        iv_Card3.setImageResource(resID3);

        if(i == firstPlayerIndex) {
            v.setBackgroundColor(Color.BLUE);
        }
        //attach to the root
        vg.addView(v);
    }
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Class nextClass = playerTurn.class;
            if (firstPlayer.isComputer()) {
                nextClass = computerTurn.class;
            }
            Intent intent = new Intent(getApplicationContext(),nextClass);
            startActivity(intent);
        }
    });

The button works as intended so long as it's visible, but it needs to be visible no matter how many players are in the game at once. Any suggestions? I tried simply moving the button inside the scrollView and the app broke upon trying to load the page.

1 Answers1

1

Do not give weight to scrollview. It does not work well with scrollview. ScrollView neglects weight and take height as per the height of content in it. So to solve it, You need to keep scrollview inside a linear layout and give weight to linear layout instead of scrollview

`<LinearLayout
        android:id="@+id/fp_SV_placeHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

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

            <LinearLayout
                android:id="@+id/fp_SV_placeHolder"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" />
        </ScrollView>
    </LinearLayout>`
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • This worked, and the button was technically clickable at the bottom of the screen no matter the number of players, however the button was still not displaying properly... it was infinitely small or something. By removing the weight parameter from the button as well it now remains visible and clickable. Thank you so much! – Dr_StrangeKill Apr 09 '17 at 19:52
  • I didn't see that you gave weight to button as well, if you give weight to both, it will try to adjust both views according to weight but if you give weight to just one view then i will give proper height to each view and adjust itself in the remaining available space. – Malwinder Singh Apr 09 '17 at 19:56