0
votes

I have below RelativeLayout with a Button and LinearLayout. I am adding TextViews to LinearLayout but even though I have set orientation to vertical in LinearLayout attribute the content of LinearLayout is coming horizontal.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button android:text="Add a server" android:id="@+id/addHost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:onClick="addHost"/>


    <LinearLayout android:orientation="vertical"
        android:id="@+id/listhosts"
        android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
        android:layout_below="@+id/addHost" android:layout_weight="1">                              
        </LinearLayout>

</RelativeLayout>

And programatically I am doing like this:-

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.listhosts);

linearLayout.setOrientation(LinearLayout.VERTICAL);

TextView h = new TextView(this);
h.setText(line);
h.setId(index++);
linearLayout.addView(h);

Any clue where I am wrong?

Answer:

Adding below line worked:

h.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

2

2 Answers

1
votes

Eventhough the LinearLayout orientation is set to vertical, I was getting horizontal rendering of listview. The reasons was text views are added to linearlayout dynamically, hence textview parameters need to set programatically. Below I have mentioned how I have achieved this.

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.listhosts);

linearLayout.setOrientation(LinearLayout.VERTICAL);

TextView h = new TextView(this);
h.setText(line);
h.setId(index++);
linearLayout.addView(h);
//Adding below line worked for me.
h.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
0
votes
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
         android:id="@+id/listhosts"
     android:layout_width="fill_parent"                
             android:layout_height="fill_parent"
     >
<TableRow android:baselineAligned="false">
    <Button android:text="Add a server" android:id="@+id/addHost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:onClick="addHost"/>
</TableRow>

<TableRow android:id="@+id/tableRow1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
         android:id="@+id/listhosts1"
     android:layout_width="fill_parent"                
             android:layout_height="fill_parent"
     android:layout_weight="1">
      </LinearLayout>                


</TableRow>
</LinearLayout>