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.
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);