4

I am currently developing an Android game. I have a problem with my content inside a Fragment. The content gets cut off and I am not able to find the problem. I only use the resource directory, where I define the different layouts. That is why I know the problem lies in the layouts. First I thought it has something to do with the fact I used android:layout_height="match_parent". I found this solution on StackOverflow and I hoped that would also solve my problem. But this did not help me at all.

I have the activity_game.xml. There I have a placeholder for the current displayed Fragment. The content is the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  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="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

The Fragment itself has the following layout defined:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal">

  <TextView
    android:id="@+id/money"
    style="@style/traderY.TextCenter" />

  <TextView
    android:id="@+id/location"
    style="@style/traderY.TextCenter" />

  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/junkie_icon"/>

  <ListView
    android:id="@+id/drug_list"
    style="@style/traderY.List" />

  <TableRow
    style="@style/traderY.ButtonRow">

    <Button
        android:id="@+id/sell_drugs"
        style="@style/traderY.ButtonCenter"
        android:text="@string/sell"/>

    <Button
        android:id="@+id/nothing"
        style="@style/traderY.ButtonCenter"
        android:text="@string/nothing"/>

  </TableRow>

</LinearLayout>

And here is the captured screen to show the problem.

Screen capture

As you can see, the last two buttons are being cut. I would like to avoid that and rather allow the user to scroll down. I am in a dead situation and I would appreciate any help. And I hope I manage to explain the problem good enough to be understood.

SMR
  • 6,628
  • 2
  • 35
  • 56
Jernej K
  • 1,602
  • 2
  • 25
  • 38

5 Answers5

1

You should wrap your fragment layout in a Scrollview to enable scrolling:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<!-- your layout here -->

</ScrollView>

You can see an example of it in use here: Android ScrollView Example

Simon
  • 9,762
  • 15
  • 62
  • 119
  • I will give it a try right away. Should I do this inside the `LinearLayout` of the Fragment layout or would you recommend to replace the `LinearLayout` with the `ScrollView`? – Jernej K Apr 19 '16 at 02:56
  • OK, I think I get your solution. I have to place the `LinearLayout` inside the `ScrollView`. – Jernej K Apr 19 '16 at 02:58
  • yes typically you would place `LinearLayout` inside the `ScrollView` – Simon Apr 19 '16 at 02:59
  • I did not solve my problem. By using this I automatically shrink the `ListView` I have for displaying the list of all drugs you can sell. Now the list only shows the header columns and I have the scroll inside the list. I only see one row at a time. – Jernej K Apr 19 '16 at 03:06
  • Is the `ListView` itself scrollable at the moment? – Simon Apr 19 '16 at 03:10
  • It is, but like I said, it only displays one row at a time. It is hard to scroll and it even looks horrible. – Jernej K Apr 19 '16 at 03:15
1

Without using ScrollView, you can set weight for ListView. Try this solution:

 <ListView
    ...
    android:layout_weight="1"/>
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • I will give it a try, because the `ScrollView` did not do the trick. – Jernej K Apr 19 '16 at 03:06
  • Sorry, I updated my answer. `ListView` instead of `TableRow` – T D Nguyen Apr 19 '16 at 03:07
  • OK, I used this and now the `ListView` has a scroll. And that way the content is fully displayed. I wanted to scroll the whole content, but this solution is also good enough. Thanks! – Jernej K Apr 19 '16 at 03:17
  • How/why does this work? Why specifically add it to the ListView? I'm having the same problem but just a bunch of Spinners and EditTexts in my fragment inside a Linear Layout – user3294126 Apr 22 '18 at 12:53
0

you should make your parent container height to match_parent

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  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="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>
Rashid
  • 1,700
  • 1
  • 23
  • 56
0

I would avoid wrapping your entire layout in a ScrollView, because it contains a ListView and nested scrolling views should be avoided. Instead, if you'd like the buttons to appear at the bottom of the list, you can make them the last list item in your ListView. You'd have to override getItemViewType(int position) and getViewTypeCount() in your adapter to do this.

Another solution would be to place the buttons on top of the ListView, by putting the ListView and the TableView in a RelativeLayout, then declaring the TableView last.

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
0

if your layout already contains a listview then applying parent layout as scrollview isnt a good practice. however if you still want to use it then you can set the height of the listview dynamically in order to make all its Items visible without scrolling. you can achieve this by using this small code

    public static void setListViewHeightBasedOnChildren(final ListView listView) {
            listView.post(new Runnable() {
                @Override
                public void run() {
                    ListAdapter listAdapter = listView.getAdapter();
                    if (listAdapter == null) {
                        return;
                    }
                    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
                    int listWidth = listView.getMeasuredWidth();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                        View listItem = listAdapter.getView(i, null, listView);
                        listItem.measure(
                                View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


                        totalHeight += listItem.getMeasuredHeight();
                        Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
                    }

                    Log.d("totalHeight " + totalHeight, "********");

                    ViewGroup.LayoutParams params = listView.getLayoutParams();
                    params.height = (int) ((totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))));
                    listView.setLayoutParams(params);
                    listView.requestLayout();

                }
            });
        }

just pass your ListView Id to this method and it will set the listview height dynamically for you. and then you can use scrollview without any problem, and scroll the whole content as per your need.

usage:

    ListView yourListView = (ListView) findViewById(R.id.your_list_view);

    YourListViewAdapter adapter = new YourListViewAdapter();

    yourListView.setAdapter(adapter);

    //set height here
    setListViewHeightBasedOnChildren(yourListView);

Hope it helps. Happy Coding :)

Rihan Husain
  • 88
  • 10