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?
Related
I want to perform an action when enter is pressed in android editText. I have tried various ways of using EditorInfo.IME_ACTION_DONE, which works, but the problem is, it shows a tick icon, which I don't want. I want it to look like the default enter button on the keyboard.
How do I handle enter pressed event while retaining the "Enter" shape? Is there something like EditorInfo.IME_ACTION_ENTER to get my job done?
My approach (which shows tick icon):
editorEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if( (actionId == EditorInfo.IME_ACTION_DONE){
Toast.makeText(EditorActivity.this, "working", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
I want it to look like "Enter" shape because I am making a text editor. I am trying to shift to another EditText on the "Enter" button is pressed and don't want it to look like a "Tick" button. That's why I don't want the tick symbol.
I think just changing the inputType of your EditText in your layout should do the trick to have an "Enter" like button in your soft keyboard in Android.
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine" />
Please note that I have used textMultiLine as the inputType of the EditText.
You can take some action on pressing the "Enter" button in your EditText like the following.
final EditText edittext = (EditText) findViewById(R.id.edit);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (before == 0 && count == 1 && s.charAt(start) == '\n') {
String text = edittext.getText().replace(start, start + 1, "").toString(); // Removes the enter
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Hope that completes the answer.
im making an android browser, but when i hit enter, it is adding a new line to edittext box, instead of loading the url. Please help me.The source is here
You could use a Key event. Set setOnKeyListener on the edittext and do something like this:
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)
//Load url from EditText
Try implementing an OnEditorActionListener interface. For example:
// Reference the edit text view.
EditText theEditText = (EditText) findViewById(R.id.theEditText);
// Set editor action listener.
theEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL) { // Enter key was pressed.
// Open link from url.
}
// Return true to prevent the edit text from adding a newline from the enter press.
return false; // Allow the event to propagate, Return true to consume the event.
}
});
The actionId will be equal to EditorInfo.IME_NULL if the enter key was pressed.
Hope this helps.
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.
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();
I want to know when the user presses the green button in the device to initiate a phonecall from the app directly. What event is fired when the green key is pressed?
Thanks
You need to implement KeyListener
import net.rim.device.api.system.KeyListener;
override the function
public boolean keyDown(int keycode, int time)
and inside it catch the event of pressing buttons.
How do you check which button was pressed?
if (Keypad.KEY_SEND == Keypad.key(keycode)) {//your code}
find the API DOC here : http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/Keypad.html
I found the answer to be something similar to this.
Override keyDown:
public boolean keyDown(int keycode, int time)
{
if (keycode == Keypad.SEND)
{
//handle your event
return true;
}
return super.keyDown(keycode, time);
}