Android multi-column text

Is there a way to show long text in 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 libraries that can help me, or do you know how I can approach this problem?

Any suggestion is welcome. Thanks.

EDIT: The problem is the line break, not the layout. I know that I can use TableLayout ... or weights ... to evenly distribute columns, etc. The problem is how to correctly split the String . Maybe 2 columns will be filled, and the third half? I do not know how to approach this, and not the actual location.

+4
source share
3 answers

Check TableLayout . To split the text, you can split the text after 1/3 of your number of characters. Of course, you need to split the text into one whitespace hacker. UPDATE: See also my sample code at the end of my post.

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 separation, you can test this code. The algorithm might be a little better closer to the separation 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; } 
+5
source

Are you looking for a newspaper column style? As soon as the first (fixed size) text box is filled, should the text continue in another, etc.?

If so, why not display a long line in a WebView , where you can easily achieve this with HTML and CSS?

+4
source

use ( Calculate text size according to the width of the 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 of your text will take up space. and calculate view.getWidth () about how much your viewing width is ... Then do what you need by dividing the text accordingly.

+3
source

Source: https://habr.com/ru/post/1385161/


All Articles