How to pass variables from one Activity to another in Android

How to pass variables from one Activity to another in Android

Think of a screnario where you are required to pass variables from one Activity to another.For example ,variables entered in a simple login activity are passed to another activity that uses the variables to connect to a database.
I will illustrate using code how you can pass variables from one activity to another.

How to pass variables between two activities.

Let have a MainActivity class that has three textfields.These fields,name,email and message texts while be entered.

Below is the MainActivity Code.



 package net.techoverload.myfirstapp;  
 import android.content.Intent;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.EditText;  
 public class MainActivity extends AppCompatActivity {  
   public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";  
   public final static String EXTRA_NAME="com.example.myfirstapp.NAME";  
   public final static String EXTRA_EMAIL="com.example.myfirstapp.EMAIl";  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   }  
   //This method will be called once the user clicks send message button  
   public void sendText(View view) {  
     Intent intent = new Intent(this, DisplayActivity.class); //Start the intent  
     EditText editTextMessage = (EditText) findViewById(R.id.edit_message); //get variables from textviews  
     EditText editTextName = (EditText) findViewById(R.id.enter_name);  
     EditText editTextEmail = (EditText) findViewById(R.id.enter_email);  
     String message = editTextMessage.getText().toString(); //convert to string variables  
     String name = editTextName.getText().toString();//convert to string variables  
     String email = editTextEmail.getText().toString();//convert to string variables  
     intent.putExtra(EXTRA_MESSAGE,message); //put variables in an intent  
     intent.putExtra(EXTRA_NAME,name);  
     intent.putExtra(EXTRA_EMAIL,email);  
     startActivity(intent);  
   }  
 }  


Explanation:

In the above code,I have created Intent object and named it intent.Using intent.putExtra() method i added all the three string values name,email and message to my intent.

Its time now to create next activity and get our variables that we passed.


 package net.techoverload.myfirstapp;  
 import android.content.Intent;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.ViewGroup;  
 import android.widget.TextView;  
 import org.w3c.dom.Text;  
 public class DisplayActivity extends AppCompatActivity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_display);  
     Intent intent=getIntent();  
     String name=intent.getStringExtra(MainActivity.EXTRA_NAME);  
     String email=intent.getStringExtra(MainActivity.EXTRA_EMAIL);  
     String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);  
     TextView nameView=new TextView(this);  
     nameView.setTextSize(20);  
     nameView.setText(name);  
     TextView emailView=new TextView(this);  
     emailView.setTextSize(20);  
     emailView.setText(email);  
     TextView messageView=new TextView(this);  
     messageView.setTextSize(20);  
     messageView.setText(message);  
     ViewGroup layout=(ViewGroup)findViewById(R.id.activity_display);  
     layout.addView(nameView);  
     layout.addView(emailView);  
     layout.addView(messageView);  
   }  
 }  



COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to pass variables from one Activity to another in Android
How to pass variables from one Activity to another in Android
How to pass variables from one Activity to another in Android
Java Codes
http://www.download-all.net/2017/02/how-to-pass-variables-from-one-activity.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/how-to-pass-variables-from-one-activity.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy