DevYudh
DevYudh

Reputation: 2737

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();
        }

Upvotes: 9

Views: 44342

Answers (9)

Courtney Pattison
Courtney Pattison

Reputation: 1606

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
    }

Upvotes: 1

CHarris
CHarris

Reputation: 2793

I would have modified the correct answer a bit (missing () after toString but we're not allowed edit code so:

if(textview.getText().toString().matches(""))
{
    // is null or empty
}else {
    //not null or empty
}

Upvotes: 0

Ibrahim Iqbal
Ibrahim Iqbal

Reputation: 594

if (jalan.getText()!= null && kota.getText()!=null) This is the line of code that doesn't work for you. Best solution for such scenario is using matches("") method for String

So the solution will be:

     if (!jalan.getText().toString.matches("") && !kota.getText().toString().matches("")){
   // code for not null textViews 
}
else
{
//Code for nul textView
}

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

if(!textview.getText().toString.matches(""))
{
    // not null not empty
}else {
    //null or empty
}

Upvotes: 25

Rafal Enden
Rafal Enden

Reputation: 3184

This is what I would use.

if (mText1.getText().length() > 0 && mText2.getText().length() > 0) {
   // Your code.
}

Upvotes: 5

Shayan D
Shayan D

Reputation: 619

Method getText() in TextView returns EditAble that cant compare with String you should convert EditAble to String using String.ValueOf() method.

String jalanContent = String.ValueOf(jalan.getText());
if(jalanContent.equals(""){
//do your action here
} else {
//some action here
}

hope it'd be usefull

Upvotes: 0

DevYudh
DevYudh

Reputation: 2737

in the end i found my problem solving. here is my code:

if (jalan.getText().toString().length()>0 || kota.getText().toString().length()>0){
            //switch(v.getId()){
                //case R.id.go:
                    Intent maps = new Intent();
                    maps.setClassName("com.openit.razer", "com.openit.razer.mapRazerNav");
                    mapRazerNav.putLatLong(lat, lon);
                    startActivity(maps);
                    //break;

            //}
        }else{
            Toast msg = Toast.makeText(razerNav.this, "Lengkapi Data", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();

        }

i used to check the length, because on my xml i add like this :

android:text=" "

see? this is my fault. anyway thanks for helping me. :)

Upvotes: 2

cassioso
cassioso

Reputation: 976

Are you sure that the else condition is invoked? Debug it!

Maybe you need to put the toast into a runOnUiThread statement:

runOnUiThread(new Runnable() {
    public void run() {
        mText.setText("Hello there, " + name + "!");
    }
});

Upvotes: 0

bschultz
bschultz

Reputation: 4254

Try textView.getText() != "" instead.

Upvotes: 0

Related Questions