0

Is there any solution to have 2 textview like this :

image

in fact i want to show Text2 lines below the Text1 lines. For exmple to show the translation Text1.

Any solution?

3 Answers3

1

You can use setLineSpacing to set a big enough gap in your TextViews and overlap them, then translate it so one shows up in between the lines of the other.

Kathy
  • 263
  • 1
  • 2
  • 7
  • Just to add some more detail, you'll have to adjust the margins on both TextViews and you'll want to use a layout that allows you to overlap Views as well as setting the View background to transparent but in on the whole this is probably one of the easiest ways of achieving the effect. – TheIT Dec 13 '13 at 04:21
1

Try this code. You can set lineSpacingExtra property of TextView and can use relative layout to overlap them. After this you cna set properties of your text view accordingly

In your xml define text view properties like this

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Text 1. This is Text1. This is Text 1. This is Text1. This is Text 1. This is Text1"
        android:lineSpacingExtra="20dp"
        android:textColor="#5F4C0B"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:lineSpacingExtra="26dp"
        android:text="This is Text 2. This is Text2. This is Text2. This is Text2. This is Text2. This is Text2"
        android:textColor="#B40404"
        android:textSize="20sp" />

</RelativeLayout>

You will get output like this

enter image description here

Accept the answer if you found it useful

Vaibhav Agarwal
  • 4,499
  • 3
  • 19
  • 20
0

You can dynamically create this view,

In Main Activity write the following code

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            RelativeLayout rl = new RelativeLayout(this);
            RelativeLayout.LayoutParams text1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            text1.setMargins(0, 0, 0, 0);
            RelativeLayout.LayoutParams text2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            text2.setMargins(0, 26, 0, 0);

            LinesTextView tv1 = new LinesTextView(this, null);
            tv1.setText("This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1.");
            tv1.setLineSpacing(20.0f, 1.0f);
            tv1.setLayoutParams(text1);
            tv1.setTextSize(24.0f);
            tv1.setTextColor(getResources().getColor(android.R.color.darker_gray));

            LinesTextView tv2 = new LinesTextView(this, null);
            tv2.setText("This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2.");
            tv2.setLineSpacing(25.0f, 1.0f);
            tv2.setLayoutParams(text2);
            tv2.setTextSize(18.0f);
            tv2.setTextColor(getResources().getColor(android.R.color.black));
            rl.addView(tv1);
            rl.addView(tv2);

            this.setContentView(rl);
        }
    }

In LinesTextView class write the following code

public class LinesTextView extends TextView
{
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinesTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
} 

Check Screen shot

enter image description here

Set Text color, style, margin and size accordingly.

Vaibhav Agarwal
  • 4,499
  • 3
  • 19
  • 20