Checking for space in an edittext - java

I want to check if there's any space in an edittext. I use this method to get the selected text of edittext:
private String getSelection(EditText editText) {
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
Editable edit = editText.getText();
return edit.toString().substring(start, end);
}
But when i do this:
if (getSelection(et).getText.toString.contains(" ")) Log.d("TAG","There's a space")
contains() will be always false.

I think this:
if (getSelection(et).getText.toString.contains(" ")).....
Must be like this:
if (getSelection(et).contains(" ")) .....

Related

Else If textfield (Android)

I am creating a else if statement for a textfield i have on my view, i am using this code right now. But i'm running into a few errors
private EditText editText;
editText = findViewById(R.id.your_custom_id);
//then something like this
Sting text = editText.getText();
if(text.equals("foo") {
// do something
else if(text.equals("bar") {
// do something else
} else {
// something else
}
The errors consist of "Can't resolve symbol for 'sting' or 'equals'. 'Unknown class "editText" in the 'editText = findViewbyid...'. And the last one is 'Else without if' on the "else if(text.equals("bar")" code. How can i resolve this?
See Sting text = editText.getText(); This is totally incorrect.
There is no such thing as Sting it's String.Equal method apply on object types and String is an Object.
Also you need to use toString().
Use String text = editText.getText().toString();
you need to use editText = (EditText)findViewById(R.id.your_custom_id);
instead of editText = findViewById(R.id.your_custom_id);.

for an onClick function, how do I make it so that it gives me the next element in a list from current?

I'm working on an anagram app for android in android studio. I'm currently working on a "skip" method to skip the current word from the list and show me the next word in the list. I need a button to do this, but every time I click it, it brings me to the last element. I'm guessing every time the method is called, it just iterates from beginning to end and shows me the last element.
public void skip (View v){
TextView scrambleView = (TextView) findViewById(R.id.scrambleView);
for (ListIterator<String> i = wordBank.listIterator(); i.hasNext();) {
String item = i.next();
scrambleView.setText(scramble(r, item));
}
}
Instead of using for loop (I don't truly understand why did you use it), just get the next value from the list:
public void skip (View v){
TextView scrambleView = (TextView) findViewById(R.id.scrambleView);
String item = wordBank.get(next_index);
scrambleView.setText(scramble(r, item));
}
If you don't know the index, you can use a method such as this:
public String getNext(String yourWord) {
int idx = wordBank.indexOf(yourWord);
if (idx < 0 || idx+1 == myList.size())
return "";
return wordBank.get(idx + 1);
}
You don't need to use a loop in the first place. You could just use:
public void skip (View v){
TextView scrambleView = (TextView) findViewById(R.id.scrambleView);
String item=wordBank.listIterator().next();
scrambleView.setText(scramble(r, item));
}

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",...

Limit EditText to text (A-z) only

I want an editText that only allows text input from A to z, no numbers or other characters. I've found out I have to use InputFilter but I don't understand how this code works.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
The code you posted adds a custom filter to the EditText field. It checks to see if the character entered is not a number or digit and then, if so, returns an empty string "". That code is here:
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
For your needs, you want to change the code slightly to check if the character is NOT a letter. So, just change the call to the static Character object to use the isLetter() method. That will look like this:
if (!Character.isLetter(source.charAt(i))) {
return "";
}
Now, anything that is not a letter will return an empty string.
Haven't actually done it, but check Androids NumberKeyListener. You can find the source code for it here:
http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/text/method/NumberKeyListener.java.htm
it does exactly the opposite of what you need, but that should be a good enough starting point.

Categories

Resources