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
spongyboss
8,72815 gold badges52 silver badges67 bronze badges
-
Why you are increasing you views dependencies by adding nested linear layout? Can you please try my answer ?Hiren Patel– Hiren Patel2015年07月27日 15:15:15 +00:00Commented Jul 27, 2015 at 15:15
-
Thanks for appreciation. :)Hiren Patel– Hiren Patel2015年07月27日 15:25:57 +00:00Commented Jul 27, 2015 at 15:25
2 Answers 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
Hiren Patel
52.9k21 gold badges178 silver badges153 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
spongyboss
Thank you so much man, I can't believe all I had to do was change the orientation :(
Hiren Patel
@user2236520, Glad to help you. Can you please accept the answer if you are happy with Answer ? :)
Instead of this:
layout.setOrientation(LinearLayout.HORIZONTAL);
Do this to your linear layout:
layout.setOrientation(LinearLayout.VERTICAL);
answered Jul 27, 2015 at 14:58
Aakash
5,2515 gold badges22 silver badges37 bronze badges
1 Comment
spongyboss
I can't believe that's all I had to do, I feel so dumb right now, Thank you so much man!
default