0

how do I position two programmatically created TextViews in a LinearLayout BESIDE each other? I tried the code below, but that way the "number" TextView is placed one line deeper compared to the "value" TextView, so the height of nlap LinearLayout changes. I need both TextViews to be at the same height, the "number" TextView should be on the left side an centered vertically. Any help is appreciated.

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 1, 10, 1);

    LinearLayout nlap = new LinearLayout(this);
    nlap.setOrientation(LinearLayout.VERTICAL);
                nlap.setLayoutParams(layoutParams);

    TextView value = new TextView(this);
    value.setText("Test");
    value.setTextColor(Color.parseColor("#A60101"));
    value.setTextSize(23);
    value.setGravity(Gravity.CENTER);
    value.setTypeface(font);

    TextView number = new TextView(this);
    number.setTextColor(Color.parseColor("#FFFFFF"));
    number.setText("01");

    nlap.addView(value);
    nlap.addView(number);
Droidman
  • 11,485
  • 17
  • 93
  • 141

1 Answers1

2

You should use nlap.setOrientation(LinearLayout.HORIZONTAL); to achieve having textViews beside each other, if you use VERTICAL as you do, second one will always be below the first.

slezadav
  • 6,104
  • 7
  • 40
  • 61
  • 2
    `answer.replace("VERTICAL", "HORIZONTAL");` :) – iTurki Sep 05 '12 at 07:45
  • thanks, now the textviews are placed in 1 row BUT value.setGravity(Gravity.CENTER) doesn't work for the "value" textview which I want to center. Any suggestions how to fix that? – Droidman Sep 05 '12 at 09:35
  • try nlap.setGravity(Gravity.CENTER); – slezadav Sep 05 '12 at 09:53
  • that works, but now the "number" textview is placed directly besides of the centered "value" textview. But I want the number to be on the left side of the layout. Goddammit, I'm a dumb guy I know ;) How can I lock the "number" textview on the left side of its parent layout? – Droidman Sep 05 '12 at 11:28
  • You'd better use RelativeLayout instead of linear. then you would be able to set it much easier. – slezadav Sep 05 '12 at 11:32
  • the problem is that I can't set margins for a RelativeLayout, so when multiple layouts are created, I can't visually separate them to make it look better.I tried number.setGravity(Gravity.LEFT) but it doesn't work.. – Droidman Sep 05 '12 at 11:56
  • You should be able to set margins for RelativeLayout, try: RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.addRule( RelativeLayout.ALIGN_PARENT_LEFT,number.getId() ); layoutParams.addRule( RelativeLayout.CENTER_HORIZONTAL,value.getId() ); – slezadav Sep 05 '12 at 12:00
  • 1
    Also try looking at http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically , http://stackoverflow.com/questions/3885077/setting-parameters-on-child-views-of-a-relativelayout – slezadav Sep 05 '12 at 12:04