Stanly T
Stanly T

Reputation: 1054

How to set one view in vertical linearlayout in top and another in bottom programmatically?

I'm trying to create programmatically parent LinearLayout and 3 TextViews. One TextView must be aligned in top left, the second one in the center of parent and the third one to the right and bottom of my screen. Everything must be done in code. I'm almost done, but there is still some problem. All my 3 views are on the top enter image description here

My code:

public class ActivityFour extends AppCompatActivity {
    private LinearLayout mLinearLayout;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    private static final int TV_ID1 = 101;
    private static final int TV_ID2 = 102;
    private static final int TV_ID3 = 103;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLinearLayout = new LinearLayout(this);
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(mLinearLayout, llParams);


        LinearLayout.LayoutParams linlayout_params1 =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                              LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams linlayout_params2 =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                              LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams linlayout_params3 =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                              LinearLayout.LayoutParams.WRAP_CONTENT);

        linlayout_params1.setMargins(16,16,16,16);
        tv1 = new TextView(this);
        tv1.setId(TV_ID1);
        linlayout_params1.gravity = Gravity.START;
        linlayout_params1.gravity = Gravity.TOP;

        mLinearLayout.addView(tv1, linlayout_params1);
        tv1.setText("TextView number ONE");

        linlayout_params2.setMargins(16,16,16,16);
        tv2 = new TextView(this);
        tv2.setId(TV_ID2);
        linlayout_params2.gravity = Gravity.CENTER;
        mLinearLayout.addView(tv2, linlayout_params2);
        tv2.setText("TextView number TWO");

        linlayout_params3.setMargins(16,16,16,16);
        tv3 = new TextView(this);
        tv3.setId(TV_ID3);
        linlayout_params3.gravity = Gravity.END;
        mLinearLayout.addView(tv3, linlayout_params3);
        tv3.setText("TextView number THREE");
    }
}

After adding the weight property to all view.params it looks like this enter image description here

Upvotes: 1

Views: 85

Answers (2)

kunal soneta
kunal soneta

Reputation: 9

First of all no need to create liner layout for all textview you can do like this:

in xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/test"
    tools:context=".activity.TestActivity"/>

and in activity file :

LinearLayout layout = findViewById(R.id.test);

        TextView product_1 = new TextView(this);
        product_1.setText("Product 1");
        product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
        layout.addView(product_1);

        TextView product_2 = new TextView(this);
        product_2.setText("Product 2");
        product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
        product_2.setGravity(Gravity.CENTER);
        layout.addView(product_2);

        TextView product_3 = new TextView(this);
        product_3.setText("Product 3");
        product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
        product_3.setGravity(Gravity.END|Gravity.BOTTOM);
        layout.addView(product_3);

Upvotes: 0

B&#246; macht Blau
B&#246; macht Blau

Reputation: 13009

In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:

LinearLayout.LayoutParams linlayout_params2 =
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                          LinearLayout.LayoutParams.WRAP_CONTENT, 1);

Upvotes: 1

Related Questions