Wednesday, August 29, 2012

Android | Intent Example of Explicit

As everyone knows, Android application might have more than one screen (activity). It is needed to pass some or all of the values from one activity to another activity. to achieve this, we have intent.

Intent and intent-filters are important component in Android platform.

There are two type of Intent (1) Explicit and (2) Implicit.

Explicit intent, in which target activity is already mentioned in the code. whereas, in Implicit Intent, it is decided run time through Intent-Filters.

Below is code, it explains you how Explicit intent will work. For that we need two activities. here i have added two activities with below names in the application.
(1) IntentExample (calling activity)
(2) Intent_1Example (to be called by IntentExample activity)

activity_intent_example.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvfirstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvfirstname"
        />
<EditText
   android:id="@+id/etfirstname"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/etfirstname"
   android:inputType="text"
   />
    <TextView
        android:id="@+id/tvlastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvlastname"
        />  
       
<EditText
   android:id="@+id/etlastname"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/etlastname"
   android:inputType="text"
   />

<Button
   android:id="@+id/btnok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/btnok"
   />
</LinearLayout>

strings.xml

<resources>

    <string name="app_name">IntentExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_intent_example">Intent Example</string>
    <string name="tvfirstname">First Name:</string>
    <string name="tvlastname">Last Name:</string>
    <string name="etfirstname"></string>
    <string name="etlastname"></string>
    <string name="btnok">OK</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_intent_1_example">Intent_1Example</string>
<string name="tvfirstname1"></string>
<string name="tvlastname1"></string>
</resources>


IntentExample.java

package com.example.intentexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentExample extends Activity implements OnClickListener {

private Button btnOk;
private EditText etFirstName, etLastName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_example);
        
        btnOk = (Button) findViewById(R.id.btnok);
        etFirstName=(EditText) findViewById(R.id.etfirstname);
        etLastName= (EditText) findViewById(R.id.etlastname);
        
        btnOk.setOnClickListener(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_intent_example, menu);
        return true;
    }

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent oIntent = new Intent(this.getApplicationContext(),Intent_1Example.class);
oIntent.putExtra("com.example.intentexample.firstname", etFirstName.getText().toString());
oIntent.putExtra("com.example.intentexample.lastname", etLastName.getText().toString());
startActivity(oIntent);
}
}


In above code, we have passed two values firstname and lastname to Intent_1example activity through intent. but those values are not displayed in Intent_1Example activity.

Below is code which helps you to display the values passed through intent to Intent_1Example activity.

activity_intent_1_example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
android:id="@+id/tvfirstname1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvfirstname1"
/>
    <TextView
android:id="@+id/tvlastname1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvlastname1"
/>
    
</LinearLayout>


Intent_1Example.java

package com.example.intentexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Intent_1Example extends Activity {

private Intent oIntent;
private TextView tvFirstName, tvLastName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_1_example);
        
        oIntent = getIntent();
        tvFirstName = (TextView) findViewById(R.id.tvfirstname1);
        tvLastName = (TextView) findViewById(R.id.tvlastname1);
        tvFirstName.setText("FirstName: " + oIntent.getCharSequenceExtra("com.example.intentexample.firstname").toString());
        tvLastName.setText("Last Name : " + oIntent.getCharSequenceExtra("com.example.intentexample.lastname").toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_intent_1_example, menu);
        return true;
    }
}

Result:






No comments:

Post a Comment