providence
providence

Reputation: 29463

Layout Views in LinearLayout Programmatically

I am trying to position a TextView and an ImageView within a LinearLayout programmatically. The problem is that the textview is always on top of the imageview, and I would like to it be underneath. I cannot find a way to imitate the android:layout_below= xml attribute in the java UI approach.

Upvotes: 0

Views: 419

Answers (2)

james
james

Reputation: 26271

LinearLayout lin = new LinearLayout(context);
lin.setOrientation(LinearLayout.VERTICAL);

ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);

lin.addView(imageView);
lin.addView(textView);

the first component ImageView should be placed in the LinearLayout first, before the TextView.

Edit: oops forgot about "programmatically"

Upvotes: 1

Matthew
Matthew

Reputation: 44919

You should simply switch the order of your two views.

For example:

linear.addView(image);
linear.addView(text);

instead of:

linear.addView(text);
linear.addView(image);

Upvotes: 2

Related Questions