views:

1727

answers:

2

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? Here is the code

final TextView tv = new TextView(this);

   tv.setBackgroundResource(R.drawable.splash);
   tv.setTypeface(face);
   tv.setTextSize(18);
   tv.setTextColor(R.color.BROWN);

   tv.setGravity(Gravity.CENTER_VERTICAL|

Gravity.CENTER_HORIZONTAL);

   tv.setOnTouchListener(new OnTouchListener(){


           public boolean onTouch(View v, MotionEvent e)
           {


                   Random r = new Random();
                   int i = r.nextInt(101);

                   if (e.getAction() == e.ACTION_DOWN)
                   {
                           tv.setText(tips[i]);
                           tv.setBackgroundResource(R.drawable.inner);


                   }

           return true;
           }

   });

   setContentView(tv);

} }

A: 

Stumbled upon this by chance: I might be waking up a zombie here. But to answer this question you can either:
1. Surround the TextView by a ScrollView OR
2. set the Movement method to ScrollingMovementMethod.getInstance();

Samuh
+5  A: 

You don't need to use a ScrollView actually. Just set the android:maxLines and android:scrollbars = "vertical" properties of textview in layout xml file. Then use the TexView.setMovementMethod(new ScrollingMovementMethod()) in the Code. Bingo!!!! It scrolls automatically w/o any issues.

Amit Chintawar
Thanks a lot Amit!
Maxood