Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to check if textview is null or not null

I have some textview in my application and want to check whether the properties of text on my textview has a value or null. then i want to display a toast if the value on my textview null

but the toast is not running as it should

this is my code :

public class brekeleV2 extends Activity {
static Context context;
brekeleV2Model r = new brekeleV2Model();
private EditText jalan, kota, postal;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = getApplicationContext();
    Button go = (Button)findViewById(R.id.go);
    go.setOnClickListener(onGo);

    Button reset = (Button)findViewById(R.id.reset);
    reset.setOnClickListener(onRes);
    jalan =(EditText)findViewById(R.id.jalan);
    kota =(EditText)findViewById(R.id.kota);
    postal =(EditText)findViewById(R.id.post);
    jalan.setText(null);
    kota.setText(null);
    postal.setText(null);
}

and this is the onClick method code:

private View.OnClickListener onGo=new View.OnClickListener() {

    public void onClick(View v) {
        if (jalan.getText()!= null && kota.getText()!=null){
            switch(v.getId()){
                case R.id.go:
                    Intent maps = new Intent();
                    maps.setClassName("com.openit.brekele", "com.openit.brekele.brekeleV2Nav");
                    brekeleV2Nav.putLatLong(lat, lon);
                    startActivity(maps);
                    break;

            }
        }else{
            Toast msg = Toast.makeText(brekeleV2.this, "Lengkapi Data", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        }
like image 943
DevYudh Avatar asked Jan 23 '12 17:01

DevYudh


1 Answers

TextView has a public length() method you can use to check if there are any characters in the TextView.

    if (textView.length() > 0) {
        // textView is not null
    } else {
        // textView is null
    }
like image 164
Courtney Pattison Avatar answered Sep 29 '22 18:09

Courtney Pattison