0% found this document useful (0 votes)
27 views3 pages

Main - Java: Package Import Import Import Import Import Import Import Import

The putExtra method is used to pass data between screens in an Android program. It allows passing of different data types like strings, integers, and floats. The data is appended to the intent like a query string, and retrieved on the next screen using the same name. In this example, the username entered in the Main activity is passed to the Success activity using putExtra and retrieved to display a welcome message.

Uploaded by

Jb Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Main - Java: Package Import Import Import Import Import Import Import Import

The putExtra method is used to pass data between screens in an Android program. It allows passing of different data types like strings, integers, and floats. The data is appended to the intent like a query string, and retrieved on the next screen using the same name. In this example, the username entered in the Main activity is passed to the Success activity using putExtra and retrieved to display a welcome message.

Uploaded by

Jb Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The main logic used here is very simple, wherein you just need to use the putExtra Method

to pass the
data across multiple program screens.
The putExtra method accepts many kinds of data, such as String, int, float, byte etc etc ..

It's much like what we do in an ASP.Net query string; we append it with payload and in the next page
we retrieve it with the same name. The same is here; we do putExtra, specifying the name of the variable
and we will retrieve it soon, for another intent.

Main.Java

package com.Program2;
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;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* Get button Text1 Text2 */


final EditText edUsername = (EditText) findViewById(R.id.editText1);
final EditText edPassword = (EditText)findViewById(R.id.editText2);
Button btnValidate = (Button)findViewById(R.id.button1);
btnValidate.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
String uname = edUsername.getText().toString();
String pass = edPassword.getText().toString();

if(uname.equals("abel") && pass.equals("12345"))


{
Intent intent = new Intent(Main.this,Success.class);
intent.putExtra("username",edUsername.getText().toString());
startActivity(intent);
}
else
{
Toast.makeText(Main.this, "Invalid Usename password pair.",
Toast.LENGTH_LONG).show();
}
}
});
}
}

Success.Java

package com.Program2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Success extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Welcome ,"+getIntent().getExtras().getString("username"));
}
}

You might also like