Load url by pressing enter key (android) - java

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.

Related

Android - How to hide softkeyboard if EditText is empty on key press?

I'm working on a project to make calculations. So, I have my EditText box working, and I want to hide the softkeyboard if the user clicks the DONE (or any other kind of button, GO, NEXT, etc.) and the EditText box is empty
Like so:
EditText is empty -> user clicks button -> softkeyboard hides
I have this piece of code that I manage to write using tutorials and guides over the internet
I do know it is to manage the listener of the button in the softkeyboard
TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
//Calculations method
}
return false;
}
};
So, my question is: How can I manage the listener when the EditText is empty and not get errors?
you can use, for example:
TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
if (!TextUtils.isEmpty(v.getText().toString())){
// you calculations method
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return false;
}
};

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?

On enter,jump to next EditText issue

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();

Pressing the green key in blackberry

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);
}

Categories

Resources