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();
Related
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;
}
});
I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or disactivable.
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button. When I was pressing it, nothing happened of course, so I searched there and there how to make it change when pressed This worked
casedanger1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
casedanger1.setImageResource(R.drawable.casedanger1slct);
break;
case MotionEvent.ACTION_UP :
casedanger1.setImageResource(R.drawable.casedanger1);
break;
}
return false;
}
});
But when I try to disable again the button, it doesn't revert to the standard image (casedanger1)
How should I proceed ? I've been searching for days without a real solution. I have tried to make it a toggle button, which works, but makes the image bigger, and thus cropped.
Any hints that could help ? I'm really desperated, this is not something I'm familiar with.
Thank you
-Pliskin
I think you were close. Try this
casedanger1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = false;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
casedanger1.setImageResource(R.drawable.casedanger1slct);
} else {
casedanger1.setImageResource(R.drawable.casedanger1);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
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;
}
});
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 am learning how to make android apps, and I can't figure out how to implement a kind of OnMouseMoved event in android.
I've tried using an OnTouchListener, but It doesn't update when I touch, hold and move. It only updates when I tap a different points. Like this:
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
thread.getGameState().touch(x);
return false;
}
I've tried this
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
thread.getGameState().touch(x);
}
return false;
}
But this doesn't even respond at all to touch, maybe the point i'm getting from the event is an old point, and not the new drag point?
thread.getGameState().touch(x); sets the x-value of and object, which should make it like dragging.
You must return true in onTouch() to tell Android that you will handle the complete touch event, so you can receive events after ACTION_DOWN:
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
thread.getGameState().touch(x);
}
return true;
}