Tag Archives: Intent

Many people told me that you should teach android from the scratch , I told them yes I will and I am , So come to the point here is your tutorial after knowing basic widgets in Android I am going to teach you how to switch between one Activity to another and then send data from one Activity to another. Data or values can be send from one Activity to another using intent.putExtras(key,value).

This is possible via Intent and it is of two type Implicit Intent and Explicit Intent .What’s the difference between Implicit Intent and Explicit Intent ? have a look . Implicit Intent is not specified the component.

Lets take an example – “Java file should call implicit Intent asked the system to perform the service without telling the system which Java file do the service.”

implicit Intent

1

2

3

Intenti=newIntent(Intent.ACTION_VIEW);

i.setData(Uri.parse("http://www.androidarena.com"));

startActivity(i);

Explicit Intent is something which gives the name of the componet .Lets take an example –

Lets start with design part for this project , start with screen1.xml . you have to take two editText ,Two TextView and one Button that’s very simple and already teach that stuff see my previous tutorials . screen1.xml

Now prepare second screen so that if you get values from first Activity then where it will go. so for that you have to take three TextView and one Button. screen2.xml

screen2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="You Typed ...."

android:textSize="25dip"

android:gravity="center"

android:layout_margin="15dip"/>

<TextView android:id="@+id/txtName"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_margin="15dip"

android:textSize="18dip"/>

<TextView android:id="@+id/txtpass"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_margin="15dip"

android:textSize="18dip"/>

<Button android:id="@+id/btnClose"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="15dip"

android:text="Close"/>

</LinearLayout>

Now have a look on Java part for this application , It is very simple. we are using the concept that we are getting value from EditText and then using i.putExtras(key,getValue from edittext) values can be send . FirstActivity.java

FirstActivity.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

packageandroidarena.androidswitchviews;

import androidarena.androidswitchviews.R;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

publicclassFirstActivityextendsActivity{

// Initializing variables

EditText inputName;

EditText inputpass;

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.screen1);

inputName=(EditText)findViewById(R.id.name);

inputpass=(EditText)findViewById(R.id.pass);

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

btnNextScreen.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(View arg0){

//Starting a new Intent

Intenti=newIntent(getApplicationContext(),SecondActivity.class);

//Sending data to another Activity

i.putExtra("name",inputName.getText().toString());

i.putExtra("pass",inputpass.getText().toString());

// starting new activity

startActivity(i);

}

});

}

}

Now you have to code for SecondActivity,java. On the receiver side we are having getStringExtras(key) .