On enter,jump to next EditText issue - java

I have 3 EditTexts in a row but on 3 different layouts. I set them up so when i click enter,it will jump to the next EditText from that row,on the next layout.The problem is that when i click enter from the first EditText on the row,it goes to the third.From the third it goes to the second and from the second to the first.It's like i press enter on the first EditText,it goes to the second and then to the third on the same enter.
How can i stop it from jumping for example from the first EditText to the second and then the third on the same press of the enter key?
Here's some of my code :
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v1, int keyCode, KeyEvent KEYCODE_ENTER) {
l2.getChildAt(localizarer).requestFocus();
return true;
}
});
et2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v2, int keyCode, KeyEvent KEYCODE_ENTER) {
l3.getChildAt(localizarer).requestFocus();
return true;
}
});
et3.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v3, int keyCode, KeyEvent KEYCODE_ENTER) {
l1.getChildAt(localizarer).requestFocus();
return true;
}
});
It's like i need some kind of break function,lol.
How can i fix this ?
Thanks and have a nice day/night !
Fixed by doing this :
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on key press
l2.getChildAt(localizarer).requestFocus();
return true;
}
return false;
}
});
But now an older issue is back,it jumps to my second row of edittexts instead of going to the first child in the layout it goes to the second..

Are your edit texts defined in your xml do you have them in the corrent order there?
if that doesn't work you can use
android:nextFocusDown
to set the order

Why don't you use:
et1.requestFocus();
Instead of
l1.getChildAt(localizarer).requestFocus();

Related

How to detect when "Go" is pressed on Android Keyboard

I'm trying to detect when "Go" is pressed on the android Keyboard (the last key on the bottom right).
My code works in the emulator and on my Nexus 5, but doesn't on some other Android devices.
What am I doing wrong?
editTextMain.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
editTextMain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i==KeyEvent.KEYCODE_ENTER)
{
Intent intent = new Intent(thisActivity, ActivityRisultati.class);
startActivity(intent);
return true;
}
}
}
Try this
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
I have some code in my app which detects when any key which sends the KEYCODE_ENTER value is pressed. It detects both the Enter and Done keys on a Motorola Droid 3 slide-out keyboard. Here's my code:
private OnKeyListener enterKeyListener = new OnKeyListener()
{
public boolean onKey(View v, int k, KeyEvent e)
{
/*
* "Enter" or "Done" key was pressed
*/
if( (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (e.getAction() == KeyEvent.ACTION_UP) )
{
//
// Do some stuff ...
//
return true;
}
return false;
}
};
Note the additional check for KeyEvent.ACTION_UP. You might need that on some devices in order to prevent the listener from firing as soon as the button is pressed, and repeatedly if the button is held down. In my case, I only wanted it to take an action if the key had been pressed and then released.
Hope this helps.

Override Delete button of soft keyboard

I am trying to override the delete button of my soft keyboard. I am able to detect the delete button being pressed but only if the EditText is empty. Is there a way of detecting if it has been pressed when there is text in my EditText? What i want to do is override the delete button to make it delete the entire contents of the EditText instead of just letter by letter. Below is the code I am using:
mEtCoupon.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// You can identify which key pressed buy checking keyCode value
// with KeyEvent.KEYCODE_
if (keyCode == KeyEvent.KEYCODE_DEL) {
// this is for backspace
Log.e("IME_TEST", "DEL KEY");
}
return false;
}
});
Is it possible to do this?

It is possible to detect when I stop pressing a key in Java?

When I'm pressing a key i want to execute an instruction and when I stop pressing the key i want to execute another instruction. (Like the buttons on the android making setOnClickListener -> instruction for example). You can do in Java with the keys?
Sorry for my bad english.
Your question is really hard to understand because you seem to be talking about keys and buttons at the same time. To detect that a button is pressed and then released you will need to override the onTouchEvent() method for the button. For keys/physical buttons on the device there are two methods to override:
public boolean onKeyDown(int keyCode, KeyEvent msg)
public boolean onKeyUp(int keyCode, KeyEvent msg)
update: a more detailed explanation
boolean bKeyIsPressed;
#Override public boolean onKeyDown(int keyCode, KeyEvent msg)
{
switch (keyCode)
{
case THE_KEY_I_WANT:
bKeyIsPressed = true;
break;
}
}
#Override public boolean onKeyUp(int keyCode, KeyEvent msg)
{
switch (keyCode)
{
case THE_KEY_I_WANT:
bKeyIsPressed = false;
break;
}
}

ToDo list example: error adding task (setOnKeyListener)

I am following the book Professional Android development by Reto Meier, and there is an example of a ToDo list to be done in order to practice.
The problem is that I do everything as the book says, but I cannot add any task when pushing the central KeyPad, as I get an exception and the program has to close.
I tried to debug in Eclipse, and apparently it cannot find the .class file (?)
Here is the code where all the bad things are happening, specially in the todoItems.add line:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
todoItems.add(0, myEditText.getText().toString());
myEditText.setText("");
aa.notifyDataSetChanged();
return true;
}
return false;
}
});
give this a shot - it uses View v that's passed in.
I assume that "todoItems" is properly instatiated
myEditText.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
EditText edittxt = (EditText)v;
todoItems.add(0, edittxt.getText().toString());
return false;
}
});

Android: How can I set a listener to the MenuButton?

I want to do a custom action when pressing on the Menu button on the phone.
Is it possible to set an onClickListener (or similar) on the button and if so, how?
onCreateOptionsMenu is only called the first time the button is pressed - I've already tried this.
Usually you shouldn't override MENU behavior as users expect menu to appear, however you can use something along these lines:
/* (non-Javadoc)
* #see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Log.d(TAG, "MENU pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
But onPrepareOptionsMenu(..) is called each time. :)
Updated for AppCompat v.22.+
As mentioned in this forum, KeyDown is not called for KEYCODE_MENU button pressed.
The solution is to override dispatchKeyEvent to this way:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
int action = event.getAction();
boolean isDown = action == KeyEvent.ACTION_DOWN;
if (keyCode == KeyEvent.KEYCODE_MENU) {
return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event);
}
return super.dispatchKeyEvent(event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
// do what you want to do here
return true;
}
return super.onKeyDown(keyCode, event);
}
It works until Google developers release a fix for this (or maybe it is not a bug and it works this way from now on).
You could probably hack something in using "OnMenuOpened" or some such, but I really wouldn't recommend it. The menu button is only supposed to be used to show menus, so there is consistency between applications.

Categories

Resources