Thursday 28 March 2013

StartActivityForResult Demo



 Demo:


1)Calling Activity:
 
package com.venky.startactivityforresult;

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 CallingActivity 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 btnCalculateResult = (Button) findViewById(R.id.button1);

            btnCalculateResult.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());

            edtFirstNumber.setText("");
            edtSecondNumber.setText("");

            Intent intent = new Intent(getApplicationContext(),
                        CalledActivity.class);

            intent.putExtra("num1", fNumber);
            intent.putExtra("num2", sNumber);

            startActivityForResult(intent, 1);
      }

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

            tvResult.setText("Sum is =>" + data.getIntExtra("result", 0));

      }
}


2)Called Activity:
 
package com.venky.startactivityforresult;

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

public class CalledActivity 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)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" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Caluculate Sum"
        android:textSize="20dp" />

    <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>


4)Download this Project Click Here
 

No comments:

Post a Comment