0

enter image description here

I am getting list of phone companies from web service and i have to set it to textview but the problem is i am not getting alignment as above image.How to achieve it.

Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
  • 1
    What are you getting and what do you want? Please be more clear.. – Amulya Khare Oct 17 '13 at 11:18
  • i am getting list of phone companies and i am not getting above alignments of textview. – Avinash Kumar Pankaj Oct 17 '13 at 11:24
  • Alignment is a very generic term, and especially if you show a picture different people may have different interpretation.. like are you looking for left alignment or line spacing or what?? It is always good to put in some effort and try to explain what is going wrong? "i am not getting above alignment" won't help people understand.. – Amulya Khare Oct 17 '13 at 11:28
  • Maybe show a picture of what you are currently getting, and code which produces that view. – Amulya Khare Oct 17 '13 at 11:29
  • actually i have to generate textview dynamically according to number of phone companies. and i want each textview to be in that row only in which it started like if intext is in second row then it should be in second row only. I dont want that half portion in other row and other half in another. – Avinash Kumar Pankaj Oct 17 '13 at 11:33
  • Each name of company is different `TextView` or 1 `TextView` with all this string? – Amulya Khare Oct 17 '13 at 11:36
  • yeah i have to generate TextView for each company as i have to navigate to related activity on clicking that particular company – Avinash Kumar Pankaj Oct 17 '13 at 11:40
  • I am not sure how one textview can split into two rows? Maybe show the code and the screenshot that you are getting now. – Amulya Khare Oct 17 '13 at 11:42
  • by the way how can i achieve it?? – Avinash Kumar Pankaj Oct 17 '13 at 11:44
  • 1
    i guess you will have to create a dynamic layout for the circled part in the image above. And to create n number of textviews dynamically check this link http://stackoverflow.com/a/5918524 – Shrikant Oct 17 '13 at 11:52

2 Answers2

2

From what I understand, you want to add text views one beside the other, but when they overflow (go out of the screen) the next text view should be placed in the next line.

Doing this is not trivial. Implementing something like this (optimally and correctly) requires understanding of how android draws views (onMeasure and onLayout). However if you do not care about efficiency that much (mainly because you are going to do it only for a small portion of the view) then here is my quick hack:

mContainer = (RelativeLayout) findViewById(R.id.container);

// first layout all the text views in a relative layout without any params set.
// this will let the system draw them independent of one another and calculate the
// width of each text view for us.
for (int i = 0; i < 10; i++) {
    TextView tv = new TextView(getApplicationContext());
    tv.setText("Text View " + i);
    tv.setId(i+1);
    tv.setPadding(10, 10, 20, 10);
    mContainer.addView(tv);
}

// post a runnable on the layout which will do the layout again, but this time 
// using the width of the individual text views, it will place them in correct position.
mContainer.post(new Runnable() {
    @Override
    public void run() {
        int totalWidth = mContainer.getWidth();

        // loop through each text view, and set its layout params
        for (int i = 0; i < 10; i++) {
            View child = mContainer.getChildAt(i);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // this text view can fit in the same row so lets place it relative to the previous one.
            if(child.getWidth() < totalWidth) {

                if(i > 0) {  // i == 0 is in correct position
                    params.addRule(RelativeLayout.RIGHT_OF, mContainer.getChildAt(i-1).getId());
                    params.addRule(RelativeLayout.ALIGN_BOTTOM, mContainer.getChildAt(i-1).getId());
                }
            }
            else {
                // place it in the next row.
                totalWidth = mContainer.getWidth();
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.addRule(RelativeLayout.BELOW, mContainer.getChildAt(i-1).getId());
            }
            child.setLayoutParams(params);
            totalWidth = totalWidth - child.getWidth();
        }

        mContainer.requestLayout();
    }
});

Basically, I let the system do the layout and measurement for me in the first round(s) of drawing. Then using the widths of each text view now available, I reset the layout params based on the wrapping logic and do the layout again.

Try it with text of different size, it will auto adjust

Try it with text of different size, it will auto adjust. I would say this solution is pretty hacky but it works. If you are not satisfied with it take a look at this.

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
0

use

 android:textAlignment="textStart"
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32