tsil
tsil

Reputation: 2069

TextView and EditText cut off in LinearLayout

I have a LinearLayout with horizontal orientation in which I add some TextView and EditText dynamically. My problem is when the TextView is long, it is cut off (by height) and the EditText is invisible.

Adding padding to the views didn't solve it. When setting the LinearLayout minHeight value, the TextView is displayed correctly but the EditText is still invisible.

<LinearLayout
        android:id="@+id/content_content"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"></LinearLayout>

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);

for (int i = 0; i < cntCount; i++) {
    TextView cntView = new TextView(getActivity());
    cntView.setLayoutParams(params);
    cntView.setText(cnt.get(i));

    contentContentView.addView(cntView);

    EditText answerView = new EditText(getActivity());
    answerView.setLayoutParams(params);
    answerView.setId(i);
    answerView.setHint("......");

    contentContentView.addView(answerView);

}

EDIT

I have found the solution and created a library. For those interested you can download it here

Upvotes: 1

Views: 1157

Answers (2)

Jim
Jim

Reputation: 10288

Your LinearLayout has an orientation of "horizontal" - it will add the next view to the right of the current view. This means that if the TextView wraps (fills the parent) there is no room for any additional views in the LinearLayout that will be visible on the screen (they will be added to the right of the visible layout).

If you are trying to place an EditText in the same line with a series of TextViews, then you will need to create a custom TextView and layout. This requires doing measurements about the placement and size of the EditText that are not standard with Android.

This post may help: How to overlay EditText on TextView just after the end of text

Upvotes: 1

mrtn
mrtn

Reputation: 889

You can set up layout_weights for the EditText and the TextView instead of using wrap_content for the layout_width. That would make sure that the EditText will always be shown.

Upvotes: 1

Related Questions