Issue
I want to create a button like we have in WhatsApp for using microphone to record speech.
Basically, if user starts to type something then that microphone button converts into send text button .However, if user deletes the text then that button again changes to microphone button. Can anyone please tell me how to create a button like that?
Solution
Use a TextWatcher
to detect when the EditText
's content has been changed and perform your desired action.
Example:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//called after the EditText's text is changed
if (editable.length() > 0) {
//change to send message icon
} else {
//change to microphone icon
}
}
});
For your button's onClickListener
, just perform a check whether the EditText
is empty or not.
Example:
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = yourEditText.getText().toString();
if (text.isEmpty()) {
//perform your microphone action
} else {
//perform your send message action
}
}
});
Answered By - Lucas Cabrales
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.