Skip to main content

Making TextView scrollable on Android ?


I am displaying text in a textview that appears to be too long to fit into one screen. I need to make my TextView scrollable. How can I do that?


A1. in XML

      <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <TextView
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"/>
    </ScrollView>

</LinearLayout>

A2. You can do your work by simply adding:

      android:scrollbars = "vertical"

     And, put this code in your Java class:

      textview.setMovementMethod(new ScrollingMovementMethod());


A3.  In Java

      This will provide smooth scrolling text with a scroll bar.

      ScrollView scroller = new ScrollView(this);
      TextView tv = new TextView(this);
      tv.setText(R.string.my_text);
      scroller.addView(tv);

  XML Side:

  1.  <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        tools:context="com.mbh.usbcom.MainActivity">
        <TextView
            android:id="@+id/tv_log"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="Log:" />
       </RelativeLayout>

   JavaSide :

        tv_log = (TextView) findViewById(R.id.tv_log);
        tv_log.setMovementMethod(new ScrollingMovementMethod());

A4.Add the following in the textview in XML.

         android:scrollbars="vertical"

In the Java file.

        textView.setMovementMethod(new ScrollingMovementMethod());


A5.The code below creates an automatic horizontal scrolling textview:

    TextView to xml use

         <TextView android:maxLines="1" 
          android:ellipsize="marquee"
          android:scrollHorizontally="true"/>

    In Java

       tv.setSelected(true);
       tv.setHorizontallyScrolling(true); 

Comments

Popular posts from this blog

Reset Android textview maxlines ?

Reset Android textview maxlines  ? A1.         Actually, the way android platform does that is by setting the MaxLine to                                            Integer.MAX_VALUE. textView . setMaxLines ( Integer . MAX_VALUE );        also, if you are using Ellipsize, don't forget to set to null. textView . setEllipsize ( null );       just check how the android framework do just that ;) watch the                                                              setMaxLines(Integer.MAX_VALUE); private void applySingleLine ( boolean singleLine , boolean applyTransformation ) { mSingleLine = singleLine ; if ( singleLine ) { ...

Android set the gravity for a TextView programmatically

Android set the gravity for a TextView programmatically  A1.        This will center the text in a text view: TextView ta = ( TextView ) findViewById ( R . layout . text_view ); LayoutParams lp = new LayoutParams (); lp . gravity = Gravity . CENTER_HORIZONTAL ; ta . setLayoutParams ( lp ); A2.      textView . setGravity ( Gravity . CENTER | Gravity . BOTTOM ); A3.              Use this code TextView textView = new TextView ( YourActivity . this ); textView . setGravity ( Gravity . CENTER | Gravity . TOP ); textView . setText ( "some text

Android and   in TextView ?

Android and &nbsp; in TextView ? A1.            The TextView should respect the non breaking space <string name = "test" > Hello&#160;world </string>                                     or new TextView ( "Hello\u00A0world" ); A2.      One unique situation I ran into was adding a non-breaking space to a string resource that took               String.format parameters. <resources> <string name = "answer_progress" formatted = "false" > Answered %d of %d </string> </resources>       I tried to simply copy and past the non-breaking space character into the string and it was                     replaced       with a regular old space after compilin...