Question

I tried to add some GUI elements like an ImageView or a TextView to a LinearLayout programmatically. But the elements aren't displayed.

To see if a element is drawn or not, I set a different background color for each element. The result was that I can only see the background color of the LinearLayout. But why?

public class MyLinearLayout extends LinearLayout {
  public MyLinearLayout(Context context) {
    super(context);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    setBackgroundColor(Color.RED);


    imageView = new ImageView(context);
    params = new LinearLayout.LayoutParams(100, 100);
    imageView.setLayoutParams(params);
    imageView.setBackgroundColor(Color.BLUE);

    addView(imageView);
  }
}

The strange thing is that I can see the red background color of the LinearLayout but in the size of the ImageView. If I add some other GUI elements like a TextView, I can see how the LinearLayout grows. But I can not see the TextView.

I'm really confused, because this not the first time I do something like this. Can u tell me what I'm doing wrong?


This is a snippet of the layout.xml file:

<LinearLayout android:layout_width="match_parent"
                  android:layout_height="45dp"
                  android:id="@+id/bottom_bar"
                  android:layout_alignParentBottom="true"
                  android:gravity="bottom">

        <FrameLayout android:id="@+id/block_edit_delete_layout"
                     android:layout_height="match_parent"
                     android:layout_width="wrap_content"
                     android:background="@drawable/block_edit_delete_selector">

            <ImageView android:layout_height="match_parent"
                       android:layout_width="wrap_content"
                       android:src="@drawable/block_edit_delete"
                       android:scaleType="fitXY"
                       android:contentDescription="@string/delete"/>
        </FrameLayout>

        <LinearLayout
                android:id="@+id/block_edit_progress"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal"/>

        <FrameLayout android:id="@+id/block_edit_random_layout"
                     android:layout_height="match_parent"
                     android:layout_width="wrap_content"
                     android:background="@drawable/block_edit_delete_selector">

            <ImageView android:layout_height="match_parent"
                       android:layout_width="wrap_content"
                       android:src="@drawable/block_edit_random"
                       android:scaleType="fitXY"
                       android:contentDescription="@string/random_numbers"/>

        </FrameLayout>
    </LinearLayout>

The LinearLayout with the ID block_edit_progress is the container layout of multiple instances of the class MyLinearLayout. The instances are added in the code:

    for(int i = 0; i < numberOfMyLinearLayouts; i++) {
        MyLinearLayout v = new MyLinearLayout(getContext());
        addView(v);
    }

I hope this helps.

Was it helpful?

Solution 3

I solved the problem. (Or found a workaround)

I moved the complete initialization stuff out of the constructor of the MyLinearLayout. If I then adding a View after the layout has been completely generated, everything works.

Like this:

MyLinearLayout ll = new MyLinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
ll.setLayoutParams(params);
ll.setBackgroundColor(Color.RED);

ImageView v = new ImageView(getContext());
params = new LinearLayout.LayoutParams(50, 50);
v.setLayoutParams(params);
v.setBackgroundColor(Color.GREEN);

ll.addView(v);
addView(ll);

I don't know why the other way doesn't work. Thanks for the fast answers!

OTHER TIPS

If i convert your code to xml, it would be something like:

    <LinearLayout layout_width=wrap_content, layout_height = wrap_content>
    <LinearLayout id= MyLinearLayout>//just an idea, syntax may be wrong
    <LinearLayout layout_width= 100, layout_width=100>
    <ImageView color=BLUE>
    </ImageView>
    </LinearLayout>
    </LinearLayout>
    </LinearLayout>

Whenever you call setLayoutParams on a View, parameter params you give should be parent element.

Try something like if you want linearlayout to be the parent of your linearlayout, use MATCH_PARENT for width, height if you want your view to span the width, height of view's parent

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             ViewGroup.LayoutParams.MATCH_PARENT,
             ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(lp);//lp is parent view

Also try this, just in case views are getting added to right of your views, and you are not able to see them on screen

yourview.setOrientation(LinearLayout.VERTICAL);

Change the width and height of linear layout to match_parent and see how it changes. wrap_content will only show the content of the linear layout, which seems to be your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top