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);
The code below creates an automatic horizontal scrolling textview:
While adding TextView to xml use
Set the following properties of TextView in onCreate()
It is not necessary to put in
You can do your work by simply adding:
And, put this code in your Java class:
This will provide smooth scrolling text with a scroll bar.
If you don't want to use the EditText solution then you might have better luck with:
XML - You can use
android:scrollHorizontally
AttributeWhether the text is allowed to be wider than the view (and therefore can be scrolled horizontally).
May be a boolean value, such as "true" or "false".
Prigramacaly -
setHorizontallyScrolling(boolean)
If you want text to be scrolled within the textview, then you can follow the following:
First you should have to subclass textview.
And then use that.
Following is an example of a subclassed textview.
Now, you have to use that in the XML in this way:
That's it.