LOGO1_03

Pass Value from one Activity to another

We already learn How switch one activity to another activity in android, now we learn how pass value from one activity to another using putExtra()  method.

Now follow the code.

 


IntentCallingFirstActivity.java


package com.androidfromhome.valuepass;

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;

public class IntentCallingFirstActivity extends Activity implements OnClickListener {

    private Button btn_press;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        
        btn_press=(Button)findViewById(R.id.btn_press);
        btn_press.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch(v.getId()){
        
        case R.id.btn_press:
            
            //Creation of Intent Class Object
            Intent intent_obj=new Intent(IntentCallingFirstActivity.this,IntentCallingSecondActivity.class);
            //put value to Intent object.
            intent_obj.putExtra("string_name", "Android From Home");
            intent_obj.putExtra("int_name", 2);
            intent_obj.putExtra("double_name", 12.16);
            
            startActivity(intent_obj);
            
            
            break;
        
        }
        
    }

    
}


IntentCallingSecondActivity.java


package com.androidfromhome.valuepass;

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

public class IntentCallingSecondActivity extends Activity implements OnClickListener {

    
    private Button btn_second;
    private TextView tv_second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        
        //Receive value from first Activity.
        Bundle bundle_obj=getIntent().getExtras();
        String pass_string_value=bundle_obj.getString("string_name");
        int pass_int_value=bundle_obj.getInt("int_name");
        double pass_double_value=bundle_obj.getDouble("double_name");
        
        String result="String value: "+pass_string_value+",  int value: "+pass_int_value+
                ",  Double value: "+pass_double_value;
        
        btn_second=(Button)findViewById(R.id.btn_second);
        btn_second.setOnClickListener(this);
        tv_second=(TextView)findViewById(R.id.tv_second);
        tv_second.setText(result);
        
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch(v.getId()){
        
        case R.id.btn_second:
            finish();
            break;
        
        }
        
    }

    
}


first_activity.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dfdfdf"
    android:orientation="vertical"
    tools:context="com.androidfromhome.valuepass.IntentCallingFirstActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Intent Calling Example"
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_press"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Go Other Activity"
            android:textColor="#ffffff"
            android:textSize="18dp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


second_activity.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dfdfdf"
    android:orientation="vertical"
    tools:context="com.androidfromhome.valuepass.IntentCallingFirstActivity" >

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="10dp"
        android:text="Value"
        android:textColor="#000000"
        android:textSize="18dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Go Back To First Activity"
            android:textColor="#ffffff"
            android:textSize="18dp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget