Search code examples
androidtextviewscrollableright-align

Scrolling TextView repaint problem


I was searching for a way to make TextView scrollable. I've found an answer to this question, involving using specific XML attributes...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/textview" android:text="The long text, which no way have chance to fit within screen's width"
        android:maxLines="1"
        android:scrollHorizontally="true" />
</LinearLayout>

...and a small piece of source code...

package spk.sketchbook;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.widget.TextView;

public class SketchbookMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView = (TextView)findViewById(R.id.textview);
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());
    }
}

...and everything works like charm, but only until one tries to align text inside TextView to the right...

android:gravity="right|center_vertical" 

...to observe, that until user tries to scroll the text, it is invisible (probably scrolled out of the view).

Unfortunately, this is just what I need to do (make right-aligned TextView scrollable).

Any ideas please?


Solution

  • Here is a different approach:

    <?xml version="1.0" encoding="utf-8"?>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/textview" 
            android:gravity="right|center_vertical"  
            android:text="The long text, which no way have chance to fit within screen's width"
            android:maxLines="1"/>
    </HorizontalScrollView>
    

    I think that you don't need any code to make this work