0

I have an array of strings(dynamic). I have to show that strings in a textview. The size of the string may vary. The first string is "check", the second String is "my head is breaking" and so on. I want the textview to adjust itself. Say for ex : If the first textview occupied less than half of the screen, The second textview will be next to it. Else, The 2nd Textview will be placed at the next row.

Please check the following image. How can i achieve like this?

enter image description here

Rethinavel
  • 3,912
  • 7
  • 28
  • 49

2 Answers2

1

I'm not native speaker.

In order to achieve your purpose you need to follow these steps: 1. Find the dimensions of your device: I did this some time ago, here you have my implementation.

protected void loadDeviceDimensions(){
    Point size = new Point();//Calcular Width y Height.
    WindowManager w = getWindowManager();
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
    {
         w.getDefaultDisplay().getSize(size);
         DeviceWidth = size.x;
         DeviceHeight = size.y; 
    }
    else
    {
         Display d = w.getDefaultDisplay(); 
         DeviceWidth = d.getWidth(); 
         DeviceHeight = d.getHeight(); 
    }
}
  1. Once you have the device dimensions the next step is create the text fields, the text, font, and all the other stuffs. Don't worry about the position, just create the fields
  2. Once you have your fields created with their text and font set, its time to set their position. For these purpose you will need to use the TextView.getWidth() and TextView.getHeight(). The code should look like this.
for(int i=0, i < textView-1 , i++) {

     if( textViews[i].getWidth <= deviceWidth/2 && textViews[i+1].getWidth <= deviceWidth/2){
          //Put the two text fields on the same line
          i++;//Don't forget this line.
     }else{
          //Put only one text field on the line.
     }
}
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
Hristo Ivanov
  • 679
  • 8
  • 25
0

You can check your Display size, and you can check your size the TextView will have. then it should not be a problem, otherwise let me know.

display size

textview size without to draw first

Community
  • 1
  • 1
lukas
  • 495
  • 4
  • 13