Sunday 7 September 2014

Transfer data from one Activity to Another Activity in Android



Transfer data with Intents :


We can easily transfer data from one activity to another activity with the use of Intents in Android Application. In this example I am sending the name (set in edit text) from one activity and will use that name in another activity.


Create a new project in Eclipse : File => New => Android Application Project and give the package name as com.javalanguageprogramming.datatransferfromactivitydemo.

Copy the code in activity_main.xml shown below :


 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   android:background="#ced3e2"  
    >  
   
   <LinearLayout  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal" >  
   
     <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_marginLeft="20dp"  
       android:layout_marginTop="10dp"  
       android:text="Your Name"  
       android:textSize="18sp" />  
   
     <EditText  
       android:id="@+id/nameEdit"  
       android:layout_width="180dp"  
       android:layout_height="40dp"  
       android:layout_marginLeft="20dp"  
       android:layout_marginTop="10dp" />  
   </LinearLayout>  
   
   <Button  
     android:id="@+id/sendButton"  
     android:layout_width="100dp"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:layout_marginTop="10dp"  
     android:text="Send" />  
   
 </LinearLayout>  

In the above xml file a text view is used to give the label “your name” and edit text is used for entering the name and a button is used to send the name to another activity.

Copy the code in activity_receive_data.xml shown below :

 <?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"  
   android:background="#ced3e2"  
    >  
   
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:layout_marginTop="30dp"  
     android:gravity="center"  
     android:text="Hey!"  
     android:textSize="23sp" />  
   
   <TextView  
     android:id="@+id/nameText"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:layout_marginTop="10dp"  
     android:gravity="center"  
     android:textSize="25sp"  
     android:textStyle="bold" />  
   
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:layout_marginTop="10dp"  
     android:gravity="center"  
     android:text="Welcome to"  
     android:textSize="23sp" />  
   
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:layout_marginTop="10dp"  
     android:gravity="center"  
     android:text="http://javalanguageprogramming.blogspot.in"  
     android:textSize="20sp" />  
   
 </LinearLayout>  


In the above code 4 text views are used, out of one is used for the name which is coming from MainActivity.

Copy the code in MainActivity.java shown below :

 package com.javalanguageprogramming.datatransferfromactivitydemo;  
   
 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;  
   
   
 public class MainActivity extends Activity {  
   
      EditText name;  
      Button send;  
        
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
             
           //initialize variables  
           name = (EditText)findViewById(R.id.nameEdit);  
           send = (Button)findViewById(R.id.sendButton);  
             
           //set on click listener on button  
           send.setOnClickListener(new OnClickListener() {  
                  
                @Override  
                public void onClick(View v) {  
                     // check if the edit text is empty or not  
                     if(name.getText() != null || !name.getText().equals("")){  
                          //make an intent  
                          Intent intent = new Intent(MainActivity.this, ReceiveDataActivity.class);  
                          //put data in intent  
                          intent.putExtra("NAME", name.getText().toString());  
                          //clear the edit text so that after user come back he will again enter text  
                          name.setText("");  
                          //start another activity  
                          startActivity(intent);  
                     }  
                }  
           });  
      }  
 }  
   


In this activity, putExtra method of Intent type is used to send the data from this activity to another activity.

Copy the code of ReceiveDataActivity.java shown below :

 package com.javalanguageprogramming.datatransferfromactivitydemo;  
   
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.widget.TextView;  
   
   
 public class ReceiveDataActivity extends Activity{  
   
      TextView nameText;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_receive_data);  
             
           //initialize text view  
           nameText = (TextView)findViewById(R.id.nameText);  
             
           //get the intent  
           Intent intent = getIntent();  
           //get the name with the use of intent  
           String name = intent.getStringExtra("NAME");  
             
           //set the text view  
           nameText.setText(name);  
      }  
   
 }  
   


In this activity, getStringExtra method of intent type is used to receive data in this activity that came from previous activity.

Do not forget to add ReceiveDataActivity in AndroidManifest. The code of AndroidManifest is shown below :

 <?xml version="1.0" encoding="utf-8"?>  
   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.javalanguageprogramming.datatransferfromactivitydemo"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.javalanguageprogramming.datatransferfromactivitydemo.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
   
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
     <activity  
       android:name="com.javalanguageprogramming.datatransferfromactivitydemo.ReceiveDataActivity"  
       android:label="@string/app_name" >  
         
     </activity>  
   </application>  
   
 </manifest>  
   

15 comments :

  1. Thank u for Sharing a good and useful information. Keep Sharing These type of articles

    Hadoop Training in Hyderabad

    Java Training in Hyderabad

    ReplyDelete
  2. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.

    AWS Training in Chennai

    ReplyDelete
  3. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    ReplyDelete
  4. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    python training in velachery | python training institute in chennai



    ReplyDelete
  5. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
    Java training in Marathahalli | Java training in Btm layout

    Java training in Jaya nagar | Java training in Electronic city

    ReplyDelete
  6. Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!

    Data Science Training in Chennai | Data Science training in anna nagar

    Data Science training in chennai | Data science training in Bangalore

    Data Science training in marathahalli | Data Science training in btm layout

    ReplyDelete
  7. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    apple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai

    ReplyDelete
  8. I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  9. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  10. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  11. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    mg university bcom result
    rajasthan university bcom final year result

    ReplyDelete
  12. Great post. Thanks for sharing your brain. Shershaah Full Movie Download & Review Check mine out sometime

    ReplyDelete
  13. Once I initially commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any way you possibly can take away me from that service? Thanks! apple kundendienst berlin

    ReplyDelete
  14. Are you eager to enhance your Java programming skills and stay ahead in the competitive IT landscape? Look no further! Our Java Frameworks Training Course in Noida is meticulously crafted to empower you with the latest tools and techniques in Java development. Noida, a thriving IT hub, provides the perfect backdrop for your learning journey.

    ReplyDelete