Tim Daiber
Tim Daiber

Reputation: 358

Make TextView appear above another TextView

I am trying to make 2 TextViews apear above each other through code.

My question is: how can I stack them over each other?

Sample Code

private TextView DurationView;
private TextView DurationViewoverall;

DurationView = new TextView(getContext());
    DurationView.setText("");
    addView(DurationView);


    DurationViewoverall = new TextView(getContext());
    DurationViewoverall.setPadding(5,5,5,5);
    DurationViewoverall.setText("");
    addView(DurationViewoverall);

I am trying to have DurationView appear above DurationViewoverall. The class they are in extends Linear Layout.

Upvotes: 0

Views: 97

Answers (2)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

Add LayoutParams to the textview and make your linear layout as orientation vertical

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

DurationView = new TextView(getContext());
LinearLayout.LayoutParams durationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurationView.setLayoutParams(durationViewParams);
DurationView.setText("Text1");
linearLayout.addView(DurationView);


DurationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurationViewoverall.setLayoutParams(durationViewoverallParams);
DurationViewoverall.setPadding(5,5,5,5);
DurationViewoverall.setText("Text2");
linearLayout.addView(DurationViewoverall);

addView(linearLayout);

Upvotes: 2

ssawchenko
ssawchenko

Reputation: 1228

Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.

Upvotes: 0

Related Questions