Sunday, 15 July 2012

pass a message from one activity to another in android


                   pass a message from one activity  to another


Here is a simple program for intent to pass a message from one activity  to another.



ButtonclickActivity.java

package a.b;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class ButtonclickActivity extends Activity {

 public final static String EXTRA_MESSAGE=null;

  Button b1;

  EditText e1;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

      

        b1=(Button)findViewById(R.id.button1);

        

        

    }

    public void sendmsg(View v)

    {

     if (v==b1)

     {

     EditText e1=(EditText)findViewById(R.id.edit1);

     Intent i=new Intent(this,msgclass.class);

     //startActivity(i);

     String msg=e1.getText().toString();

     i.putExtra(EXTRA_MESSAGE, msg);

     startActivity(i);

     Toast.makeText(this, "haiiiiii",Toast.LENGTH_LONG).show();

     }

    }

    

} 


 msgclass.java


package a.b;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;


public class msgclass extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

      

        Intent i = getIntent();

        String msg = i.getStringExtra(ButtonclickActivity.EXTRA_MESSAGE);

    

        TextView textView = new TextView(this);

        textView.setTextSize(40);

        textView.setText(msg);

        setContentView(textView);

             

           }

}

 main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <EditText 
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="enter your messagge here.................."
    android:layout_marginTop="50dip"/>
    <Button android:layout_height="62dip"
     android:id="@+id/button1" 
     android:layout_marginLeft="75dip" 
     android:layout_marginTop="75dip" 
     android:text="enter" 
     android:onClick="sendmsg" 
     android:layout_width="62dip">
     </Button>
</LinearLayout>










  

1 comment:

  1. What should we do if i need to get two text input.
    and display them both.

    ReplyDelete