Kerv
Kerv

Reputation: 179

How to set default value to 0 in edittext when nothing is inputted?

I have an app here which adds the number. I have 4 edittexts here. What I want to happen is that when i entered none in one of the edittexts, it will assume that I entered 0. How can it be done? Here is my code:

public class Order extends Activity {
Button GoBackHome;
private Button button1;  
private EditText txtbox1,txtbox2,txtbox3,txtbox4;  
private TextView tv;
Button PayNow;

@Override
public void onBackPressed() {
}

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

        GoBackHome = (Button) findViewById(R.id.gohomebutton);
        PayNow = (Button) findViewById(R.id.button2);
        txtbox1= (EditText) findViewById(R.id.editText1);  
        button1 = (Button) findViewById(R.id.button1);  
        tv = (TextView) findViewById(R.id.editText5);  
        txtbox2= (EditText) findViewById(R.id.editText2);
        txtbox3= (EditText) findViewById(R.id.editText3);
        txtbox4= (EditText) findViewById(R.id.editText4);
        button1.setOnClickListener(new clicker());  

        GoBackHome.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent i = new Intent(Order.this, MainActivity.class);               
                startActivity(i);
            }
        });

        PayNow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent i = new Intent(Order.this, Payment.class);               
                startActivity(i);
            }
        });


}


class clicker implements Button.OnClickListener  
{  
    public void onClick(View v)  
    {  
        String a,b,c,d;  
        Integer vis;  
        a = txtbox1.getText().toString();  
        b = txtbox2.getText().toString();
        c = txtbox3.getText().toString();
        d = txtbox4.getText().toString();
        vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;  
        tv.setText(vis.toString());  
    }  
}  

}

Upvotes: 5

Views: 18407

Answers (4)

Hugo Hideki Yamashita
Hugo Hideki Yamashita

Reputation: 221

You can set android:hint="0" in your XML file, then, in your code, you can check if it's empty (maybe using TextUtils.isEmpty()) and setting some variable to 0.

android:hint="0" will make a "0" appear in your EditTexts, but the "0" will disappear when anything is inputted.

Then you can change the onClick() to this:

class clicker implements Button.OnClickListener {  
    public void onClick(View v) {  
        String a,b,c,d;  
        Integer vis;  
        a = txtbox1.getText().toString();  
        b = txtbox2.getText().toString();
        c = txtbox3.getText().toString();
        d = txtbox4.getText().toString();

        try {
            vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;  
            tv.setText(vis.toString());
        } catch (NumberFormatException e) {
            vis = "0";
        }

        // Do something with "vis"
    }  
} 

Or you can create a method to check a value, try to parse to an int or return a default value.

public int getInt(String edtValue, int defaultValue) {
    int value = defaultValue;

    if (edtValue != null) {
        try {
            value = Integer.parseInt(edtValue);
        } catch (NumberFormatException e) {
            value = defaultValue;
        }
    }

    return value;
}

Then you change your call to

vis = this.getInt(a, 0) * 2 + this.getInt(b, 0) * 3 + this.getInt(c, 0) * 4 + this.getInt(d, 0) * 5; 

Upvotes: 1

Rachit Mishra
Rachit Mishra

Reputation: 6112

Check the EditText length when you get it

String value = null;

if(ed.getText().length()){
 value = textBox.getText().toString();
} else 
 value = 0+"";

Upvotes: 1

Andrew G
Andrew G

Reputation: 1557

You can do as Tushar said or you can initialize the value in the XML. Something like

<EditText 
    android:name="@+id/editText1"
    android:text="0"/>

FYI you might also find it cleaner to handle your button clicks on xml like:

<Button
    android:name="@+id/btn1"
    android:onClick="handleClicks"/>

and then in java you'd have a public void method:

public void handleClicks(View clickedView){
   if(clickedView.getId() == btn1.getId(){
       ...
   } else if (...){}
}

Upvotes: 9

Tushar Gupta
Tushar Gupta

Reputation: 15923

initialize as :

txtbox1.setText("0");

Upvotes: 1

Related Questions