Android custom keyboard in webview - android

I'm trying to implement a custom keyboard in Android. I want to input some text in my Webview. Since I don't want to display the keyboard when I hit everywhere in the Webview I was thinking of a listener of something like this:
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (((WebView) v).getHitTestResult().getType() == EDIT_TEXT_TYPE)
showMyKeyboardHere();
return false;
}
});
is my listener, it seems to return EDIT_TEXT_TYPE on the key press I do AFTER I've pressed the edit text field. So when I press the edit text field it returns UNKNOWN_TYPE and the click after that when pressed somewhere else returns EDIT_TEXT_TYPE. My guess would be that the OnTouchListener happens before the touch even gets sent on to the Webview it won't have registered it. Any way to change that?
Now to my question: Since I will only want my custom keyboard in this application, is there anyway to listen to whatever event is called to bring up the normal keyboard and immediately hide it and show my own?

Related

IsSelectable steals event from onClickListener on TextView

I have a TextView that has the isSelectable attribute set to TRUE but i also have a onClickListener on it because. I want if a person holds on the text the text to be selected and he can copy it but if he just clicks on it i want a screen to be opened.
this.subtitle.setTextIsSelectable(true);
this.subtitle.setOnClickListener(v -> openMyScreen());
So what happens is that the selection works fine but if you click on it, the first event is consumed somewhere and only when i click for the second time it works. Does any 1 have any idea how i can fix this.
So the issue is, when you click it once, the textview gets focused. That's what's consuming your click event. The only work around I have been able to find is using setOnFocusChangeListener on the textview, then check if the texview got focused, and use that as a click event.
textview.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Handle click event
}
}
});
Please note that you will still have to use OnClickListener as well to handle click events post focusing

Android onKeyDown() not execute on pressing delete button

I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

Automatic Android Click/Tap tracking

Is there a clever way to "do something" every time a user clicks or taps on the screen? I'd like to know which view they tapped on, and do it without having to replace all my standard views with custom views.
Basically, I want to add click tracking in my app to help with analytics. We already have page tracking (by using a shared superclass for every activity), but in some cases page tracking isn't enough and we actually want to track clicks.
How can I execute a function every time the user clicks on a View on the screen?
In each of your Activities, just add this at the end of onCreate (this is especially easy if all your Activities are subclasses of a custom Activity, because then you only need to write this once):
getWindow().getDecorView().findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
//handle your code here
return false;//don't absorb the touch.
}
});
Edit
The above code does not allow easy recognition of which Views are touched. The below code will. Note that this may break other onTouchListeners if the View in question already has registered an onTouch event. If not, this will work great. It uses the droidQuery library to select all views in the hierarchy and set uses code like above to handle touches without absorbing the event. The best place for this would be at the end of onCreate, in every Activity:
First, select the highest level of the architecture you want. If your layout's root view has an id, that would be the best thing:
$.with(this, R.id.root_id)
If not, either add one, or select the topmost view (above your layout). Note that for analytics this may provide some results you do not need:
$.with(this).selectAll()
Now, on the end of this selection, append the following:
.each(new Function() {
#Override
public void invoke($ d, Object... args) {
d.view(0).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
//do something with the clicked view, such as:
handleTouch(v, e);
return false;//don't absorb the touch.
}
})
}
});
Then have a new method:
private void handleTouch(View v, MotionEvent e) {
//handle the view touch.
}

Android: Is there any way to detect any user input?

on Android! I need to get user input (touch event, keyboard event). is there any way? In java code, It seems there is no way. What about native code?
In java code, It seems there is no way.
=> sorry, there is a way to do detect any action user made and play with application.
Some examples:
KeyboardView.OnKeyboardActionListener
Responding to Touch Events
for touch you can use
mView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//show dialog here
return false;
}
});
if your YourActivity implements OnTouchListener you can get the event and where the user touched on the screen:
public class YourActivity extends Activity implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("I touched: "+event.getX()+"-"+event.getY());
}
}
Actually I don't know what you exactly mean. Do you mean text input on an EditText or in General when something is touched?
If you mean text you can use TextWatcher
http://developer.android.com/reference/android/text/TextWatcher.html
If you mean on a View directly you can use OnTouchListener as mentioned above.
https://developer.android.com/reference/android/app/Activity.html#onUserInteraction()
Activity
public void onUserInteraction ()
Added in API level 3 Called whenever a key, touch, or trackball event
is dispatched to the activity. Implement this method if you wish to
know that the user has interacted with the device in some way while
your activity is running. This callback and onUserLeaveHint() are
intended to help activities manage status bar notifications
intelligently; specifically, for helping activities determine the
proper time to cancel a notfication.
All calls to your activity's onUserLeaveHint() callback will be
accompanied by calls to onUserInteraction(). This ensures that your
activity will be told of relevant user activity such as pulling down
the notification pane and touching an item there.
Note that this callback will be invoked for the touch down action that
begins a touch gesture, but may not be invoked for the touch-moved and
touch-up actions that follow.

Change Spinner content on opening

I've a Spinner and its content depends on actual location (GPS position). So the content should changes continually, but it's only visible to the user when he/she selects an item. Instead of having a thread who continually updates the Spinner content, or a button to force an update from the user, I'd like to obtain another behaviour.
When the user touches the Spinner, before the Spinner opens, it should be updated. I'm already able to change programmatically the Spinner's content. What I need is an event that triggers when the user touch the closed Spinner, but before the opened Spinner is shown.
I hope this question is clear enough. Thank you for you attention.
You can use onTouchListener
spinner.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Load your spinner here
}
return false;
}
});

Categories

Resources