0

According to my requirement in android, if the user presses only "space" and clicks "post" button a toast should be displayed. So i have the code as below, but the toast doesnt get displayed. please can anybody tell why it is not displaying ?

   mEditText.addTextChangedListener(mTextEditorWatcher);    
  Button button = (Button) findViewById(R.id.button1);

 button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        String result =mEditText.getText().toString();
         if(result.contains ("\\s"))
           Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
    }
});
}
DD.
  • 973
  • 2
  • 10
  • 32

5 Answers5

0

I dont think contains get you the best result, try this:

if(result.equals(" "))
    Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
Coderji
  • 7,655
  • 5
  • 37
  • 51
0

Looks like you are trying to use regex with String.contains method. String.contains method works with String. It doesn't work with regex.

See this question for more details: How to use regex in String.contains() method in Java

By the way, you should not check for just one space. You should handle any number of spaces.

if(result.length()!=0 && result.trim().equals(""))
    ShowToast();
Community
  • 1
  • 1
Vikram Singh
  • 1,726
  • 1
  • 13
  • 25
  • 1
    nice move, but if the EditText is empty, it will still show the toast, as i understood @DD wants to show it when only space is entered in the EditText. – Coderji Nov 16 '13 at 05:29
  • thanks it works :) @VikramSingh but any spaces followed by the text doesnt get trimmed. – DD. Nov 16 '13 at 05:40
0

Try this..

if(result.contains (" "))
           Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

You have a two way of solutions

1.You should check this condition by " " --> if(result.contains ("\s"))

Example:

if(result.contains(" "))

2.You Should Check with Pattern

Example:

String result =ed.getText().toString();
       Pattern pattern = Pattern.compile("\\s");
   Matcher matcher = pattern.matcher(result);
   boolean found = matcher.find();
   if(found)
         Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();

Please use any of these solutions.It will work.

Arun Kumar
  • 100
  • 1
  • 2
  • 12
  • DD for Vikram singh's answers works for only one space. But,you will use my second solution it will show toast for every spaces. – Arun Kumar Nov 16 '13 at 05:45
  • Vikram singh's solution will work with one space and multiple space also. String.trim() will remove all the white spaces in the beginning and end – SathMK Nov 16 '13 at 06:10
  • Ok. I thought if enter "arun kumar" this time also will shown the toast. But,Now only i can understand the complete requirement. Sorry vikram singh – Arun Kumar Nov 16 '13 at 06:38
0

I changed the if condition to space and i am getting a Toast message successfully.

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.Button1);
    final EditText mEditText = (EditText) findViewById(R.id.EditText1);

    button.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {
           // TODO Auto-generated method stub
           String result =mEditText.getText().toString();
           Log.e("Test Message", result);
           if(result.contains (" "))
             Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
       }
   });
}

Please try and let me know if you are still facing the issue ...

Thanks

Prem
  • 4,823
  • 4
  • 31
  • 63