I want to add a search button on the keyboard input and the EditText and searches I proceeded as follows and I have them the following errors
recherche=(EditText)findViewById(R.id.recherche);
recherche.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
new DownloadTask().execute();
return true;
}
return false;
}
});
ERROR : The type new TextView.OnEditorActionListener(){} must implement the inherited abstract method
TextView.OnEditorActionListener.onEditorAction(TextView, int, KeyEvent)
ERROR : KeyEvent cannot be resolved to a type
Here is a sample of my code:
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (i == EditorInfo.IME_ACTION_DONE)) {
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
bQServer.performClick();
}
return false;
}
};
//etUserName.setOnEditorActionListener(enterListener);
etPassword.setOnEditorActionListener(enterListener);
You should be able to just change KeyEvent.KEYCODE_ENTER for KeyEvent.KEYCODE_SEARCH
i.e
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
new DownloadTask().execute();
return true;
}
return false;
}
};
recherche.setOnEditorActionListener(enterListener);
--Edit--
Make sure you have imported KeyEvent and onEditorActionListener
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
Related
I'm make a EditText and the value i got from Firebase, it is possible if edittext is not null then enter key pressed automatically
private void init(){
Log.d(TAG, "init: initializing");
if (mSearchText != null){
what must i write here?
}
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){
//execute our method for searching
geoLocate();
}
return false;
}
});
[1] you can try below code
String yourtext=mSearchText.getText().toString().trim();
if(!TextUtils.isEmpty(yourtext)) //or else you can use if(yourtext!=null && !yourtext.equalsIgnoreCase(""))
{
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){
//execute your method for searching
geoLocate();
}
return false;
}
});
}
[2] else you can achieve your functionality using this(Direct method calling)
String yourtext=mSearchText.getText().toString().trim();
if(!TextUtils.isEmpty(yourtext)) //or else you can use if(yourtext!=null && !yourtext.equalsIgnoreCase(""))
{
//execute your method for searching
geoLocate();
}
you can try this
EditText editText;
BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POUND));
read more
I am trying to disable left and right on the keyboard with this code but viewPager on the press of left and right button change the state of the pager.
edComment.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS
|| actionId == EditorInfo.IME_FLAG_NAVIGATE_NEXT
|| actionId == EditorInfo.IME_ACTION_PREVIOUS
|| actionId == EditorInfo.IME_ACTION_NEXT
) {
return false;
}
// Return true if you have consumed the action, else false.
return false;
}
});
You can extend the ViewPager and override the arrowScroll method like this:
public class Wizard extends ViewPager {
#Override
public boolean arrowScroll(int direction) {
// Never allow pressing keyboard arrows to switch between pages
return false;
}
}
The keyboard app can show whatever keys it wants. There is no way to force it not to show certain buttons, and even if there was another keyboard wouldn't follow the same rules.
According to #Gabe Sechan button are not hide or disable so I am doing like that
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i("your tag", "Keycode: " + keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.e("Click","left");
/* for (int n = 0; n < count; n++) {
setCurrentPage(arrayListView.get(n + 1));
}*/
pager.setCurrentItem(getItem(+1), true);
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e("Click","right");
pager.setCurrentItem(getItem(-1), true);
return true;
case KeyEvent.KEYCODE_DPAD_UP:
Log.e("Click","up");
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.e("Click","Down");
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
I have 4 editText, and I want to store all the values in String arrayList. I have declared my arrayList globally. I can get the correct size of my arrayList, but I can't get any String value. Here's my code:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
String player1 = editText.getText().toString();
playerName.add(player1);
}
return false;
}
});
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
editText2.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
editText3.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
First, correct the listener, you use the same editText everywhere when you get the text editText.getText().toString().
Note : You could update this to use 1 instance of listener but that a different question.
To read a List, use List.get(int) or iterate it like
for(int i = 0; i < list.size(); ++i){
String s = list.get(i);
...
}
or
for(String s : list){
...
}
Now, did you need to use a listener, or could you just get the value when you need it ? That your project but that's an idea. Because did you want to stack the value if you input a value twice in the same EditText?
playerName.get(index);
you can get the string object you need from ArrayList in this way
try this:
//declare global
ArrayList<EditText> editextList = new ArrayList<>();
ArrayList<String> editextValuesList = new ArrayList<>();
onCreate(){
//bind edittext and then
editextList.add(editText);
editextList.add(editText1);
editextList.add(editText2);
editextList.add(editText3);
//you can use below lines anywhere if any text changes in edittext then it will give you updated text
for(int i=0;i<edittextList.size();i++){
String editTextString = edittextList.get(i).getText().toString();
editextValuesList.add(editTextString);
}
}
To get a single player, please try this code:
String player1 = playerName.get(0);
To get all the players from the list, please use this code:
for (String player : playerName) {
Log.d("TAG", player);
}
Hope it helps.
I'm trying to cacth done action which comes from softkeyboard. My code is like this;
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
nextPage();
return true;
}
else {
return false;
}
}
};
}
However even though IDE evaluates "(actionId == EditorInfo.IME_ACTION_DONE)"as true, it's not calling nextPage(), returning false instead. If I cast expression like this, it works fine.
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((boolean)(actionId == EditorInfo.IME_ACTION_DONE)) {
processPayment();
return true;
}
else {
return false;
}
}
};
}
Any idea what can cause this?
This question already has answers here:
How do I handle ImeOptions' done button click?
(9 answers)
Closed 6 years ago.
I have an EditText that let me type in the an IP address:
In the xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/enterIP"
android:layout_weight="1"
android:onClick="enterIP"
android:inputType="textPhonetic"
android:imeOptions="actionDone"
/>
I used android:imeOptions="actionDone" so that the input box will disappear after I press Done.
In the Java:
public void enterIP(View view) {
EditText theIP = (EditText) findViewById(R.id.enterIP);
try {
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
} catch (NullPointerException e)
{
Log.d("Error", "Input address is NULL.");
}
Toast.makeText(getApplicationContext(), "New IP is " + myIP, Toast.LENGTH_LONG).show();
}
However, the problem with this is that when I pressed Done, myIP still holds the old value. Only when I touch the EditText to bring up the input again the value is updated.
So how can I make sure myIP will be updated when Done is pressed. ?
Thanks
try this
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do something
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
}
return false;
}
});
Use this method:
theIP.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
return true;
}
return false;
}
you should set onEditorActionListener for the EditText to implement the action you want to perform.
theIP.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
try {
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
} catch (NullPointerException e)
{
Log.d("Error", "Input address is NULL.");
}
return true;
}
return false;
}
});