I had an edit text which I wanted to make scrollable. The problem was that this edit text is located inside a scrollable view.
I searched the internet and found a solution that works fine. Source:How to scroll the edittext inside the scrollview
youredittext.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (youredittext.hasFocus()) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_SCROLL:
v.getParent().requestDisallowInterceptTouchEvent(false);
return true;
}
}
return false;
}
});
The problem is after applying this solution the edit text would only scroll without a "velocity vector." So like you have to hold down your thumb and move it but the moment you remove your thumb it stops scrolling immediately.
While for example, the android scroll view has a velocity vector in which you scroll up fast then you remove your thumb, it would keep scrolling with decreasing speed. Similar to scrolling on Facebook and Instagram.
Any help is appreciated and thanks!
Related
I have a RecyclerView where I want to enable Swipe to Delete/Star on items. My item is a FrameLayout where a MaterialCardView is on the top and the revealed star and archive layouts are under it.
I already made the swipe behavior work using onTouch method applied on the CardView only, but it's just way too hard to scroll across the list or use onClick or onLongClick as onTouch overrides them. The only way to scroll the RecyclerView or invoke onClick or onLongClick is to move only in Y axis without moving even a half pixel in X, as moving in X will invoke an ACTION_MOVE event that will redirect all next touch events only to the CardView. (requestDisallowInterceptTouchEvent()) as the first statement of the switch case of ACTION_MOVE).
So I want to apply ItemTouchHelper or something similar on the CardView while having the ability to modify the way how the card X changes (To make it slower than user swiping speed like in irremovable notifications in Android) and get the MotionEvent that the user applies, and that's because ItemTouchHelper isn't very literal about what can be treated as a swipe, so it would allow onClick and onLongClick on small movements, and allow scrolling the list when the movement of Y axis is way greater than X's.
Please don't close this question saying "Too board" like a lot of other questions I had a chance to answer :(
i create a system overlay app using this way
but i have a problem .... when i move my button to corner of screen, i can't touch system's view like Call button in the following image
image
how can i disable any touch in my button ? ( ignore my view's touch and touch system's view )
in somewhere i find this code but it isn't work
bl.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Always visit developer.android.com first, it's really well documented with fundamental concepts.
The onTouch method will pass the event to the layer below it, if it returns false.
If you've extended a default touchable View class, you should use return super.onTouch()
Here's the link you're looking for:
https://developer.android.com/reference/android/view/View.OnTouchListener.html
in our app, I'm loading an initial webview. Then in order to allow users to see chat history, I want to add new webviews on top on that initial one. The way I do that now is to have a linear layout wrapping the initial webview; this LinearLayout is called webview_wrapper and is in a ScrollView. Then, using a ScrollViewListener interface, when the user scrolls up past a set coordinate I create a new webview, call it newWebView, and call webview_wrapper.addView(newWebView, 0), the problem I'm having is that I want to do the loading and adding of the webview off the screen, then the user can continue to scroll up. This adding and scrolling happens inside of an onAnimationEnd method of an AnimationListener(while I make the post request for webview).
I feel like I've tried every way like calling scrollTo or scrollBy after adding the view but it's only scrolling partially. What is the best way to do this?
This problem was eventually solved by using a OnGlobalLayoutListener on the containing ScrollView, inside of which I call scrollTo(0, webViewHeight) and removeOnGlobalLayoutListener(this). Right before that, I make the WebView visible, set its height via setLayoutParams(view must be visible first).
Please correct me if I'm wrong but this probably works as seamlessly as it does because of the OnGlobalLayoutListener. When the view is made visible and the height set, and right after we set an OnGlobalLayoutListener which fires the scrollTo() to the exact height of the new view when the scrollView is being laid out, the net result becomes the appearance of the view inflating off screen.
final int newHeight = (int) ((height / 380.0) * webView.getWidth());
// Must set visible before setting layout params
currentOldView.setVisibility(View.VISIBLE);
// Without this line, view will bounce around as it attempts to self set the height based on HTML content
currentOldView.setLayoutParams(new LinearLayout.LayoutParams(webView.getWidth(), newHeight));
// Use viewTreeObserver to wait until scrollView completely laid out before attempting to scroll
ViewTreeObserver observer = scrollView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
scrollView.scrollTo(0, newHeight); // Because spinner_wrapper still on view, will scroll to an overlap point.
canAddOldChat = true;
currentOldView.startAnimation(fadeInAnim);
if (isLastOldChat) spinner_wrapper.setAlpha(0.0f);
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
I am working on a simple converter between Decimal/Hex/Binary and have run into an issue I can't seem to solve. This is the layout I am more or less aiming for.
What I am here is when one of the buttons is pressed, it stays pressed to indicate which conversion you are currently working with. Now I have looked everywhere and it doesn't seem there is a good way of doing this, or I haven't found it at least.
What would be the best way to go about this? is there someway to just use buttons and have them stay in their "pressed" state when they are clicked? By this I only need the color of the pressed button to show. Or is there a way of doing this with some type of radio group, where the radio buttons have the same style as a regular button?
Try:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
... do a call to your conversion code here ....
button.setPressed(true);
return true;
}
});
For more info: Android docs and this SO question
I'm very new to Android programming, and trying to understand touch events with nested views. To start, here's a description of my app:
I have a relative layout that I've added via the GUI editor. Everything is default. I've also created a class called ClipGrid that extends ScrollView. Nested inside that, I make a HorizontalScrollView. Inside of that, I make a TableLayout and it's rows. The rows contain buttons.
The end result is a grid of buttons. It displays 4x4 at once, but can scroll either direction to display other buttons.
I call it to the screen from my main activity like this:
ClipGrid clip_grid = new ClipGrid(this);
setContentView(clip_grid);
I did that just for testing purposes, and I think I will have to change it later when I want to add other views to my relativelayout. But I think it might have implications for touch events.
in the end, I want to detect when the grid has been moved and snap the newly viewable 4x4 grid of buttons to the edge of my layout when the user lifts their finger. I'm just not sure how to go about implementing this and any help would be appreciated. Thanks.
The way touch events are handled is kind of a cascading effect that starts from the top view and goes down to the lower nested views. Basically, Android will pass the event to each view until true is returned.
The general way you could implement the onTouchEvent event of a View would be:
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean actionHandled = false;
final int action = event.getAction();
switch(action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// user first touches view with pointer
break;
case MotionEvent.ACTION_MOVE:
// user is still touching view and moving pointer around
break;
case MotionEvent.ACTION_UP:
// user lifts pointer
break;
}
// if the action was not handled by this touch, send it to other views
if (!actionHandled)
actionHandled |= super.onTouch(v, MotionEvent.event);
return actionHandled;
}