Thursday 21 March 2013

Data Transfer using Intent


Demo:


1)Data Transfer Activity:

 
package com.venky.datatransfer;

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;

public class DataTransferusingIntent extends Activity {
      /** Called when the activity is first created. */
      EditText edtUsername, edtPassword;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btnLogin = (Button) findViewById(R.id.btnLogin);

            edtUsername = (EditText) findViewById(R.id.edtUserName);
            edtPassword = (EditText) findViewById(R.id.edtPassword);

            btnLogin.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String Username, Password = null;

                        Username = edtUsername.getText().toString();
                        Password = edtPassword.getText().toString();

                        Intent intent = new Intent(getApplicationContext(),
                                    SecondScreen.class);
                        intent.putExtra("username", Username);
                        intent.putExtra("password", Password);
                        startActivity(intent);
                  }
            });
      }
}


2)SecondScreen Activity:

package com.venky.datatransfer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class SecondScreen extends Activity {
      EditText edtUsername, edtPassword;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.secondscreen);

            Bundle b = getIntent().getExtras();

            String UserName = b.getString("username");
            String Password = b.getString("password");

            TextView tvUserName = (TextView) findViewById(R.id.tvForUserName);
            TextView tvPassword = (TextView) findViewById(R.id.tvForPassword);

            tvUserName.setText(UserName);
            tvPassword.setText(Password);
      }
}


3)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center|center_vertical"
    android:background="@color/myFrontEnd"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="UserName" />

    <EditText
        android:id="@+id/edtUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Password" />

    <EditText
        android:id="@+id/edtPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Login" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel" />

</LinearLayout>


4)secondscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center|center_vertical"
    android:background="@color/myBackground"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Your UserName and Password"
        android:textSize="15dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvForUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/tvForPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

</LinearLayout>


5)Download this Project Click Here
  

No comments:

Post a Comment