TextView Scrollable - android

I set my TextView to be scrollable:
textView.setMovementMethod(new ScrollingMovementMethod());
But, when I update my TextView's texts, the scrolling position keeps at the last text position, resulting in, sometimes, the text getting invisible and then, I need to move the scroll to see the new text (I update with: textView.setText(newText)).
I tried those codes, but no changes were noted:
textView.invalidate();
textView.requestLayout();
textView.scrollBy(0, 0)
textView.scrollTo(0, 0);
P.S.: the textView is a child of a RelativeLayout.

Why don't you just use a ScrollView in xml will be much easier open the ScrollView open a layout set it how you want it to be horizontal or vertical put your TextViews in and then close first the layout and then ScrollView and Voila done..

What I did to solve my problem:
textView.post(new Runnable() {
#Override
public void run() {
textHeight = textViews.getLineHeight() * textViews.getLineCount();
if (textHeight > relativeLayout.getHeight())
textView.setMovementMethod(new ScrollingMovementMethod());
else
textView.setMovementMethod(null);
}
});

Related

How to detect motions on a scrollable textview

I have a textview which only shows 3 lines and then you have to scroll to the other ones.
I set the textview in my xml file as scrollable and activated its scrolling method with textview.setMovementMethod(new ScrollingMovementMethod()).
The scrolling works perfectly fine.
The problem I face is, that I need to know when the user has scrolled to the bottom of the textview in the case that the textview has more than 3 lins. Is there a way to check when the user has scrolled to bottom of the textview?
I played a little bit around with the OnTouchListener, OnDragListener and OnHoverListener but none of them really worked.
If you need more details, just let me know.
Thanks for your help!
You can try this :
scrollView.getViewTreeObserver()
.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (scrollView.getChildAt(0).getBottom()
<= (scrollView.getHeight() + scrollView.getScrollY())) {
//scroll view is at bottom
} else {
//scroll view is not at bottom
}
}
});
Best

Scrollable and Clickable textview inside gridview android

I have a grid view inwhich I inflate two layouts one with {background imageview containing a imageview and two textviews aligned vertically over the image} and another layout with {background image containing three textviews aligned vertically over the image}.
I have restricted maximum number of lines in textview. Please suggest how to make the textview scrollable in the gridview and I want the gridview items to be clickable which will navigate to next screen on item click.
I tried to make the scrollable textview working inside the gridview using the requestDisallowInterceptTouchEvent method. But i didn't get the expected result. When the setscrollingmovement() method is used, the textview goes unclickable and so this is also not working.
I am not able to post the scenario as image, as I don't have the enough reputations to post image. Image uploaded in google drive.Please use below link to view the image.
https://drive.google.com/file/d/0B45x-sgFopMBMUJOaHk0NWlkMzQ/edit?usp=sharing
I got it working :-) But partially. I used android:descendantFocusability="blocksDescendants" in the root layout of gridview item's custom layout (inflating layout). The thing is onclick works outside of the textiew.
So, the textview scrolls and clicking outside of the textview somewhere on background image fires grid view's onClickListener()
But not able to trigger gridview's onclicklistener() when clicking on textview. Need to look into this issue.
Used suggestion from the stackoverflow link: Referred Link
Edited: Scrollable and clickable textview in gridview - Got Workaround using onTouchlistener
Setting below ontouch click listener for the textview inside the adapter class for gridview will allow you to scroll text as well as fire onItemClick of GridView.
OnTouchListener aListener = new OnTouchListener() {
long startTouchTime = 0;
#Override
public boolean onTouch(View v,
MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("ACTION_DOWN",
"ACTION_DOWN, intercept true");
startTouchTime = (System
.currentTimeMillis());
// To scroll the textview set this
textview
.setMovementMethod(new ScrollingMovementMethod());
v.getParent()
.requestDisallowInterceptTouchEvent(
true);
break;
case MotionEvent.ACTION_UP:
Log.e("ACTION_UP",
"Call onitemclick"
+ ((System
.currentTimeMillis()) - startTouchTime));
if ((System.currentTimeMillis())
- startTouchTime < 200)
// To call gridview onItemClick here
myGridActivity.myGridView
.getOnItemClickListener()
.onItemClick(
null,
myGridActivity.myGridView,
(Integer) imageView
.getTag(), // Setting the position here through imageview set tag
(long) 6767);
break;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
Log.e("ACTION_MOVE",
"ACTION_MOVE, intercept true");
v.getParent()
.requestDisallowInterceptTouchEvent(
true);
} else {
Log.e("Other", "Intercept false");
v.getParent()
.requestDisallowInterceptTouchEvent(
true);
}
return false;
}
};
textview.setOnTouchListener(aCBInfoListener);
Set android:scrollbars="vertical" for the textview to be scrolled.
Try this:
Properties of your TextView in your layout's xml file:
android:maxLines = "INTEGER"
android:scrollbars = "vertical"
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
in your code.
It will work fine..
or else you can put the TextView into the ScrollView:
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testin testin testing "></TextView>
</ScrollView>

How to move the control to the bottom of a TextView in ScrollView ( Scroll to bottom)

In my layout I have a TextView inside a ScrollView. But I am unable to scroll to the bottom of the TextView programmatically.
I have tried with setSelection(int) but the method is undefined for TextView. So can any one help me.
Thanks in advance.
Ok finally i got the work around.
TextView tv = (TextView)findViewById(R.id.textView);//my text view
ScrollView sv = (ScrollView) findViewById(R.id.scrollView);//my scrollview
String log ="a Very long text";
tv.setText(log);
sv.post(new Runnable() {
public void run() {
sv.scrollTo(0, tv.getHeight());
}
});

Reset scroll in ScrollView

I have a ScrollView (and a LinearLayout within it) set as main content. When the user scrolls the view further down, and then if I replace it's child (LinearLayout) with another LinearLayout, the view remains in the scrolled positioned.
How do I reset the ScrollView back to coordinate 0?
Why not just use scrollTo(0, 0);?
Link: http://developer.android.com/reference/android/view/View.html#scrollTo(int%2C%20int)
If u want to clear the scrollview u can use
removeAllViews();
Its all in the documentation... http://developer.android.com/reference/android/widget/ScrollView.html search for fullScroll(int)
When scrollView.scrollTo(0, 0) & scrollView.fullScroll(View.FOCUS_UP) doesn't work, you can accomplish the intended with:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, 0);
}
});
yourScrollView.scrollTo(0,0) will do the job just fine.
Just use
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViewMain);
hscrollViewMain.scrollTo(0, 0);

TextSwitcher bug

i have a problem with the TextSwitcher.
In my app i have 3 Textswitcher with wrap_content (width and higth) inside an LinearLayout!
Each TextSwitcher is declared with an in and outAnimation.
My problem:
When the text is switch the first time, i have spaces between the elements!
like: 1.23 45 678
but it has to: 1.2345678 without spaces
when the text switches the second or 3. time the spaces are gone.
I cant imagine why there are spaces?? There is no other Element, no padding, no margin.
maybie the reason is the ViewFactory:
ViewFactory for the middle part switche:
public View makeView() {
TextView t = new TextView(SmartTraderFxTrading.this);
t.setTextColor(Color.BLACK);
t.setTypeface(null, Typeface.BOLD);
t.setTextSize(26f);
return t;
}
an for the switcher left and right from the middle part:
public View makeView() {
TextView t = new TextView(this);
t.setTextColor(Color.DKGRAY);
t.setTextSize(20f);
return t;
}
but i can change everything, the spaces are always there after the first switch, and also maybie after the second.
Is it a bug? Please help!
EDIT:
Problem solved!
The problem is that the Text befor has more digits as after! So the TextSwitcher has a larger width and after redraw of the activitie it wraps the TS.

Categories

Resources