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
Related
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);
}
});
As I've a master in MS Paint, I will just upload a picture selfdescripting what I'm trying to achieve.
I've searched, but I'm not really sure what do I've to search. I've found something called Animations. I managed to rotate, fade, etc an element from a View (with this great tutorial http://www.vogella.com/articles/AndroidAnimation/article.html)
But this is a bit limited for what I'm trying to achieve, and now, I'm stuck, because I don't know how is this really called in android development. Tried words like "scrollup layouts" but I didn't get any better results.
Can you give me some tips?
Thank you.
You can see a live example, with this app: https://play.google.com/store/apps/details?id=alexcrusher.just6weeks
Sincerely,
Sergi
Use something like this as your layout (Use Linear, Relative or other layout if you wish):
<LinearLayout
android:id="#+id/lty_parent">
<LinearLayout
android:id="#+id/lyt_first" />
<LinearLayout
android:id="#+id/lyt_second"/>
</LinearLayout>
And then in an onClick method on whatever you want to use to control it, set the Visibility between Visible and Gone.
public void buttonClickListener(){
((Button) findViewById(R.id.your_button))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (lyt_second.getVisibility() == View.GONE) {
lyt_second.setVisibility(View.VISIBILE);
}
else {
lyt_second.setVisibility(View.GONE);
}
});
Which is fine if you just want a simple appear/disappear with nothing fancy. Things get a little bit more complicated if you want to animate it, as you need to play around with negative margins in order to make it appear to grow and shrink, like so:
We use the same onClick method that we did before, but this time when we click it starts up a custom SlideAnimation for the hidden/visible view.
#Override
public void onClick(View v) {
SlideAnimation slideAnim = new SlideAnimation(lyt_second, time);
lyt_second.startAnimation(slideAnim);
}
The implementation of the SlideAnimation is based on a general Animation class, which we extend and then Override the transformation.
public SlideAnimation(View view, int duration) {
//Set the duration of the animation to the int we passed in
setDuration(duration);
//Set the view to be animated to the view we passed in
viewToBeAnimated = view;
//Get the Margin Parameters for the view so we can edit them
viewMarginParams = (MarginLayoutParams) view.getLayoutParams();
//If the view is VISIBLE, hide it after. If it's GONE, show it before we start.
hideAfter = (view.getVisibility() == View.VISIBLE);
//First off, start the margin at the bottom margin we've already set.
//You need your layout to have a negative margin for this to work correctly.
marginStart = viewMarginParams.bottomMargin;
//Decide if we're expanding or collapsing
if (marginStart == 0){
marginEnd = 0 - view.getHeight();
}
else {
marginEnd = 0;
}
//Make sure the view is visible for our animation
view.setVisibility(View.VISIBLE);
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Setting the new bottom margin to the start of the margin
// plus the inbetween bits
viewMarginParams.bottomMargin = marginStart
+ (int) ((marginEnd - marginStart) * interpolatedTime);
// Request the layout as it happens so we can see it redrawing
viewToBeAnimated.requestLayout();
// Make sure we have finished before we mess about with the rest of it
} else if (!alreadyFinished) {
viewMarginParams.bottomMargin = marginEnd;
viewToBeAnimated.requestLayout();
if (hideAfter) {
viewToBeAnimated.setVisibility(View.GONE);
}
alreadyFinished = true;
}
hideAfter = false;
}
}
EDIT: If anyone had used this code before and found that if you click on the button that starts the animation more than once before the animation was finished, it would mess up the animation from then on, causing it to always hide the view after the animation finished. I missed the reset of the hideAfter boolean near the bottom of the code, added it now.
you can do this manually by using setvisibility feature on the event onClick()
or
use this
dynamically adding two views one below other
I have a layout in which i have two button:
1)back
2)help
and ScollView in which i place two image Views:
1) firstImageVeiw
1) SecondImageVeiw
first image View will be displayed on front and second is placed beneath the 1st image.
Now i want that if i click on help button the the scrollView automatically slides to 2nd image view.
Any help or suggestion.
You can use scrollview's scrollTo() method
helpbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourscrollview.scrollTo(0, yourImageView.getTop());
}
});
At runtime you can get the location of every view object with
getleft()
getTop()
getRight()
getBottom()
i have Horizontal scrollview with 50 controll,as i am scroll it will display 50 gracefully.but at initially user not able to understand that this is scrollview and "I need to scroll for next item". so i have simply put two image at Right side and left side as you can see in image.now my problem is "i don't able to understand that how can i invisible left side arrow to visible & invisible right side arrow".
Edit I just realized how old this post is, but maybe it will help someone in the future.
Alright this is my first post so hopefully I can format it correctly. I actually had this exact same problem so I'll post my solution for it. I used images for the arrows.
hsv = (HorizontalScrollView) findViewById(R.id.HorizontalScrollView);
left_arrow = (ImageView) findViewById(R.id.left_arrow);
right_arrow = (ImageView) findViewById(R.id.right_arrow);
hsv.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
// TODO Auto-generated method stub
//Checks if your horizontalscrollview can scroll left
if (!hsv.canScrollHorizontally(-1))
{
left_arrow.setVisibility(View.INVISIBLE);
}
else
{
left_arrow.setVisibility(View.VISIBLE);
}
//Checks if the horizontalscrollview can scroll right
if(!hsv.canScrollHorizontally(1))
{
right_arrow.setVisibility(View.INVISIBLE);
}
else
{
right_arrow.setVisibility(View.VISIBLE);
}
}
});
I put this in my onCreate method and it works like a charm. The listener checks when you scroll and the canScrollHorizontally uses a postive / negative integer to determine if the view can scroll any more. Positive for right, negative for left.
I have a webview in a scrollview, when the Activity loads, it forces my scrollview to the bottom (where the webview is) once the webview finishes "loadData".
How do I keep this from happening?
I've tried this, but it jumps the screen up and down, which I don't want:
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.post(new Runnable()
{
#Override
public void run()
{
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.fullScroll(ScrollView.FOCUS_UP);
}
});
The answer of fhucho is OK for stoping the webview from scrolling to bottom. But you will lose all accessibility with trackball, etc. So I found an improvement :
public class MyScrollView extends ScrollView {
#Override
public void requestChildFocus(View child, View focused) {
if (focused instanceof WebView )
return;
super.requestChildFocus(child, focused);
}
}
Hope it helps !
Create a custom ScrollView with this method:
#Override
public void requestChildFocus(View child, View focused) {
if (child instanceof WebView) return;
}
For focus related issues, I found this worked better, adding these to your WebView in XML:
android:focusable="false"
android:focusableInTouchMode="false"
Although for the actual question asked, just adding another view that gets the focus by default, instead of the WebView, should have resolved that issue. I think others like me might find this when trying to deal with other general issues with a WebView inside a ScrollView taking the focus. Adding the above to the XML does not stop hyperlinks in the WebView from working.
add this to your main layout
android:descendantFocusability="blocksDescendants"