Search code examples
javaandroidandroid-layoutandroid-relativelayout

Add views programmatically in a layout and "clustered" to the center


I have a relative layout and I want to do the following:
I want to place on the right of the relative layout programmatically X textviews.
X can be just 1 or can be up to 4.
What I need is that in the end I want the textviews to "cluster" to towards the center/vertically.
Examples:

---------------------------  RelativeLayout  Top   


TextView


---------------------------  RelativeLayout Bottom    

With 2:

---------------------------  RelativeLayout  Top   

TextView-1  (the center is between Text-1 and Text-2)
TextView-2  

---------------------------  RelativeLayout Bottom    

With 3:

---------------------------  RelativeLayout  Top   

TextView-1
TextView-2
TextView-3

---------------------------  RelativeLayout Bottom    

etc.
What I tried is to create a linear layout with vertical orientation and add it to the left of the relative layout.
Then I am adding the text views as children if applicable to the linear layout but this does not work:
1) The linear layout always wraps instead of matching parent width
2) I can't seem to be able to set each child layout_gravity to center to test what happens because if I do:

((LinearLayout.LayoutParams)textView.getLayoutParams()).gravity = Gravity.CENTER;
That affects the text and not the position.
Can anyone help on this? Even a simple example with views to understand how this is done would be great!

Update: This is what I do now:

LinearLayout linearLayout = new LinearLayout(theContext);  RelativeLayout.LayoutParams linearLayoutsParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);  linearLayoutsParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  linearLayout.setOrientation(LinearLayout.VERTICAL);  linearLayout.setLayoutParams(linearLayoutsParams); 

Solution

  • From what I have understood, you don't need to set gravity to the LinearLayout childs. Just set

    android:layout_centerVertical="true"
    

    on the LinearLayout itself (with height = wrap_content, and vertical orientation). Something like:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
    
        android:gravity="center_vertical"
        android:orientation="vertical">
    

    Programmatically (though untested):

    LinearLayout linearLayout = new LinearLayout(context);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setGravity(Gravity.CENTER_VERTICAL); //not sure it's needed
    linearLayout.setLayoutParams(params);
    

    The linear layout rule will not propagate to its childs: LL will have equally spaced childs. But we set height to wrap_content so it's gonna be exactly that tall, and we set center_vertical so it's gonna stay centered.