Search code examples
javaandroiduser-interfacestring-formatting

Can I easily display text on alternating lines in Android/java/


I have an ambitious formatting problem which has several parts and I have only a tyro’s grasp of Android programming. I want to display text from two different files in alternating lines:

This is text from the first file, which is different from the second file. The 
This is the text from the second file. Notice that it isn’t a lot like the first

differences are pretty great, but take (overall) nearly the same number of 
file, but has a similar number of characters. The contents of the second 

characters, plus or minus about 5%
file should be nearly the same in length.

The idea is to have this display on a phone at reasonable (and adjustable) font size, with the length of the line limited by the size of the text boxes. I think I need to figure out for each text box how many characters will fit, find a method to write that many (or fewer, splitting at space or . or ! or ?) and continuing on the next text box. As the files are pretty long, I would need a method to either scroll (seems unlikely) or whole-page refresh the displays.

My questions:

  1. Does my plan of attack seem reasonable?
  2. Is there a library which could make this process simpler?
  3. If this attack is really silly, can anyone suggest a better method?

Solution

  • Even if your idea is a viable approach, let me suggest yet another option: take advantage of the fact that you can place two TextViews in almost the same place in Android

    Each TextView can show lines of one of the files. Both will have a line height three times as much as the font size requires (use android:lineSpacingMultiplier). If you place the second TextView a little bit lower than the first one (use android:marginTop), then you will get a pattern like the following:

    enter image description here

    layout xml example:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        tools:context=".PlusOneFragment">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="16sp"
            android:lineSpacingMultiplier="3"
            android:text="@string/textview1_text" />
    
        <TextView
            android:layout_marginTop="18dp"
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="16sp"
            android:lineSpacingMultiplier="3"
            android:text="@string/textview2_text" />
    
    </FrameLayout>
    

    strings.xml contains

    <string name="textview1_text">This is text from the first file, which is different from the second file. The differences are pretty great, but take (overall) nearly the same number of characters, plus or minus about 5%</string>
    <string name="textview2_text">This is the text from the second file. Notice that it isn’t a lot like the first file, but has a similar number of characters. The contents of the second  file should be nearly the same in length.</string>