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">
When the orientation of a LinearLayout is unspecified, it is using the default, which is horizontal .
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".
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.
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.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With