Add a character in an EditText - java

I need to add a character "S" right after the last written number in an EditText (Oe) so:
if i write i number : "123", it must send "123S" instead. If i write "1234", it must send "1234S" instead.
How to do that ?
my code:
((Button) findViewById(R.id.btn_write_HRM_Noty)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
str = Oe.getText().toString();
str = str.substring(0, 0) + "G" + str.substring(0, str.length());
mService.enableHRNotification(mDevice, str);
}
});

I'm a little confused. Are you simply asking how to add a character to a string? If so: str+='S'; will work. Then you simply add the string back to the EditText with .setText(str) or simply put it back into your notification with 'S' added (as Blundell suggested).

mService.enableHRNotification(mDevice, str + "S");

1) Make a new string including by putting together the edittext and the character you want to add finalString = (string + "s"). Then use the setText()method to change the contents shown in the edittext
2) You could do this directly after the user enters something in the text box (after it loses focus or they finish typing and go to the next field)
Check this thread out: How can i know when a edittext lost focus

Related

Failed to get string from autocompletebubbletext

i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!

EditText to enter 3 alphas and 4 digits - change input type dynamically

I need to set one of my EditText to enter three alpha value and four digits Ex:- ABC1234. So I need to change automatically input type from Text to phone when user typed three alphas. I came up with following code and but its not change to phone input type and text type is remains for any text length.
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
if(text.length()==1 || text.length()==2 || text.isEmpty()){
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
System.out.println("Length Text type : " + text.length());
}else{
editText.setRawInputType(InputType.TYPE_CLASS_PHONE);
System.out.println("Length phone type : " + text.length());
}
}
What I have done wrong.
setRawInputType() is usually used when you initialize the view, in a constructor of a custom view or in onCreate() method of an activity.
In your case you have to call setInputType() change the mode of the soft keyboard.

Simple Calculator Not working

Can anyone tell me what's wrong with the following listener? I'm always crashing...
#Override
public void onClick(View view) {
Editable num1 = NumberOne.getText();
Editable num2 = NumberTwo.getText();
int um1 = Integer.parseInt(num1.toString());
int um2 = Integer.parseInt(num2.toString());
Results.setText(um1 + um2);
}
The setText method accepts a String as an argument. The result of um1 + um2 is going to be integer. I'd suggest you first convert the result to a String and then set it inside the setText method.
Something like this should work:
Results.setText(Integer.toString(um1+um2));
Even better you can do this:
Results.setText(Integer.toString(Integer.parseInt(num1.toString()) + Integer.parseInt(num2.toString())));
The reason that it is not working is because you cannot set text to an. You have to use Results.setText(String.valueOf(um1 + um2)) (as Andre stated) to convert the integers to strings. Therefore, the setText will work.

EditText text cannot be converted to int as there is no text there

Basically I need to check that the user input from inputET (an EditText) is equal to the integer, correctAnswer. The problem I'm getting is that "" (which is the text in the EditText field) cannot be converted to an int. Is there any other ways of achieving this or catching the error, I've tried the following code which to my understanding asks if the string in the EditText is not equal to "". Am i going the right way about this or is there an easier way?
// check the input
if (inputET.getText().toString() != "") {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
if the user inputs the same int as the correctAnswer integer then the EditText text is reset to "".
Thanks for any help.
try this:
if (!inputET.getText().toString().equals("")) {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
Used .equals() method for String comparison.
Based on your requirement I think using TextUtil class will be right way to go for checking the edittext is empty or not.
if(!TextUtils.isEmpty( inputET.getText().toString())){
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
rather tha doing if (inputET.getText().toString() != "") have a try with
if (!inputET.getText().toString().equals(""))
print the "inputET.getText().toString()" to console and see what it returns. I would hope you need to check the following
String str = inputET.getText().toString()
if (null!=str && str.trim().equals("")) {
if(inputET.getText().toString()!=null&&!(inputET.getText().toString().isEmpty())){
//code for mot null
}else{
//code for null
}

Get entire text from EditText

When I enter "sa" into EditText it takes only s. How can I pass my entire text?
Suppose I want to compare the text with the string "sa", how to get entire text from edittext?
e = (EditText) findViewById(R.id.selection);
//edittext watcher
e.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//getting edittext
String s1 =s.toString();
System.out.println("edittext :" + s1);
// compare edittext string woth arraylist
if (s1.length() >=1) {
//taking stringbuilder to add list items StringBuilder sb = new StringBuilder();
for(inti=0;i<orginalarrayelements.length;i++)
{
//compare edittext with arraylist with region matches if(orginalarrayelements[i].regionMatches(true, 0, s1, 0, s1.length())){
sb.append(orginalarrayelements[i] +",");
System.out.println("modified array: " + sb);
}
}
I think you are trying to get the full text from your EditText, right?
OnTextChanged gets called every time your text changes, so it will always only contain either one letter or null if you deleted a letter.
If you want to get the full text, call e.getText().toString(); in your OnTextChanged, OnClick on a Button or anywhere else.
By the way: rethink you names: "e" is not a name you should use. Take something like "editText", "myEditText", "usernameEditText",...

Categories

Resources