0

I have two relative layouts that are currently displayed one below the other. I want the second layout to be displayed to right of the first relative layout programatically.

following is the code i used.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RelativeLayout 
         android:layout_width="620dp"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:id="@+id/lay1" >    
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/hello1" />
    </RelativeLayout>
    <RelativeLayout 
         android:layout_width="620dp"
         android:layout_height="wrap_content"
         android:orientation="vertical" 
         android:id="@+id/lay2">  
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/hello2" />
    </RelativeLayout>
</LinearLayout>

myAct.java

public class myAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layoutContainer = new LinearLayout(this);
        layoutContainer.setLayoutParams(new     LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        // Arguments here: width, height, weight 
        LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
        RelativeLayout layoutLeft = new RelativeLayout(this);

        layoutContainer.addView(layoutLeft, childLp);
        RelativeLayout layoutRight = new RelativeLayout(this);
        layoutContainer.addView(layoutRight, childLp);
     }
}

Thanks in advance

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
JKV
  • 271
  • 3
  • 12

3 Answers3

1

Change your linear layout orientation to horizontal. I would also set a weight sum of the linear layout to 2, and give each relative layout a weight of 1 to make sure evenly split.

Tony
  • 2,335
  • 21
  • 25
0

Modify these changes in your code you will get horizontal orientation.

In main.xml

android:layout_width="620dp" to android:layout_width="wrap_content"

Add android:id="@+id/ll"inLinearLayout

In myAct.java

remove all statements after setContentView and add below lines

LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
ll.setOrientation(LinearLayout.HORIZONTAL);

Try it, if any doubts ask me.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
0

Have a look on Predicate or Row Layout code written in this post:

Line-breaking widget layout for Android

Change the following line according to your requirement.

child.layout(xpos, ypos, xpos + childw, ypos + childh);

For example if you need only 2 textview in one row, do something like

if((i%2) != 0)

xpos = screenWidth / 2;

else

xpos = 0;
Community
  • 1
  • 1
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38