Text is messed up with ImageSpan in EditText - android

I am building a simple chat app where the user has the ability to send text and emoticons. I can send both text and emoticons to another phone. My problems are:
1.When I type something and add an emoticon:
Then I cannot type any text right before and right after the image. I can write before the "o" letter. The system "sees" that I type, so even if I type "Honey" after the smiley, I cannot see it, but the EditText registers it and the message is sent:
2.When I add just an emoticon to the Edittext then I delete it, I cannot type anything because the deleted emoticon appears. It appears only once, so no matter how many characters I type, the EditText looks like just before I deleted the emoticon, BUT the text is sent without the emoticon, just like in all three cases.
3.When I type "something" in the EditText then insert an emoticon after "some":
Then I put the cursor after the emoticon and delete it, here what's left:
But the correct message is sent when I press the Send button:
That's what's inside the button listener of the emoticon (this method is activated when I click the emoticon to add it to the EditText).
ib_happy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int cursorPosition = mOutEditText.getSelectionStart();
mOutEditText.getText().insert(cursorPosition, smileys[0]);
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText());
ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mOutEditText.setText(ssb, BufferType.SPANNABLE);
mOutEditText.setSelection(cursorPosition+2);
dialog_emoticon.dismiss();
}
});

I found the solution. All I had to do was to change Spannable.SPAN_INCLUSIVE_INCLUSIVE to Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

I would add a textwatcher to that edittext and watch as the user types, that way I can reposition the images/set the text/make corrections/validate input/etc.
editText.addTextChangedListener(textWatcher);
textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
//editText.doStuffHere
//reposition your image/etc.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) { }
};

Related

How to save input from TextEdit field into a string?

I'm making a simple app, where there is one TextInput field and a button next to it.
The TextInput field loads #string/duration_time from strings.xml.
The button next to it will display a simple message ("hello" + the string duration_time). However, it will always display the original information from string duration_time.
How do I set up a TextChangedListener to update the string/duration_time to reflect the input from the user? How do I access the information from TextInput and use it? Because as in the example below, under "public void afterTextChanged" I am trying to save the information, but I don't know where it's stored.
Kind regards
button2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String duration_input= WHAT_Do_I_Put_Here;
}
});
Suppose you are using editText and button as Android Wigets.
Now if you want to get the the text inside editText on click of button.It can be done like this
String textEdit = "";
button.setOnClickListener(->view{
textEdit = editText.getText().toString();
});
Now your textEdit will contain the text which you entered in your editText.

How to change the color of a particular string in edit text on the fly??-Android

In my app, I want the users to give their answers in the form of text through edit text. So for the correct answer I want the letters to turn green (or red for incorrect) on the fly while typing.
For example, if the answer is DOG, I want the the text to turn green if the user types DOG dynamically. Even if the the first letter he types is D then I want the text color to be green. Only when the user's input text is not correct do I want it to be red. The text color should change on the fly while typing.
Create EditText and call addTextChangedListener for it supplying custom TextWatcher where you mostly need to override its onTextChanged.
In this method change your text color according to your logic.
Snapshot :
mEditBox = (EditText) findViewById(R.id.my_edit_box_id);
mEditBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String currentText = mEditBox.getText().toString();
// highligt correct answer in green
if ("DOG".startsWith(currentText)) { // user starts typing "DOG"
mEditBox.setTextColor(Color.GREEN);
} else {
mEditBox.setTextColor(Color.RED); // incorrect input
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Associate EditText with a TextView for labeling purposes

Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);

How to search a character from a string android

I have 3 widgets on my screen, Text View, Edit Text and a Button. What ever I insert in my edit text, when I click the button, the text view gets the string from the edit text. Now, what I want to do is that, if I have already inserted the character "\" or "," or what ever character I want, it will not be inputted anymore. It's like, you can only put that character once in the edit text. Do you guys have any idea about it?
Well what I am thinking is that, I have to search from the edit view then validate it. But I don't know what code to use. Could somebody please help me? Thank you!
Use Android TextWatcher on EditText.
There are delegates which returns the charsequence that is entered
onTextChanged
afterTextChanged
beforeTextChanged
Fill the entered character in set everytime. If size is not incrementing that means a duplicate.
then avoid adding of that character in edittext
I think you need to add a TextChangedListener to your editText.
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

How to catch key pressed with the virtual keyboard and disable it for EditText in Android?

I need to control pressed buttons, before they goes to my EditText widget. It's should work like filter.
For example: I need that user could fill EditText only with digits 1,2,3,4,5 other symbols must be ignored. So the part of buttons on virtual keyboard should be disabled or I need to catch last pressed symbol, analyze it and disable for EditText.
Who knows the way how to solve this problem?
Thanks..
statusEdt.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
charTxt.setText(statusEdt.getText().length() + "/140");
}
});
I used this TextChangedListener(TextWatcher) to keep a count of how many characters had been typed into an EditText for a Twitter client I made. You could probably use a listener like this. You'll want to override beforeTextChanged or onTextChanged. These methods will pass you whatever CharSequence has been typed. You can check what was typed in and if it is not valid input you can remove it by calling setText() and passing in whatever has been typed so far minus the invalid characters.
What you probably need is an InputFilter.
For example to allow only digits, sign and decimal point:
editText.setFilters(DigistKeyListener.getInstance(true, true));

Categories

Resources