0

I am having linear layout with orientation horizontal in my XML file and i am trying to add text views using below code.

LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("Sample");
myLinearLayout.addView(tv);

Adding 5 text views in the similar fashion looks fine but if am trying to add more text views they are moving out the screen. I know using horizontal orientation aligns all views in horizontal direction. But how can i make changes to my design/code so that, the hidden text views automatically moves to the next line. Can anyone suggest me. Thanks in advance.

3 Answers 3

1

LinearLayout is either horizontal or vertical, but no both.

You can use GridLayout, maybe this fits your needs better.

Sign up to request clarification or add additional context in comments.

6 Comments

I love this simple idea. I forgot that Grid Layout aligns if there is no space. Thanks for this. Let me try it
Yes it worked but i am checking on it still. I am displaying 26 alphabets. When i tried numcolumns as autofit, only 2 columns are getting arranged. And when i try to specify a number(say 7) the last row(no of items = 5) is giving me a problem. I am trying to make it align to center. Any suggestions?
Could you help me in this issue too
I don't really know, what you mean.
look at this mkyong.com/android/android-gridview-example . I tried the sample but with android:numColumns as 7.( If i am not using it am getting only 2 items per row.) and the last row is left with only 5 elements which are getting aligned to left. Can you suggest me to make the five items in the last row to be aligned to center
|
1

you will need to set main layout orientation in xml file to Vertical and then add new LinearLayout in programmatically with 5 textview's in each rows as:

 LinearLayout innerlayout = new LinearLayout(this);
 innerlayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.WRAP_CONTENT));
 innerlayout.setOrientation(LinearLayout.HORIZONTAL);
 innerlayout.addView(tv);
 innerlayout.addView(tv1);
 ....
// add innerlayout to main layout myLinearLayout
  myLinearLayout.addView(innerlayout);

1 Comment

This is a good idea. But number of textviews in each row should not be fixed. It must aligned if there is no space in the first row.
1

You can add the TextView using a loop. Inside the loop you check to see if the x coordinate of right edge of the TextView is equal to the width of the screen. If so, you vary the y coordinate a certain amount you want, and you keep adding TextViews. You will have to change your ViewGroup to one that is not as restrictive (i.e. RelativeLayout)

Comments

Your Answer

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.