1

I am trying to add textviews dynamically to my activity and I am able to do so but the added textviews are getting added next to each other on the right side and what I want is to have each textview underneath each other.

Here is my code:

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
 for(int x = 0;x < 10; x++){
 LinearLayout layout = new LinearLayout(this);
 TextView tv = new TextView(this);
 tv.setText("Text View "+x);
 layout.setOrientation(LinearLayout.HORIZONTAL);
 layout.addView(tv);
 linearLayout.addView(layout);
 }
 }

Main XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/linearLayout"
 android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
asked Jul 27, 2015 at 14:56
2
  • Why you are increasing you views dependencies by adding nested linear layout? Can you please try my answer ? Commented Jul 27, 2015 at 15:15
  • Thanks for appreciation. :) Commented Jul 27, 2015 at 15:25

2 Answers 2

2

You can do this way:

You have to set orientation vertical of your parent Linear Layout:

Your xml should looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/linearLayout"
 android:orientation="vertical"></LinearLayout>
</RelativeLayout>

Your java code should looks like this:

for(int x = 0;x < 10; x++){
 TextView tv = new TextView(this);
 tv.setText("Text View "+x);
 linearLayout.addView(tv);
}

Hope this will help you.

answered Jul 27, 2015 at 15:05
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much man, I can't believe all I had to do was change the orientation :(
@user2236520, Glad to help you. Can you please accept the answer if you are happy with Answer ? :)
1

Instead of this:

layout.setOrientation(LinearLayout.HORIZONTAL);

Do this to your linear layout:

layout.setOrientation(LinearLayout.VERTICAL);
answered Jul 27, 2015 at 14:58

1 Comment

I can't believe that's all I had to do, I feel so dumb right now, Thank you so much man!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.