Android. onEditorAction never called - java

I'm trying to catch the event of removing keyboard from the screen and i'm using OnEditorActionListener class. However, its onEditorAction method never gets called.
This is my EditText in XML:
<EditText
android:id="#+id/editTextSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:layout_weight="0.05"
android:background="#color/white"
android:ems="10">
</EditText>
This is how i work with it in java file:
private void createTextEdit()
{
EditText searchTextField = (EditText)findViewById(R.id.editTextSearch);
searchTextField.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
System.out.println("AFTER TEXT CHANGED");
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
System.out.println("BEFORE TEXT CHANGED " + s);
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
System.out.println(s);
}
});
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);//NEVER CALLED
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
});
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchTextField, InputMethodManager.SHOW_IMPLICIT);
}
No matter what i do with searchTextField whether i start editing or finish it, the needed method is never fired. What am i doing wrong?

Same problem here on a Nexus 7.
I thought it was the imeOptions, but no.
It works in my tablet if i set android:inputType="text".
I assume that it works with other inputType.
I think it's a bug on Nexus.

Related

autoLink="web" is not working when using setOnTouchListerner on TextView

I have a TextView where i want the links to be clickable earlier it worked fine with autoLink = "web", but ever since I've implemented setOnTouchListener on it the links are not clickable, They still get highlighted though.
Here is my xml
<TextView
android:layout_below="#id/currentime"
android:id="#+id/message_text"
android:layout_toRightOf="#id/add_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="2dp"
android:layout_centerVertical="true"
android:autoLink="web"
android:text="hello"
android:textColor="#color/darkTextColor"
android:fontFamily="sans-serif"
android:textSize="15sp"
/>
And here is the setOnTouchListener
holder.usermessage.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("TEST", "onDoubleTap");
ChatMessage chatMessage = messages.get(holder.getAdapterPosition());
String usermessage = chatMessage.getMessageText();
String username = chatMessage.getMessageUser();
String replytext;
if (usermessage.contains("\n\nReply:\n")) {
replytext = usermessage.substring(usermessage.lastIndexOf("Reply:\n") + 6);
} else {
replytext = usermessage;
}
if (replytext.length() >= 30) {
cutstring = replytext.substring(0, 40) + "...";
} else {
cutstring = replytext;
}
String finalreply = username + ": " + cutstring + "\n\nReply:\n";
((ChatRoom) context).setReplyText(finalreply, username);
return super.onDoubleTap(e);
}
// implement here other callback methods like onFling, onScroll as necessary
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
Log.d("TEST", "longPressed");
ChatMessage chatMessage = messages.get(holder.getAdapterPosition());
String usermessage = chatMessage.getMessageText();
copyMessage(usermessage);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
gestureDetector.onTouchEvent(event);
return true;
}
});
What I have noticed is the links start working as soon as I change the return statement in onTouch() to false but the other methods start misbehaving they all respond to single tap and trigger onLongPressed()
Please help me if anyone has any idea how to make all of this work together.
I don't know the exact reason but onTouch method was interfering with links being clickable. I fixed this issue by Overriding onDown method and returning true in it. These are the changes made:
Changed onTouch to
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
Added onDown
#Override
public boolean onDown(MotionEvent e) {
return true;
}
Hope it helps someone else.

Android disable text watcher when there are letters

I need help on my code.
I have an editText where the user enters the opening and closing time of the store and there is a checkbox with "Close".
The user enters the opening time 09:00 and then with the Text Watcher I add a space and a dash and the user enters the closing time 19:00, so that at the end the time is 09:00 - 19:00.
For example, if the store is closed on Saturday, the user must click on the checkbox and when the checkbox is clicked, the editText has the text set and it says "Closed".
The problem is that when I use the Text Watcher, if the length is equal to 6, it adds the hyphen and then when the user clicks on the checkbox instead of writing "Closed" it says "Closed-".
How can I remove that dash?
There is a condition where the Text Watcher turns off when there are only letters in the editText.
Does anyone have a solution to my problem, do you have any advice for me?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert__orari);
lunedi_inizio_uno = (EditText)findViewById(R.id.editText_uno_lunedi_inizio);
lunediUno = (CheckBox)findViewById(R.id.checkBox_uno_lunedi);
lunedi_inizio_uno.addTextChangedListener(new TextWatcher() {
int keyDell;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lunedi_inizio_uno.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDell = 1;
int prevL = 0;
return false;
}
});
if (keyDell == 0) {
int len = lunedi_inizio_uno.getText().length();
if(len == 5) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 6) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + "-");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 7) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}
} else {
keyDell = 0;
}
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
lunediUno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
lunedi_inizio_uno.setText("Chiuso");
}
}
});
}
I solved the problem, it was enough to add "-" in the length equal to 5 and eliminate the conditions of length equal to 6 and 7.

Android, EditText not update the variable after pressing Done [duplicate]

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

How do I disable 2 buttons if a number field is empty

I have 2 buttons and 1 number field, if I press a button without something in the field, it crashes, so what I want to do is disable the buttons unless the number field has something in it, I have looked around for an answer but either they aren't relevant, or I'm not sure how it would fit into my code, here are the two onClick functions for each button. Thanks
public void toPounds(View view){
EditText amount = (EditText)findViewById(R.id.amount);
Double omrAmount = Double.parseDouble(amount.getText().toString());
Double gbrAmount = omrAmount * 1.79;
Toast.makeText(getApplicationContext(), "£" + gbrAmount.toString(), Toast.LENGTH_LONG).show();
}
public void toRiyals(View view){
EditText amount = (EditText)findViewById(R.id.amount);
Double gbrAmount = Double.parseDouble(amount.getText().toString());
Double omrAmount = gbrAmount / 1.79;
Toast.makeText(getApplicationContext(), omrAmount.toString() + " Riyals", Toast.LENGTH_LONG).show();
}
yourField.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() == 0)
button1.setEnabled(false)
else
button1.setEnabled(true)
}
});
link
If you want to disable buttons if edit text is empty then you can do the following :
EditText amount = (EditText)findViewById(R.id.amount);
Button button = (Button) findViewById(R.id.button1);
if(amount.getText().toString().isEmpty()){
button.setEnabled(false);
}
amount.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() == 0)
button1.setEnabled(false)
else
button1.setEnabled(true)
}
});
Not specifically an answer to your question, but generally speaking you would want to add some sort of check before you call your code which now crashes your application. It's not a good idea to to have code which crashes your app lingering around.
Maybe make a method like: isMyEditTextValid(...){..}

Change SeekBar progress based on EditText value

I am trying to change the progress of a SeekBar based on the number entered in an EditText but for some reason the EditText value goes to the max and I can't slide the thumb in the SeekBar.
What I am looking to achieve: If the value entered in the EditText anywhere between 70 and 190 (including both number) change the progress of the SeekBar to that value.
Partial Java code:
etOne = (EditText) findViewById(R.id.etSyst);
etOne.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String filtered_str = s.toString();
if (Integer.parseInt(filtered_str) >= 70 && Integer.parseInt(filtered_str) <= 190) {
sbSyst.setProgress(Integer.parseInt(filtered_str));
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
The partial XML:
<SeekBar
android:id="#+id/syst_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:progress="0"
android:max="120"
android:progressDrawable="#drawable/progress_bar"
android:secondaryProgress="0"
android:thumb="#drawable/thumb_state" />
I add 70 to each value, because SeekBar starts at 0, but I want it to start at 70.
After using the above code, this is what it looks like:
The SeekBar is at the maximum number and the EditText is at the maximum as well.
etOne = (EditText) findViewById(R.id.etSyst);
etOne.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if (i >= 70 && i <= 190) {
sbSyst.setProgress( i - 70); // This ensures 0-120 value for seekbar
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

Categories