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

I have text in my application and want to check if the text properties in my text view have a value or a null value. then I want to display a toast if the value in my text is null

but the toast doesn't work 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 code onClick method:

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();
        }
+5
source share
7 answers
if(!textview.getText().toString.matches(""))
{
    // not null not empty
}else {
    //null or empty
}
+16
source

In the end, I found a solution to the problem. 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 the following:

android:text=" "

cm? That's my fault. anyway thanks for the help. :)

+3
source

, .

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

textView.getText() != "".

0

, else? !

Perhaps you need to put the toast in the runOnUiThread statement:

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

The getText () method in the TextView returns an EditAble that cannot compare with String; you should convert EditAble to String using the String.ValueOf () method.

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

hope it will be useful

0
source

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

So the solution would be:

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

All Articles