Search code examples
androiddynamicandroid-linearlayout

How create LinearLayout dynamically with aligned elements


I'm very new at android. Need to dynamically create LinearLayout inside RadioGroup with aligned TextViews as following. Here I just moved elements from palette and haven't changed any properties

enter image description here

But get this

enter image description here

What am I missing?

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
      LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
      1.0f);

        final LinearLayout linearLayout = new LinearLayout(this);

        final TextView text1 = new TextView(this);
        text1.setText("aaa");
        text1.setLayoutParams(param);

        linearLayout.addView(text1);

        final TextView text2 = new TextView(this);
        text2.setText("bbb");
        text2.setLayoutParams(param);

        linearLayout.addView(text2);

        final TextView text3 = new TextView(this);
        text3.setText("ccc");
        text3.setLayoutParams(param);

        linearLayout.addView(text3);
        radioGroup.addView(linearLayout);

Solution

  • LayoutParams should be both MATCH_PARENT and linearLayout should also be set to that params