Christine Cloma

Android – Adding Numbers (with Decimals)

Hi folks, finally school year is now over! BUT, not for me until next month because that will be our summer enrollment for upcoming THESIS. :/ I have very limited summer break. Huhu

But it’s okay, I can’t even wait to graduate. 1 year to go! Hopefully by God’s grace!:D

I know its been a very long time to post my programming werks. Sorry for that! So now I’ll post some of my android activities..

Add Numbers

Adding numbers with decimal

The program was designed to compute numbers by adding the first and second number by clicking the “Compute” button. It will automatically generate the Result or the Total answer. As you can see the codes below, I used Float.parseFloat for the decimal numbers. Just in case!

Source Code:

For XML – you can make on your own.
For Java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;

public class add extends Activity {
    /** Called when the activity is first created. */
	
	Button compute;
	EditText firstno, secondno, result;
	OnClickListener onclick;
	TextView Result;	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        compute = (Button) findViewById(R.id.compute);
        firstno = (EditText) findViewById(R.id.firstno);
        secondno = (EditText) findViewById(R.id.secondno);
        result = (EditText) findViewById(R.id.result);
        
        onclick = new OnClickListener() {
        	public void onClick(View arg0){
        		float fn, sn, sum;
        		fn = Float.parseFloat(firstno.getText().toString());
        		sn = Float.parseFloat(secondno.getText().toString());
        		
        		sum = fn + sn;
        		
        		String re = (String.valueOf(sum));
        		result.setText(re.toString());	
        	}
        };
        compute.setOnClickListener(onclick);
    }
}

For more info, just go through this -> Android Developers

 

Tinay, x