All- I looked at other questions relating to this topic and found out that according to the android development website: "the action key performs a "done" operation, typically meaning the IME will be closed." My question is how do I edit the action of the done button to make it so it calls one of my methods instead of closing the IME?
Thanks for your time.
Best possible way
Example for handling Enter key event
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (EditText)findViewById(R.id.txt);
txt.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)
{
Log.d(TAG, "enter_key_called");
}
return false;
}
});
}
Related
So i was developing a bmi calculator activity in android studio. And i ran into a problem where i got a value of two variable inside from two methods for different TextView which was defined in the class. That shouldn't be problem but once i switch to another TextView in app. The first one sends a zero value, which is the problem, Anyone knows how to fix it? Thanks !
The two methods in onCreate which give value:
inheight.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
height = Double.parseDouble(inheight.getText().toString());
if(KeyEvent.KEYCODE_DEL== keyCode)
{
if(inheight.getText().length() != 0)
inheight.setText(inheight.getText().subSequence(0,inheight.getText().length()-1));
}
return true;
}
});
inweight.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
weight = Integer.parseInt(inweight.getText().toString());
if(KeyEvent.KEYCODE_DEL == keyCode)
{
if(inweight.getText().length() != 0)
inweight.setText(inweight.getText().subSequence(0,inweight.getText().length()-1));
}
return true;
}
});
And the Calculate method where i try to calulate
public void calculate()
{
if(height!=0 && weight !=0)
{
// do this
}
else
{
Toast.makeText(this, "Error: Values Cannot Be Zero!", Toast.LENGTH_LONG).show();
}
So when i run it always shows the toast message :(. Please Help!
As stated in the android documentation, View.OnKeyListener is useful only for hardware keyboards , and a software input method (like the one android uses), has no obligation to trigger this listener.
So instead of using View.OnKeyListener to get your values i would try a different approach (i.e getting the values after pressing a button, implementing View.OnClickListener )
Also are you sure that only the first TextView gives you the zero value, or both ?
For Nvidia joystick trying to associate this event to go to search button on the grid view, it is working but it is getting frozen and nothing is happening after that. Suggest me something
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i(TAG, " :: keyCode ::"+keyCode);
if (keyCode == KeyEvent.KEYCODE_BUTTON_Y) {
searchTop.performClick();
return true;
}
return super.onKeyUp(keyCode,event);
}
Try to call the super.onKeyUp(keyCode,event) anyway, even after your event handling.
I have been getting into the android app world as of late. I understand that you want to place what variables you can into the onCreate method so that you can start using your variables right away. However, I am at a loss for a part of my code where I need to define onEditActionListener's to listen for user input but then also once enter is pressed get their text and send it to another activity. My research has shown I want to define these using the EditText and onEditActionListeners. however, I am at a loss as to how to go about this. Do I need to create a class that extends onEditActionListener?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horde_calculator);
EditText myText = (EditText) findViewById(R.id.numToSend);
OnEditorActionListener onEditorActionListener = new myClass();
myText.setOnEditorActionListener(onEditorActionListener);
}
that is a copy of my current code and I am unsure if this is the correct way to go about doing it. If I was to create a new onEditActionListener wouldn't that make my onCreate more complex than having a few lines of variables? I feel that there might be a more clear way of going about this. However, after looking through stack overflow I couldnt' seem to find something that made sense. I didn't want to just copy and paste code, but really understand how it all flows together.
Just another idea of how you can achieve this:
public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
editText = (EditText) findViewById(R.id.numToSend);
editText.setOnEditorActionListener(this);
}
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_DOWN) {
//both attempt are the same
Log.d("onEditorAction", view.getText().toString());
Log.d("onEditorAction", editText.getText().toString());
}
return true;
}
}
Use TextView.OnEditorActionListener :
TextView.OnEditorActionListener listener = new TextView.OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_DOWN) {
//call your new activity here
}
return true;
}
};
myText.setOnEditorActionListener(listener);
I am new to Android. I am trying to make a text box and on pressing done key, it should take the value to the java code. For this I am using setOnEditorActionListener.. I searched on how to do this and got many answers on how to implement it. Example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
I need to ask where should I write this thing? In which method? I tried doing it in onCreate but it threw some error. I somehow made it work using this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock);
Log.i(TAG, "onCreate");
editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(this);
}
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed");
Toast.makeText(this, "Hey you just clicked the DONE button", Toast.LENGTH_SHORT).show();
handled = true;
}
return handled;
}
Here I used this keyword, and I don't understand why have I used it.
Question 1. Please help me understand, why have we used this keyword..
Question 2. Why wasn't it working in the below code?
public void checkInput() {
Log.i(TAG, "Enter checkInput method");
final EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(TAG, "Enter onEditorAction");
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed")
handled = true;
}
return handled;
}
});
}
I called this checkInput method from onCreate.
To answer Question 1:
Here I used this keyword, and I don't understand why have I used it. Question 1. Please help me understand, why have we used this keyword..
You're telling Java to look into the Activity class for implementations of methods required by the TextView.OnEditorActionListener interface. So for all interactions with your soft keyboard, Java would look into your class for the method: onEditorAction
In order for the above to work, your activity needs to defined like:
public class MyActivity implements TextView.OnEditorActionListener {}
For question 2:
Question 2. Why wasn't it working in the below code?
To check for the "Done" action, your if statement should be:
if (actionId == EditorInfo.IME_ACTION_DONE) { ... }
Hope that helps.
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.