SwiftKey keyboard and SpannableStringBuilder with end space character issue - java

I have the strange problem when using SwiftKey Keyboard if I passed a string from EditText to SpannableStringBuilder and set it back to EditText; any Space character at the end will be trimmed automatically, using default keyboard this not happen here the code:
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.addTextChangedListener(this);
}
#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) {
if (IsChanged)
return;
IsChanged = true;
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(txtEntry.getText());
txtEntry.setText(sb);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}
I m doing this as I,m adding dynamic emotions to EditText, but can't add spaces so I made a very simple code to discover the issue.

Ok i fixed the problem, first of all must add inputType="textNoSuggestions" to EditText; this will prevent firing OnTextChanged Event twice, second the processing of string should be throw Editable; not throw EditText itself
Here is the code :
public class MainActivity extends AppCompatActivity implements TextWatcher{
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtEntry.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String ss = "";
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (IsChanged)
return;
IsChanged = true;
String sInput =editable.toString().replace(" ","#space#");
SpannableStringBuilder ObjBuilder = new SpannableStringBuilder();
ObjBuilder.append(sInput);
while (true)
{
int iStartIndex = sInput.indexOf("#space#");
int iEndIndex = iStartIndex + "#space#".length();
if (iStartIndex == -1)
break;
sInput = sInput.replaceFirst("#space#","[space]");
Drawable ObjDraw = getResources().getDrawable(R.drawable.space);
ObjDraw.setBounds(0,0,10,60);
ImageSpan Image = new ImageSpan(ObjDraw);
ObjBuilder.setSpan(Image,iStartIndex,iEndIndex,0);
}
editable.clear();
editable.append( ObjBuilder);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}

Related

how can add integer from edittext to array In android?

I am using java to code a small program. this program will take data from the keyboard and check the parity of that sequence and this is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edso = findViewById(R.id.so);
btnchan = findViewById(R.id.btnchan);
btnle = findViewById(R.id.btnle);
tvkq=findViewById(R.id.tvkq);
String n=edso.getText().toString();
List<Integer> arr = new ArrayList<Integer>();
arr.add(Integer.parseInt(n));
Integer[] array = arr.toArray(new Integer[5]);
btnchan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2==0){
tvkq.setText("chăn"+array[i]);
}
}
}
});
btnle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2!=0){
tvkq.setText("lẽ"+array[i]);
}
}
}
});
}
help me
i want to use data from edittext to check and display it in textview
int num = Integer.parseInt(mEditText.getText().toString());
arr.add(num);
or
mEditText.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) {
int num = Integer.parseInt(s.toString());
arr.add(num);
}
#Override
public void afterTextChanged(Editable s) {
}
});

Edit Text Send Function in chat application

i'm creating a narrative game, for that i have a conversation screen.
The user have to choose a predifined message to send it.
The choosen message is transfered inside the Edit Text but to send it, the user have to open the swift keyboard and click on "send".
I want to create a method that automatically send the message after the user choice. (Skip the manual "send" on the keyboard)
I tried TextWatcher :
EditText userInput;
RecyclerView recyclerView;
List<ResponseMessage> responseMessageList;
MessageAdapter messageAdapter;
String[] listChoices;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapitretest);
Button btn = (Button) findViewById(R.id.btnChoice);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listChoices = new String[]{"Mdr","Lol","xd"};
AlertDialog.Builder mBuilder= new AlertDialog.Builder(ChapTest.this);
mBuilder.setTitle("Ton choix:");
mBuilder.setSingleChoiceItems(listChoices, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
userInput.setText(listChoices[i]);
userInput.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 txtbulle = userInput.getText().toString();
}
});
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
This is my first app, thank you ;)
Update: Why I use EditText : (Conversation method)
userInput = findViewById(R.id.UserInput);
recyclerView=findViewById(R.id.Conversation);
responseMessageList = new ArrayList<>();
messageAdapter=new MessageAdapter(responseMessageList,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(messageAdapter);
userInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEND){
ResponseMessage message = new ResponseMessage(userInput.getText().toString(),true);
responseMessageList.add(message);
ResponseMessage message2 = new ResponseMessage(userInput.getText().toString(),false);
responseMessageList.add(message2);
messageAdapter.notifyDataSetChanged();
if (!isLastVisible())
recyclerView.smoothScrollToPosition(messageAdapter.getItemCount() - 1);
}
return false;
}
});
}

Disable button upon removing textview content

public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}

Check if EditText is empty in real time

In one activity of my app I have 5 EditText fields. However, I want etPlayer5 only to be enabled when the other 4 EditTexts are not empty. I wrote this little code to achieve this:
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer3 = (EditText) findViewById(R.id.etPlayer3);
etPlayer4 = (EditText) findViewById(R.id.etPlayer4);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
if (namePlayer1.matches("")||namePlayer2.matches("")||namePlayer3.matches("")||namePlayer4.matches("")) {
etPlayer5.setEnabled(false);
etPlayer5.setFocusable(false);
}
else {
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
Of course this code doesn't work in real time. What do I need to do that when the last of the EditTexts 1 to 4 is filled in, etPlayer5 is automatically set enabled without using a button or something?
Thank you!
You can have textChangeListener with all 4 Edittexts and keep boolean against all edittext. Update booleans to true when you get call of onTextChange and see if the string length is one in this edittext and further check remaining booleans are also true.This is the trick. Now this is the time to make the edittext enable.Hope that helps.Below is the code for above explanation
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3, et4, et5;
boolean firsEditText, secondEditText, thirdEditText, fourthEditText, fifthEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
et1.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) {
if (s.toString().length() > 0) {
firsEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
firsEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.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) {
if (s.toString().length() > 0) {
secondEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
secondEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et3.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) {
if (s.toString().length() > 0) {
thirdEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
thirdEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et4.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) {
if (s.toString().length() > 0) {
fourthEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
fourthEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean checkAllTrue() {
if (firsEditText && secondEditText && thirdEditText && fourthEditText)
return true;
return false;
}
}
You could do something like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean player1Changed = false, player2Changed = false;
setContentView(R.layout.main);
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
etPlayer1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player1Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
etPlayer2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player2Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}

EditText - change text while typing

I need to replace the text inside the EditText while typing :
Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D").
Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :
final EditText et = (EditText) findViewById(R.id.editTexts);
final TextView tv = (TextView) findViewById(R.id.textView2);
et.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) {
if(s.length() > 0) {
tv.setText(s.toString().substring(s.length()-1));
et.setText("");
}
}
});
You can change it as required::
public class SampleActivity extends Activity {
TextWatcher tt = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView1);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
et.setText(et.getText().toString().replace("A", "C"));
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
}
In order to change the text interactively, you need to register a TextWatcher. But trying to change the text inside the watcher creates further calls to the watcher. My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after
mEditText.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) {
mEditText.removeTextChangedListener(this);
mEditText.setText(//TODO change whatever you like here);
mEditText.setSelection(editable.length()); //moves the pointer to end
mEditText.addTextChangedListener(this);
}

Categories