How to detect if cardview located in scrollview(linearlayout) went beyond the screen Or returned back to the field of view?
I tried to do this through OnScrollChanged But it seems to me that this is not the case, and it is impossible to get the correct algorithm of work
You have to use the method getLocalVisibleRect() on your CardView:
Rect scrollViewRect = new Rect();
// Get the visible Rect of the ScrollView.
scrollView.getHitRect(scrollViewRect);
// Check if CardView's Rect is inside it.
if (!cardView.getLocalVisibleRect(scrollViewRect)) {
// The CardView is not visibile anymore.
}
You can place this code wherever you want to check if the CardView is not visible anymore on the screen.
Related
I want to know when a view of my layout is showing in physical screen. You know in some apps that uses ScrollView, you must scroll down to see other views. I want to know when a view like Button or TextView or another view is showing in physical screen to do some job. Thanks in advance:)
Try this:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (view.getLocalVisibleRect(scrollBounds)) {
// Any portion of the view, even a single pixel, is within the visible window
} else {
// NONE of the view is within the visible window
}
I have to implement slidable menu in my app. User should be able to show/hide it by tapping on it and move left/right.
To implement this, I`ve created custom SlidableFrameLayout that extends FrameLayout.
By overriding onTouchEvent method I calculate slide position and call
private void setLeftLayout(int left) {
layout(left, getTop(), getRight(), getBottom());
}
to change position for SlidableFrameLayout. Everything works fine, but when I keyboard shows (after focusing EditText), SlidableFrameLayout changes its left value to 0, so if menu was closed, it becomes opened. The same thing happens when I hide the keyboard (generally, it appears in all actions that prevents calling layout methods, I think).
I can not invent how to fix it. Could you help me? If any additional info is required, I`ll attach it. Thanks
I think your issue is related to calling layout directly. When the container view changes size as a result of the keyboard being shown/hidden onMeasure/onLayout will be called with the view's default bounds. In your case I'm guessing the left bound is 0, so layout is being called with 0 for left every time your window resizes.
Instead you probably want to use something like setTranslationX or scrollTo ..
Good luck!
As a result, I changed my logic:
private void setLeftLayout(int left) {
FrameLayout.LayoutParams params = getFrameLayoutParams();
params.leftMargin = left;
params.rightMargin = -left;
requestLayout();
}
In my case, view is a parent of FrameLayout, so I haven`t added any conditions in 1st line of the method. This method works pretty fine.
I want to make a view( a button actually) which is inside a scrollview , but when the user scolls down and the button is going up, it moves up only till it reaches the top of the visible screen and then stays there like a fixed header until the user scrolls up again and then it returns to its original position.
I have given the screens for a better understanding.
One way I have solved this is by copying the same view outside the scrollview and keeping it hidden. Only to make it visible when the old button is visisble again.
#Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (mPriceBtn.getLocalVisibleRect(scrollBounds)) {
// View is within the visible window
mPriceHiddenBtn.setVisibility(View.GONE);
} else {
// View is not within the visible window
//mPriceBtn.setY(y);
mPriceHiddenBtn.setVisibility(View.VISIBLE);
}
}
Check out this tutorial. It's about an ActionBar with similar behavior, made by ome of Google's own Android devs. I'm guessing you can find most of what you need there.
I am implementing a timeline UI, which is basically a ListView. The tricky part is when the user scrolls the ListView, it will display a text box(shows the time slot, say 9:00 am ) next to the scrollbar. It is on the left side, adjacent and will move with the scrollbar.
I am using the OnScrollListener to detect the scroll event. But I don't know how to get the exact position of the scroll bar (top, mid point, bottom), so that I can put the text box at the right place. There are many posts about getting the position of the top item but seems not for the scrollbar itself.
Any thoughts?
BTW, sorry for not attaching a screenshot of the UI. Stackoverflow doesn't allow me to do that since I am a new user.
But I don't know how to get the exact position of the scroll bar (top,
mid point, bottom), so that I can put the text box at the right place
You probably want something like the Path application. This tutorial it what you're looking for(probably, if you didn't see it yet).
Basically, the ListView has some methods regarding the scrollbars position(also size) that are used but those methods are not available from outside as they are declared as protected, so you need to extend the ListView class. Those methods are ListView.computeVerticalScrollExtent(), ListView.computeVerticalScrollOffset() and ListView.computeVerticalScrollRange().
Using those methods you should be able to figure where the scrollbars are placed at a particular moment in your listener.
getListView().getLastVisiblePosition()
listview.post(new Runnable() {
public void run() {
listview.getLastVisiblePosition();
}
});
View.getScrollY()
getScrollY:
Return the scrolled top position of this view. This is the top edge of the displayed part of your view. You do not need to draw any pixels above it, since those are outside of the frame of your view on screen.
Returns
The top edge of the displayed part of your view, in pixels.
Also refer : http://eliasbland.wordpress.com/2011/07/28/how-to-save-the-position-of-a-scrollview-when-the-orientation-changes-in-android/
I have recently encountered a very specific issue/requirement regarding an Android Button and ListView item. The requirement is something like this:
we need a button that can display a glow/aura of a specific dimension and opacity when clicked, however this glow/aura must not be part of the button itself and needs to be displayed over the neighbouring views.
same requirement for list view items - they need to glow over their neighbouring items and the list view margin.
Any idea/suggestion would be highly appreciated.
Thanks.
A solution could be in drawing a round rectangle with stroke around the button. You can get the button position using View getTop(), getLeft(), getRight() and getBottom() methods.
The round rectangle could be a custom view that has the onDraw() method overridden. You can also have a paint attribute, in this view, if you what to add the gradient and opacity, to look more like an aura. When you first create the view it should be invisible or transparent and only on demand it should appear.
From your main activity you have to add this new view, by using the layout.addView() method and depending on your demands you can make it visible and/or change their properties.
An example can be found here: https://github.com/codesorcerers/auraview
Hope this helps!
Bogdan Popa