android : Check EditText is null

Tags: , ,



i want to check and give condition to my edittext, if i input something to my edittext, l it will change my imageview, this is my code:

 if (nama_pp.getText().toString().length()==0){
            ImageView image_status=(ImageView)polis.findViewById(R.id.image_status_1);
            image_status.setImageResource(R.drawable.espaj_yellow_checklist);
        }else {
            ImageView image_status=(ImageView)findViewById(R.id.image_status_1);
            image_status.setImageResource(R.drawable.espaj_gray_checklist);
        }

i have problem here, my imageview not change…. is this code has true or not?

Answer

If I read your post correctly, you want to check if the EditText is ever empty as the user types. If that’s right, then try the following.

This line needs to appear in the onCreate method after the call to setContentView:

ImageView image_status=(ImageView)polis.findViewById(R.id.image_status_1);

Then, add this code after nama_pp has been assigned:

nama_pp.addTextChangedListener(new TextWatcher()
    {
        public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
        {
            // TODO: Implement this method
        }

        public void afterTextChanged(Editable p1)
        {
            // TODO: Implement this method
        }

        public void onTextChanged(CharSequence s, int start, int before, int count)
        { 
            if(s.length() == 0)
            {
                image_status.setImageResource(R.drawable.espaj_yellow_checklist);
            }
            else
            {
                image_status.setImageResource(R.drawable.espaj_gray_checklist);
            }
        }
    }
);


Source: stackoverflow