Jernej K
Jernej K

Reputation: 1694

Fragment content gets cut off

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.

Upvotes: 4

Views: 3017

Answers (5)

Rihan Husain
Rihan Husain

Reputation: 88

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 :)

Upvotes: 0

Andrew Orobator
Andrew Orobator

Reputation: 8666

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.

Upvotes: 0

Rashid
Rashid

Reputation: 1740

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>

Upvotes: 0

T D Nguyen
T D Nguyen

Reputation: 7613

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

 <ListView
    ...
    android:layout_weight="1"/>

Upvotes: 1

Simon
Simon

Reputation: 10160

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

Upvotes: 1

Related Questions