This is a mirror of official site: http://jasper-net.blogspot.com/

Passing Data From One Activity to Another Activity in Android

| Sunday, July 24, 2011
Here is my first article about Android on c-sharpcorner.com.

The following screen shows what we will build in this tutorial.
 
We will pass a username and a password in the EditView Control and if they match correctly then we need to move to another Intent else show a message box saying it is invalid. Also if the user successfully logins then we will show a Welcome , {username} message  in TextView.

So the main intention of this article is to explain how to pass data across multiple intents inside Android.

7-22-2011%2010-01-03%20AM.gif

 
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("kirtan") && 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();
                     }
              }
           });
    }
}


Read more: C# Corner
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://www.c-sharpcorner.com/UploadFile/kirtan007/8433/

Posted via email from Jasper-net

0 comments: