Implementing Search Button on my Keyboard - java

I'm trying to implement a search button in place of the regular enter button on my inbuilt android keyboard. I tried doing:
resultView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
System.out.println("You searched for this!");
return true;
}
return false;
}
But the regular 'enter' button is still appearing. I do not want to use XML and i'm creating my UI completely on JAVA. What should i do? Help would be appreciated. Thanks!

EditText view = new EditText(this);
view.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
view.setSingleLine(true);
And you are good to go, you set SingleLine attribute to change the new line button "default behavior in multi-line editext" to search button .

Sounds like you need to set the imeOptions programmatically. Try something like this:
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
You might have to play around with the available options to get exactly what you want.

Related

Remap physical back button for "select_button" in android tv webvew application with java

I'm a french developer , i'm creating an android tv application about cloud Gaming in a webview with java.
My application start a gaming stream direcly in the webview, on fullscreen, the physical buttons of controlers are working with the game, eccept the "view button" the "back button'.
This "view button" or if your prefer "select button" is for android tv a back button to the homescreen. So i have to overiding this back button, and i want replace it by a "select button" that it can interact with the games for displaying maps and inventory like in rpg games.
I know that the name will be "button_select" for interact with the pc game. So in android tv for now i will always redirected to the home page.
this is a sample of my code.
public class MainActivity extends AppCompatActivity {
#Override
public void onBackPressed() {
return;
}
With this overide, the back button is completly disabled for now, i want replace it or call the "button_select". I readed something about "handler" perhaps this is the solution.
Edit 16/06/2021
I tested much coded, but nothing work.
this one
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return super.onKeyDown(keyCode, event);
}
And its variants
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (action == KeyEvent.ACTION_DOWN ){
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
In game, the back button is disabled but, the" keycode_button_select "not interact with the game. There is nothing. I test with the "keycode button_start" the same things.
I tested the app button mapper for android tv, there is an option for custom keycodes with adb, and nothing is working in game.
https://play.google.com/store/apps/details?id=flar2.homebutton&hl=fr&gl=US
So by the code or by an app, nothing is working for now, i don't want to forcing the "root mode" for this things. Perhaps i'll just have to implementing a gamepad Plugin in java. I don't know...
Edit 18/06/21
I can capture the "back button" and display a dialog alert that appear on middle of the screen. But the actions after has no effect for now. I tested "dispatchEvent" with no success for now. I will testing the functions "robots". Perhaps "robot" and "dispatchEvent" will working together.
Thank you for your help.
I am assuming that you want to call button_select key when user presses back button.
In this case, create a method to call upon pressing back button. And call that method from the onBackPressed method.
For example:
public void callButtonSelectKey() {
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
#Override
public void onBackPressed() {
methodToCallUponBackPress();
return;
}
Now when the user presses the back button, it will call callButtonSelectKey method invoke "button_select" key. (i.e. remapped backbutton to button_select)
Let me know if you have any more questions.
EDIT
In an Object Oriented Language, overriding the class's (in this case KeyEvent) field (in this case, keycode) will do nothing. Like the code below:
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
To call button_select when you receive the event of KEYCODE_BACK, you should manually invoke the method to call Key Event, in this case the method I gave you earlier dispatchKeyEvent .
So you should make your change into something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
return super.onKeyDown(keyCode, event); //Edit this return statement if you want to ignore a keypress.
}

How to Completely disable keyboard when using EditText in android?

I have 5 EditTexts and I type in them using 5 buttons, so I don't need the keyboard.
How can I disable it completely, even when I click on the EditText? On EditText click I want only to focus it so that it has the cursor on. (I implemented everything so that when I click one of the 5 buttons, the focus goes on the next EditText).
The problem is that when I click on an EditText the keyboard pops up. I think I have to do it in java. Thank you!!!
MyEditor.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch event
}
});
Try :
editText.setShowSoftInputOnFocus(false);
editText.setInputType(InputType.TYPE_NULL);
editText.setFocusable(false);

Use "OK" key on softkeyboard (numberPassword keyboard) instead of clicking button?

I have an EditText which InputType is numberPassword, and a Button"ANYNAME". I'd like to use "OK" key on softkeyboard (numeric keyboard) instead of the Button"ANYNAME" to execute a giving function in my code.
How can I realize that?
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// call some function
return false;
}
});
if you want to change "Done" button title, use:
mEditText.setImeActionLabel("OK", KeyEvent.KEYCODE_ENTER);
Give some proper explanation...!!!
use Toast Message to know Action is performed or Not.

TextView editable onLongClick -- But one small issue when BACK button pressed

I have been search SO for days and have finally compiled enough answers to accomplish what I wanted. First off, it seems to be an often asked question but not really answered (at least not the way I was looking for it). I thought I would share my findings but I also have one small issue left that I would like to ask for help with. Here goes:
I have a TextView which displays a score. It starts at 0 and at an onClick event the score increments and updates the TextView (score is tracked as a byte - valScore).
onLongClick: This was the challenge. I want the user to be able to do a LongClick to correct/change the score. I first found a solution that utilized another layout.xml file with just an EditText element and the OK and CANCEL buttons. This was very cumbersome to change the score as it involved the LongClick, then the dialog opens, then you had to click on the EditText element to open the keyboard, then you enter the value, click DONE and then click OK. I shortened it by figuring out how to open the software keyboard automatically when the dialog opened. However, you still had to click DONE and then OK. I didn't like this action so I continued searching.
Days later I came up with a bit of code and then more and with a lot of playing/hacking around I came up with the following solution:
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
tvScoreHome.setInputType( InputType.TYPE_CLASS_NUMBER );
tvScoreHome.setFocusable(true);
tvScoreHome.setFocusableInTouchMode( true );
tvScoreHome.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(tvScoreHome, InputMethodManager.SHOW_FORCED);
tvScoreHome.setText("");
tvScoreHome.setOnEditorActionListener( new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
valScoreHome = Byte.valueOf( tvScoreHome.getText().toString() );
// This part will hide the keyboard after input
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
tvScoreHome.setFocusable( false );
tvScoreHome.setFocusableInTouchMode( false );
tvScoreHome.setText( Byte.toString(valScoreHome) );
return true;
}
return false;
}
});
return true;
}
});
This works EXACTLY how I want. User performs LongClick the keyboard opens, the user enters the new value and clicks DONE. The TextView is updated and it works great!
The problem arises if the user changes their mind and hits the BACK button on the device. The keyboard closes (GOOD), but then the focus remains on the TextView instead of removing the focus like I do if the DONE button is pressed. So if you cancel out of a change every click after that results in the keyboard opening again instead of just incrementing the score -- until you actually type a value into the keyboard and click DONE (then the regular behavior takes over again. I need to setFocusableInTouchMode to FALSE if the BACK button is pressed.
The other issue is that the setText() method is executed even if the BACK button is pressed if a different value has been typed in. Even though valScoreHome isn't updated the TextView changes. On the next increment it goes to the correct number again, but the setText() should not execute if the BACK button is pressed.
Can someone help me figure this out please?
Both issues can be handled by subclassing TextView.
The back button press that closes the keyboard is handled by overriding onKeyPreIme.
To avoid updating the text when the user closes the keyboard, the score value is saved in the variable mScore, but only if the TextView is currently not focusable. That means, the TextView "remembers" the current value of the score, that was not entered by the user. When the user closes the the keyboard, the text is set back to the saved value.
public class ScoreTextView extends TextView {
private CharSequence mScore;
public ScoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setText(CharSequence text, BufferType type) {
if (!isFocusable()) {
mScore = text;
}
super.setText(text, type);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setFocusable(false);
setFocusableInTouchMode(false);
setText(mScore);
}
return super.onKeyPreIme(keyCode, event);
}
}

How to send key event to an edit text

For example, send a backspace key to the edit text control to remove a character or send a char code like 112 to append a character in the edittext control programmatically.
Actually, I need a method like
void onKeyReceived(int keyCode)
{
// here I would like to append the keyCode to EditText, I know how to add a visible character, but what about some special keys, like arrow key, backspace key.
}
To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL, 0));
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_DEL, 0));
This can be used to send any other key code, not just delete.
Your question is not all that clear, but I think you want to modify/append text to a TextView when certain buttons are pressed. If so, you want a combination of some of the existing answers.
#Override
public void onCreate(Bundle savedInstanceState) {
...
(TextView) textView = (TextView) findViewById(R.id.myTextView);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
// user pressed the "BACK" key. Append "_back" to the text
textView.append("_back");
return true;
case KeyEvent.KEYCODE_DEL:
// user pressed the "BACKSPACE" key. Append "_del" to the text
textView.append("_del");
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
Whether to return true for each case you have handled (as above) or to always return super.onKeyDown(keyCode, event); after your switch statement will depend on your exact requirements. Check the documentation for the behaviour of onKeyDown
If, instead of appending text in each case you want to delete a character, or move the cursor, you could do that in each case statement. Have a look at the TextView documentation for the different methods you can call. Also look at the KeyEvent documentation for a list of the keys you can check for.
I think you need use addTextChangedListener to EditText.
Refer the answer of EditText input with pattern android and Live editing of users input
virsir , I suppose you are looking for dispatching hard keys programmatically.
For that you may try dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target) with an example at Back and other hard keys: three stories
Hope that helps.
Check for key events in your activity. for example, this code listens for back keypress:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.
String curText = mEditText.getText();
if(!curText.equals("")){
mEditText.setText(curText.subString(0, curText.length - 1));
}
to simulate backspace key, just ad code
editText.setText(editText.getText().substring(0,editText.getText().length()-1))
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
to simulate adding a character, put the code
editText.setText(editText.getText() + (char) charCode)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Take a look at this article: creating-input-method.html. Basically, you can either manually send KeyEvents or you can manually edit and commit text around the cursor in the application's Input View.These are all done via your IME's InputConnection.
Hope this helps,
if you want a click listener, the best way to do it is this:
View textfield = findViewById(R.id.textfield);
textfield .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*your code for the click event here*/ }});
if you want a backspace button, do this:
public void backSpace() {
EditText textfield = (EditText)findViewById(R.id.textfield);
try {
textfield.getText().delete(textfield.getSelectionEnd() - 1, textfield.getSelectionStart());
} catch (Exception e) {
try {
textfield.getText().delete(textfield.length() - 1, textfield.length());
} catch (Exception myException) {
//textfield.getText().delete(textfield.length(), textfield.length() - 1);
}
}
}
if you want to append a character in the EditText, do this:
EditText textfield = (EditText)findViewById(R.id.textfield);
textfield.setText(textfield.getText().concat("112"));
try implementing TextWatcher interface.
it has 3 methods which you need to override.
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged","here");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.v("beforeTextChanged","here");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
I think this will work.

Categories