How to start Android Activity and pass Variables from One Activity to Another.

Its pretty  much easy to start a activity and pass variables from one activity to another.This post will show you step by step how to...

Its pretty  much easy to start a activity and pass variables from one activity to another.This post will show you step by step how to do this.

1.Our first step is to write MainActivity where am going to add two EditText i.e username and password fields  and a Button then will send action.Then am going to pass these two variables to my next activity using Intent and Display them.

MainActivity  Layout  XML Code.


  

    
    

        

        

        



MainActivity Java Code


package net.techoverload.myfirstapp;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;


public class MainActivity extends AppCompatActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public final static  String EXTRA_PASSWORD="com.example.myfirstapp.PASSWORD";
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Toolbar myToolBar= (Toolbar)findViewById(R.id.my_toolbar) ;
        setSupportActionBar(myToolBar);


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_favorite:
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);


        }



    }
    /*Implementing  sendMessage method*/
    public void sendMessage(View view) {
        EditText message = (EditText) findViewById(R.id.username);
        EditText password = (EditText) findViewById(R.id.password);
        String pass = password.getText().toString();
        String text = message.getText().toString();
        Intent intent = new Intent(this,DisplayActivity.class);
        intent.putExtra(EXTRA_PASSWORD,pass);
        intent.putExtra(EXTRA_MESSAGE, text);

        startActivity(intent);
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}


Second Activity Layout XML File





    

    

    





Second Activity Java Code

package net.techoverload.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
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 message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        String pass=intent.getStringExtra(MainActivity.EXTRA_PASSWORD);


       EditText editUsername=(EditText)findViewById(R.id.edit_username);
        EditText editPass =(EditText)findViewById(R.id.edit_password);
                editUsername.setText(message);
        editPass.setText(pass);






    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_favorite:
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);


        }



    }

}



Code to pass Variables to the other activity


 /*Implementing  sendMessage method*/
    public void sendMessage(View view) {
        EditText message = (EditText) findViewById(R.id.username);
        EditText password = (EditText) findViewById(R.id.password);
        String pass = password.getText().toString();
        String text = message.getText().toString();
        Intent intent = new Intent(this,DisplayActivity.class);
        intent.putExtra(EXTRA_PASSWORD,pass);
        intent.putExtra(EXTRA_MESSAGE, text);

        startActivity(intent);
    }

    /**

Retrieving Variables from the other activity  


Intent intent=getIntent();
        String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        String pass=intent.getStringExtra(MainActivity.EXTRA_PASSWORD);


       EditText editUsername=(EditText)findViewById(R.id.edit_username);
        EditText editPass =(EditText)findViewById(R.id.edit_password);
                editUsername.setText(message);
        editPass.setText(pass);

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 start Android Activity and pass Variables from One Activity to Another.
How to start Android Activity and pass Variables from One Activity to Another.
Java Codes
http://www.download-all.net/2017/06/how-to-start-android-activity-and-pass.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/06/how-to-start-android-activity-and-pass.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