Jayakarthik Appasamy
Jayakarthik Appasamy

Reputation: 129

Android dynamic RelativeLayout

My layout.xml file looks like this:

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="270dp"
        android:layout_weight="0.47" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>

    </ScrollView>

I use the following code to create TextViews and Buttons inside a for loop:

  View linearLayout =  findViewById(R.id.linearLayout2);


for (int i=0 ; i<10; i++){
     TextView textview = new TextView(this);
     textview.setText("Text view" + i);
     textview.setId(i);

     textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
     ((LinearLayout) linearLayout).addView(textview);


    Button button = new Button(this);
    button.setText("View");
    button.setId(i);
    int width=90;
    int height=60;
    button.setLayoutParams(new LayoutParams(width, height));
    ((LinearLayout) linearLayout).addView(button);

}

I'm getting the Button below the TextView. Can anyone help me with replacing this LinearLayout with RelativeLayout so that I get the TextView and the Button side by side?

Upvotes: 1

Views: 9427

Answers (4)

user
user

Reputation: 87064

Modify your layout so you have a RelativeLayout instead of the LinearLayout:

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="270dp"
        android:layout_weight="0.47" >

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </RelativeLayout>

</ScrollView>

Then try this to make the TextViews and Buttons sit on the same row:

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
    for (int i = 0; i < 10; i++) {
        Button button = new Button(this);
        button.setText("View" + i);
        button.setId(1000 + i);
        int width = 90;
        int height = 60;

        TextView textview = new TextView(this);
        textview.setText("Text view" + i);
        textview.setId(2000 + i);

        if (i == 0) {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview.setLayoutParams(rlp2);
            rl.addView(textview);
            RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                    width, height);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            button.setLayoutParams(rlp1);
            rl.addView(button);
        } else {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1);
            textview.setLayoutParams(rlp2);
            rl.addView(textview);
            RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                    width, height);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1);
            button.setLayoutParams(rlp1);
            rl.addView(button);
        }
    }

Upvotes: 6

SamSPICA
SamSPICA

Reputation: 1366

You are defining LinearLayout with android:orientation="vertical", so it will tend to be one below another.

Add your textview and button to individual linear layouts, with orientation as horizontal, and add this linearLayout to the original vertical LinearLayout.

Edit: Try the following:

View linearLayout =  findViewById(R.id.linearLayout2);

for (int i=0 ; i<10; i++){
 View indiLyt = new View(this);
 indiLyt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 indiLyt.setOrientation(LinearLayout.HORIZONTAL);

 TextView textview = new TextView(this);
 textview.setText("Text view" + i);
 textview.setId(i);

 textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 ((LinearLayout) indiLyt).addView(textview);

 Button button = new Button(this);
 button.setText("View");
 button.setId(i);
 int width=90;
 int height=60;
 button.setLayoutParams(new LayoutParams(width, height));
 ((LinearLayout) indiLyt).addView(button);
 ((LinearLayout) linearLayout).addView(indiLyt);
}

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

try this solution::;

have a new xml file as below in res/layout of your project.

Save this as linear.xml

< LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:id="@+id/text"
            android:layout_width=" wrap_content "
            android:layout_height=" wrap_content " />
<Button            android:id="@+id/button"
            android:layout_width="90dp"
            android:layout_height="60dp" />
    </ LinearLayout >

and the below code:::

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.linearLayout2);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);

for (int i=0 ; i<10; i++){
View view = inflater.inflate(R.layout.linear,null);
view.findViewById(R.id.text).setText("Text view" + i);
view.findViewById(R.id.text).setTextColor(Color.BLUE);
view.findViewById(R.id.button).setText(“View");
view.findViewById(R.id.button).setTextColor(Color.BLUE);
linearLayout.addView(view);
}

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

 View linearLayout =  findViewById(R.id.linearLayout2);


for (int i=0 ; i<10; i++){
     TextView textview = new TextView(this);
     textview.setText("Text view" + i);
     textview.setId(i);

     textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
     ((LinearLayout) linearLayout).addView(textview);


    Button button = new Button(this);
    button.setText("View");
    button.setId(i);
    int width=90;
    int height=60;
    button.setLayoutParams(new LayoutParams(width, height));
    ((LinearLayout) linearLayout).addView(button);

}

replace the above with below code::

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);

TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);

l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
}

Upvotes: 0

Related Questions