2

I'm trying to make a layout in Android with Fragments. I started to use Commonsware's MergeAdapter but I'm having a weird bit of trouble. The layout worked fine before, but now I get this:

enter image description here

There are a couple problems: The white strip should have text going all the way across its length. I set the TextView with a white background to make sure the width was being set correctly. The checkbox right below it should also say "Issue?" but it's trying to wrap the text for some reason.

Here is the layout code that is being added:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
    <TextView android:id="@+id/tvInstructions" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
        <CheckBox android:id="@+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp" android:text="Issues?" />
        <TextView android:id="@+id/tvStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5" android:text="Station:" />
        <Spinner android:id="@+id/spStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>

and here's how I'm inflating it inside the Fragment's onActivityCreated:

LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MergeAdapter _adapter = new MergeAdapter();

View observationHeaderView = inflater.inflate(R.layout.observationheader, getListView(), false);
_adapter.addView(observationHeaderView);

I get a feeling it has something to do with how I'm inflating the layout. Any help would be appreciated. Thanks.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jess
  • 42,368
  • 6
  • 37
  • 51

2 Answers2

1

Don't use match_parent for android:layout_height for something going into an AdapterView, such as your root LinearLayout.

You might also consider temporarily putting your header layout outside the ListView (e.g., in a vertical LinearLayout also holding the ListView) and get it debugged before adding the complication of having it in the ListView.

Beyond that, fire up Hierarchy View (Eclipse perspective or standalone edition) and try to figure out where your layout rules are going wrong.

Also, I am uncertain how well your Spinners will work inside of a ListView on Honeycomb.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Changing the layout_height didn't fix it. I put the header outside of the ListView and it works just fine. It only does this inside the ListView. I tried Hierarchy View before coming here, and I couldn't tell what was going on. The element is the correct width, as best I can tell. And the spinners work fine. – Jess Sep 28 '11 at 01:45
  • It works fine if I don't add any other views to the ListAdapter. – Jess Sep 28 '11 at 01:56
  • @Andrew Koester: If you create a full sample project that demonstrates the problem, I can take a look at it. – CommonsWare Sep 28 '11 at 11:08
  • Ah-hah. While creating the sample project, it appeared to work fine until I selected a value from one of the Spinners. I assume this is the root of my problem, I'll just replace it with a button and a radio button alert dialog. – Jess Sep 28 '11 at 21:52
  • Stranger and stranger. It still does it with a textbox, once I change the value. – Jess Sep 29 '11 at 01:20
  • @Andrew Koester: IMHO, `ListView` is really not designed for things like `EditText` and `Spinner`. If you don't have many rows, you might just put all of them in a vertical `LinearLayout` and wrap that in a `ScrollView`. – CommonsWare Sep 29 '11 at 10:42
  • @CommonsWare i am using merge adapter to show my list of data's in vertical scroll is it possible to show it in horizontal list view if can u please share the lib link – Manoj Mar 17 '15 at 10:36
0

It appears my problem was with layout_width being match_parent in certain places in my header, when it should have been wrap_content. The following worked:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
    <TextView android:id="@+id/tvInstructions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
        <CheckBox android:id="@+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:text="Issues?" />
        <TextView android:id="@+id/tvStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5" android:text="Station:" />
        <Spinner android:id="@+id/spStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>
Jess
  • 42,368
  • 6
  • 37
  • 51