Coding with Android Studio.
I am trying to make the emVideoView work with onKeyDown (or other keys) for keyboard or remote control on a user click to load java activity (Play list).
Here is what I tried. Instead of loading a list it is returning or exiting completely rather than loading an Activity example (VideoSelectionActivity) is my page that have all the play list.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (emVideoView != null && (emVideoView.isPlaying() || emVideoView.getFocusedChild() != null ) &&
(keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_DPAD_DOWN)) {
return emVideoView.onKeyDown(keyCode, event);
} else {
return super.onKeyDown(keyCode, event);
}
}
#Override
public void KeyEvent(VideoSelectionActivity arg0) {
if (emVideoView.isPlaying()==true) {emVideoView.showDefaultControls();}
}
I have changed this many times and the onClick events works but incorrectly also not loading activity.
Related
I am trying to add shortcuts in my app when external keyboard is connected. SHIFT+KEY, this combination is triggering the following event. But Control+KEY and Control key events are not triggered. Can anyone suggest me on this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT) {
flag_sellreceipt = KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT);
int i = KeyEvent.META_CTRL_LEFT_ON;
System.out.println("ctrl is pressed");
}
return
super.onKeyDown(keyCode, event);
}
Note: I have tested this in emulator.
I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.
I have a WebViewClient in an Activity in which I override the onKeyDown method like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
finish();
return true;
}
}
This works like a charm on my phone as well as the emulators I tested on including a 3.0 emulator.
Weird thing is that on a 3.1 emulator as well as on my Xoom tablet (3.0.1) it does NOT work. It seems that webView.canGoBack() always returns true on these platforms.
Questions:
Has anybody else found similar behaviour?
Do you have a workaround/hack that allows me to make the backbutton work to navigate in the web view history as well as ultimately out of the activity if required?
Update: I have since then change the app to use fragments with the compatbility library so I am now using this:
webView.setOnKeyListener(
new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
redirectHelper.finish();
return true;
}
}
}
);
where redirect helper basically is a wrapper for proper finishing of an activity or removing a fragment from the stack. Still has the same issue though..
I'm using this without issues on 3.1 and Galaxy Tab 10.1. Haven't tried onKeyDown method.
#Override
public void onBackPressed() {
if( webView.canGoBack() ) {
webView.goBack();
} else {
super.onBackPressed();
}
}
If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?
The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.
Is this possible?
edit: I think I found it, just the old way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{ // do something on back.
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I've attached an OnKeyListener to an EditText. I've overrode the onKey handler to capture a KeyEvent.
When a user hits the enter key (either their computer enter key while using the emulator, or the hardware enter key on their phone with a hardware keyboard), the onKey handler gets executed twice. Both executions have the keyCode 66.
Why is this happening?
I want my screen so when the user hits the enter key, a search is performed. Because of what is happening, the search is needlessly happening twice.
My method looks like this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
return false;
}
});
Ahhhh
I think this is happening for key up and key down?
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction()==0) {
Try this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
}
return false;
}
});
you can filter like this :
object.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
// do stuff
return true;
}
return false;
}
});
idem when you push the key with KeyEvent.ACTION_DOWN
I had the same issue and the answers above helped me but I am using Xamarin.Android (c#) so it is slightly different syntax.. Here is what worked for me:
MyStringTextBox.KeyPressed += OnEnterKeyPressed;
protected void OnEnterKeyPressed(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Up)
{
DoSomething(this, EventArgs.Empty);
}
else
{
e.Handled = false;
}
}
This way, the DoSomething() would only be called on hitting Enter Key (Up) only and thus would be fired once. Works and tested on Xamarin.Android
This event is fired by KeyEvent.ACTION_DOWN and KeyEvent.ACTION_UP.
I have done debugging and finally I realize that there is an param called KeyEvent event that I never use, then I checked and found the problem.
I debugged and what worked for me was that,
editText.setOnKeyListener(View.OnKeyListener { view, i, keyEvent ->
if (i == KeyEvent.KEYCODE_ENTER && enterAsSend && (keyEvent.action == KeyEvent.ACTION_UP || keyEvent.action == KeyEvent.ACTION_DOWN)) {
//Do stuff
}
return#OnKeyListener true
}
false
})
and checkout your Editext that android:inputType="textNoSuggestions" because the first click of enter key gives us the suggestion from the dictionary.