4

Is there any way in which I can display a long text on multiple columns?

For example I need to display an article, I have the entire String and I want to place it on 3 columns. How can I achieve this? Are there any libraries that can help me or do you know how should I approach this problem?

Any suggestion is appreciated. Thanks.

EDIT: The issue is the splitting of the string not the layout. I know I can use TableLayout... or weights... to distribute column evenly and so on. The problem is how do I split the String properly. Maybe 2 column would get filled and the 3rd just half of it? I don't know how to approach this, not the actual layout.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Would using three Textviews in a linear layout work? Would have to set layout to horizontal and evenly spacing might be tricky but i've managed this with Buttonviews. Possibly a Table might help too. – D-Dᴙum Dec 11 '11 at 13:16
  • but I have a single string. how will I know what part to display on each column... ? – Ovidiu Latcu Dec 11 '11 at 13:19
  • If you have no particular way the string needs to be split could you just get the length of the string and divide it in 3? – D-Dᴙum Dec 11 '11 at 13:27
  • I added an example for splitting the text in my answer. – rekire Dec 16 '11 at 12:21

3 Answers3

5

Check the TableLayout. To split your text you could split your text after 1/3 of your count of chars. Of cource you have to split the text at one whitespace charecter. UPDATE: See also my example code at the end of my posting.

Example

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView
            android:text="@string/table_layout_4_open"
            android:padding="3dip" />
        <TextView
            android:text="@string/table_layout_4_open_shortcut"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="@string/table_layout_4_save"
            android:padding="3dip" />
        <TextView
            android:text="@string/table_layout_4_save_shortcut"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

For splitting you can test this code. The algorithem could maybe a little better be nearer to the split boundaries, but it works.

public static String[] getRows(String text, int rows) {
    // some checks
    if(text==null)
        throw new NullPointerException("text was null!");
    if(rows<0 && rows > 10)
        throw new IllegalArgumentException("rows must be between 1 and 10!");
    if(rows==1)
        return new String[] { text };
    // some init stuff
    int len=text.length();
    int splitOffset=0;
    String[] ret=new String[rows];
    Pattern whitespace = Pattern.compile("\\w+");
    // do the work
    for(int row=1;row<rows;row++) {
        int end;
        int searchOffset=len/rows*row;          
        // search next white space
        Matcher matcher = whitespace.matcher(text.substring(searchOffset));
        if(matcher.find() && !matcher.hitEnd()) {
            // splitting on white space
            end=matcher.end()+searchOffset;
        } else {
            // hard splitting if there are no white spaces
            end=searchOffset;
        }
        ret[row-1]=text.substring(splitOffset, end);
        splitOffset=end;
    }
    // put the remaing into the last element
    ret[rows-1]=text.substring(splitOffset);
    return ret;
}
rekire
  • 47,260
  • 30
  • 167
  • 264
4

Are you looking for a 'newspaper column' style? Once the first (fixed size) text-box fills, the text should continue into the other and so on?

If so, why not display your long string into a WebView where you can more easily achieve this using HTML and CSS?

Valentin Galea
  • 1,084
  • 8
  • 18
  • Yes, this would be a good solution. The only problem would be,if I would have a really long article, and in this case I would like to put it on multiple columns and views inside a ViewPager. – Ovidiu Latcu Dec 13 '11 at 08:50
3

use (Calculate text size according to width of text area)

Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height =  bounds.height();
text_width =  bounds.width();

to calculate how much is your text going to take space. and calculate view.getWidth() about how much is your view width... Then do the needful by dividing the text accordingly.

Community
  • 1
  • 1
Abhinava
  • 1,030
  • 9
  • 19