Android Intent Compute Salary (passing of value to other layout)

This app shows you on how to pass value from main activity to another. The main activity contains employee name, position, sss, tax, pag-ibig, and philhealth. The basic rate depends on the position of the employee. The computed salary will pass through parameter and display to the other activity.
Code snippet
1. Open “res/layout/main.xml” file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Name" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="text"
        android:id="@+id/idfname" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Position" />
    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/spos"
        android:prompt="@string/pos_prompt"
        android:id="@+id/idposition"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Monthly Rate" />
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/idrate" />
    </LinearLayout>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Deductions" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">       
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="SSS" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="text"
        android:id="@+id/idsss" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">    
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="TAX" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="text"
        android:id="@+id/idtax" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">   
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Pag-ibig" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="text"
        android:id="@+id/idpagibig" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">   
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Philhealth" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="text"
        android:id="@+id/idphilhealth" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Calculate"
        android:id="@+id/idcalculate"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Close"
        android:id="@+id/idclose"/>
    </LinearLayout>
</LinearLayout>
2. Right click the res/layout folder under the Package Explorer pane. Go to new -> android XML file. Enter file "display" then click finish button. Perform the following xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Name:" />
    <TextView
        android:id="@+id/tvname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Position:" />
    <TextView
        android:id="@+id/tvposition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Salary" />
    <TextView
        android:id="@+id/tvsalary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" /> 
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="bottom|center" >
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Back"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:id="@+id/idbalik" />
    </LinearLayout>
</LinearLayout> 
3. Write a code inside activity “onCreate()” methodpackage mobiapps.midterm.activity2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MidtermActivity2Activity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    Button btncalculate,btnclose;
    EditText etnameetsssettaxetpagibigetphilhealth;
    Spinner spposition;
    TextView tvrate;
    double rate,salary,sss,tax,pagibig,philhealth,deduction;
    String sname,sposition;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spposition=(Spinner)findViewById(R.id.idposition);
        btncalculate=(Button)findViewById(R.id.idcalculate);
        btnclose=(Button)findViewById(R.id.idclose);
        etname=(EditText)findViewById(R.id.idfname);
        ettax=(EditText)findViewById(R.id.idtax);
        etsss=(EditText)findViewById(R.id.idsss);
        etpagibig=(EditText)findViewById(R.id.idpagibig);
        etphilhealth=(EditText)findViewById(R.id.idphilhealth);
        tvrate=(TextView)findViewById(R.id.idrate);
        btncalculate.setOnClickListener(this);
        spposition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {
        // TODO Auto-generated method stub
        if (spposition.getSelectedItem().toString().equalsIgnoreCase("Manager"))
             rate=Double.parseDouble("30000");
        else if (spposition.getSelectedItem().toString().equalsIgnoreCase("Regular"))
             rate=Double.parseDouble("20000");
        else if (spposition.getSelectedItem().toString().equalsIgnoreCase("Casual"))
             rate=Double.parseDouble("8000");
             tvrate.setText(String.valueOf(rate));
                  }
    public void onNothingSelected(AdapterView<?> arg0) {
      // TODO Auto-generated method stub
                       
        }    
           
    });
    }
    public void onClick(View v) {
          // TODO Auto-generated method stub
        switch (v.getId()){
            case R.id.idcalculate:
                  tax=Double.parseDouble(ettax.getText().toString());
                  sss=Double.parseDouble(etsss.getText().toString());
                    pagibig=Double.parseDouble(etpagibig.getText().toString());
                      philhealth=Double.parseDouble(etphilhealth.getText().toString());
               deduction =tax + sss + pagibig + philhealth;
               salary=rate-deduction;
               sname=etname.getText().toString();
                  sposition=spposition.getSelectedItem().toString();
               Bundle b = new Bundle();
               b.putString("k_name"sname);
               b.putString("k_position"sposition);
               b.putString("k_salary", String.valueOf(salary));
               Intent i = new Intent("mobiapps.midterm.activity2.DISPLAYOUTPUT");
               i.putExtras(b);
               startActivity(i);
               break;
            }
      }
}

4. To add java file. Right package name, go to new the select class.

package mobiapps.midterm.activity2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayOutput extends Activity{
      TextView vposvnamevsalary;
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);
            vname = (TextView) findViewById(R.id.tvname);
            vpos = (TextView) findViewById(R.id.tvposition);
            vsalary = (TextView) findViewById(R.id.tvsalary);
            Bundle b = getIntent().getExtras();
            String s_name = b.getString("k_name");
            String s_position = b.getString("k_position");
            String s_salary = b.getString("k_salary");
            vname.setText(s_name);
            vpos.setText(s_position);
            vsalary.setText(s_salary);
      }
}
Screenshots
Programming Language: Java + XML
Programming IDE: Eclipse