Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_below and weightsum of LinearLayout Programmatically

I am creating LinearLayout in RelativeLayout programmatically. What I am trying to do is to assign the weigtsum in LinearLayout and want to set the layout_above attribute as well. The problem is weightSum is available in LinearLayout.LayoutParams and layout_above is available in RelativeLayout.LayoutParams. Currently,, I am doing

LinearLayout ll_menu_code = new LinearLayout(this);
        ll_menu_code.setId(1001);
        LinearLayout.LayoutParams parmas_ll_menu_code = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
        parmas_ll_menu_code.setMargins(0, 20, 0, 0);
        ll_menu_code.setLayoutParams(parmas_ll_menu_code);

Which sets the weightSum. How to set the layout_above? What should the way to set the both attributes?

I want to create the following xml layout programmatically

<LinearLayout
                                android:layout_below="@id/ll_menu_code"
                                android:id="@+id/ll_quantity"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginTop="20dp"
                                android:weightSum="5">
like image 557
Mustansar Saeed Avatar asked May 05 '16 08:05

Mustansar Saeed


People also ask

What is the default orientation of a LinearLayout?

When the orientation of a LinearLayout is unspecified, it is using the default, which is horizontal .

How do you align text in LinearLayout?

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".

What is RelativeLayout and LinearLayout?

Android Layout Types LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.


1 Answers

If you want to add LinearLayout into RelativeLayout programmatically, then you must create instance of RelativeLayout.LayoutParams rather than LinearLayout.LayoutParams since LinearLayout is a child to Relativelayout

So your code must be

LinearLayout ll_menu_code = new LinearLayout(this);
    ll_menu_code.setId(1001);
    ll_menu_code.setWeightSum(2f);        

    RelativeLayout.LayoutParams parmas_ll_menu_code = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    parmas_ll_menu_code.setMargins(0, 20, 0, 0);
    parmas_ll_menu_code.addRule(RelativeLayout.BELOW, 1001);

    ll_menu_code.setLayoutParams(parmas_ll_menu_code);

I Hope it helps..

like image 186
Fabin Paul Avatar answered Oct 14 '22 10:10

Fabin Paul