Thursday 28 March 2013

StartActivityForResult - Result Getting from Various Activities

Demo:



1)Main Activity:


package com.venky.startactivityforresult1;

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

public class MainActivity extends Activity implements OnClickListener {
      /** Called when the activity is first created. */

      TextView tvResult;

      EditText edtFirstNumber, edtSecondNumber;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            tvResult = (TextView) findViewById(R.id.textView1);

            edtFirstNumber = (EditText) findViewById(R.id.editText1);
            edtSecondNumber = (EditText) findViewById(R.id.editText2);

            Button btnSum = (Button) findViewById(R.id.button1);
            Button btnSub = (Button) findViewById(R.id.button2);
            Button btnMul = (Button) findViewById(R.id.button3);
            Button btnDiv = (Button) findViewById(R.id.button4);

            btnSum.setOnClickListener(this);
            btnSub.setOnClickListener(this);
            btnMul.setOnClickListener(this);
            btnDiv.setOnClickListener(this);
      }

      @Override
      public void onClick(View v) {
            // TODO Auto-generated method stub

            int fNumber, sNumber;

            fNumber = Integer.parseInt(edtFirstNumber.getText().toString().trim());

            sNumber = Integer.parseInt(edtSecondNumber.getText().toString().trim());

            switch (v.getId()) {
            case R.id.button1:

                  Intent intent1 = new Intent(getApplicationContext(),
                              SumActivity.class);
                  intent1.putExtra("num1", fNumber);
                  intent1.putExtra("num2", sNumber);

                  startActivityForResult(intent1, 1);

                  break;

            case R.id.button2:
                  Intent intent2 = new Intent(getApplicationContext(),
                              SubActivity.class);
                  intent2.putExtra("num1", fNumber);
                  intent2.putExtra("num2", sNumber);

                  startActivityForResult(intent2, 1);
                  break;

            case R.id.button3:
                  Intent intent3 = new Intent(getApplicationContext(),
                              MulActivity.class);
                  intent3.putExtra("num1", fNumber);
                  intent3.putExtra("num2", sNumber);

                  startActivityForResult(intent3, 1);
                  break;

            case R.id.button4:
                  Intent intent4 = new Intent(getApplicationContext(),
                              DivActivity.class);
                  intent4.putExtra("num1", fNumber);
                  intent4.putExtra("num2", sNumber);

                  startActivityForResult(intent4, 1);
                  break;
            }

      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 1 && resultCode == 2) {

                  tvResult.setText("Sum is =>" + data.getIntExtra("result", 0));
            } else if (requestCode == 1 && resultCode == 3) {
                  tvResult.setText("Sub is =>" + data.getIntExtra("result", 0));
            } else if (requestCode == 1 && resultCode == 4) {
                  tvResult.setText("Mul is =>" + data.getIntExtra("result", 0));
            } else if (requestCode == 1 && resultCode == 5) {
                  tvResult.setText("Div is =>" + data.getIntExtra("result", 0));
            }
      }

}

2)Sum Activity:

package com.venky.startactivityforresult1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SumActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
     
            Intent intent = getIntent();

            int num1 = intent.getIntExtra("num1", 0);
            int num2 = intent.getIntExtra("num2", 0);

            int result = num1 + num2;

            intent.putExtra("result", result);

            setResult(2, intent);
           
            finish();
      }
}


3)Sub Activity:
 
package com.venky.startactivityforresult1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SubActivity extends Activity {
     
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
     
     
            Intent intent = getIntent();

            int num1 = intent.getIntExtra("num1", 0);
            int num2 = intent.getIntExtra("num2", 0);

            int result = num1 - num2;

            intent.putExtra("result", result);

            setResult(3, intent);
           
            finish();
      }

}


4)Mul Activity:
 
package com.venky.startactivityforresult1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MulActivity extends Activity {

     
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
     
            Intent intent = getIntent();

            int num1 = intent.getIntExtra("num1", 0);
            int num2 = intent.getIntExtra("num2", 0);

            int result = num1 * num2;

            intent.putExtra("result", result);

            setResult(4, intent);
           
            finish();
      }
}


5)Div Activity:
 
package com.venky.startactivityforresult1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class DivActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            Intent intent = getIntent();

            int num1 = intent.getIntExtra("num1", 0);
            int num2 = intent.getIntExtra("num2", 0);

            int result = num1 / num2;

            intent.putExtra("result", result);

            setResult(5, intent);

            finish();
      }
}


6)main.xml:
 
<?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:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Enter First Number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter Second Number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sum" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sub" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Mul" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Div" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>



7)Download this Project Click Here

No comments:

Post a Comment