I'm working on an App with Android Studio(Java) and want a ScrollView inside a ScrollView.
If I scroll the small one to the end it automatically starts scrolling the big one, is it possibile to disable the scrolling function of the big one when scolling the small one?
You need to handle this by disabling the other ScrollView when scrolling the current one
Let's say you have scroll_view_parent and scroll_view_child
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.scroll_view_child).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Related
I have a linear layout in a fragment with a bunch of checkboxes and various edittext widgets inside it. Basically like a quiz. A bunch of multiple choice(checkboxes) and a dozen short answer(edittexts) questions.
What I would like is for users to be able to click an edittext, type in an answer, then press DONE or click anywhere else on the layout and have the widget lose focus and the keyboard hide. Currently I am overriding the setOnEditorActionListener and setOnFocusChangeListener methods of each edittext to give focus back to a main layout, and hide the keyboard respectively. Here is the code for an edittext instance called "input_7d":
final EditText input_7d = (EditText) thisview.findViewById(R.id.txtinput_7d);
final LinearLayout parentLayout = (LinearLayout) thisview.findViewById(R.id.main_layout);
input_7d.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
parentLayout.requestFocus();
}
return false;
}
});
input_7d.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(parentLayout.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
This is annoying to do for every edittext I add, and it means editing lots of code if I remove them or add more in the future. What I would like to do is have a custom edittext class that can return focus to it's parent view/layout and hide the keyboard, then use that instead of the built in edittext. I'm very new to this and I haven't been able to find a way for a custom edittext to pass focus back to it's parent layout. Is there a better way to get a bunch of edittexts to all have this behavior and not have it all "hardcoded" into my fragment class?
So I could not find a way to have a edittext pass focus back to it's parent layout from inside the view itself. So instead I have opted to just disable focusable property of it when it
1) It loses focus (user clicked outside the view on something focusable, ie. The parent layout)
2) Finishes it's edit(user presses Done action on soft keyboard)
Surprisingly neither of these actions by default remove focus and the cursor from a default editText. At least not inside my scroll views.
So I added these lines to a custom view(myEditText) that extends the editText view:
this.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
}
return false;
}
});
this.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
hideKeyboardFrom(context, v);
}
}
});
this.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
myEditText.setFocusableInTouchMode(true);
myEditText.setFocusable(true);
return false;
}
});
I find it really annoying that to get simple functionality like not having the cursor blinking at me always or having the view not take focus when changing fragments and such you have to do such a weird workaround. Making a view unfocusable unless it's focused in which case it is focusable but only until it isn't focused again just seems dumb. Still wondering if there is a better way to do this for a large number of edits in one layout.
you can set a touch listener for the root layout and then remove the focus whenever not needed for the view
findViewById(R.id.rootView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch() //check for y
return false;
}
});
I have WebView and other view behind/under.
How to control touches on WebView and pass behind to other view, but only these from specific area of WebView?
Is there maybe a function where you can catch events and other function that you can trigger these events on other view?
This code did the trick:
sourceView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
MotionEvent motionEvent = MotionEvent.obtain(event);
//these 3 lines is for bug when target and source has different on screen positions
sourceView.getLocationOnScreen(posTemp1);
targetView.getLocationOnScreen(posTemp2);
motionEvent.offsetLocation(posTemp1[0]-posTemp2[0], posTemp1[1]-posTemp2[1]);
targetView.dispatchTouchEvent(motionEvent);
return false; //true if we want sourceView to not handle it
}
});
I didn't include detecting specific area, but it's easy just test result of motionEvent getX() and getY();
As the title says, is there a method in libGDX which returns the touch pressure (similar to MotionEvent.getPressure() in Android) ?
Or is there any other way I can get touch pressure in my game which works on both android and iOS(using RoboVM)?
I can't help you with the iOS part
but for android there is a work around
you can do something like this to get the pressure
View view = initializeForView(game, config);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
getInput().onTouch(v, event);
game.setPressure(event.getPressure());
Log.v("log", "hooked " + event.getPressure());
return true;
}
});
and still have the complete event handling of libgdx
I'm having a problem with my on touch listener - it works fine when the animation isn't running but wont work during the animation.
In case it is relevant the only method I'm using is the onFling() method.
I set up the touch listener and animation(to make an image view move down the screen):
image.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
and the Animation:
Animation moveDown = new TranslateAnimation(image.getX(), image.getX(), 0, pxHeight-pxImageHeight/2);
I have an onAnimationListener set up and have tried moving the onTouchListener to different parts of the code (such as onCreate, the onStart method of the animation and others)
In short, I want to be able to swipe my image during the animation of the imageView moving down the screen
Thanks
I was wondering how one uses OnTouchListener. (I'm using Android Studio.)
This is my code and when I press the "Vibrate" button, the button image doesn't change to it's pressed state:
vibrateButton = (Button) findViewById(R.id.button_vibrate);
vibrationInstance = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
vibrateButtonPressed = false;
if (!(vibrationInstance.hasVibrator())) {
vibrateButton.setEnabled(false);
}
vibrateButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
vibrationInstance.cancel();
}
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
vibrationInstance.vibrate(vibrationPattern, 0);
}
return false;
}
});
Is this how one uses OnTouchListener and what is the need for return false;? Thanks!
I guess your application is crashing because you forgot to add the permission to the manifest file. Make sure this is in it: <uses-permission android:name="android.permission.VIBRATE" />
And, by the way, if you want to receive all the touch events occuring to the View, you should return true in the OnTouchListener.