Multiple Interface Communication in Android

Here I am posting one another Android example for multiple interface communication. While developing most of the application, we may need multiple interfaces. This could be achieve by creating two activities and calling one from another.

Here in this example, I create TextView(label), EditText(textbox) and Button in main.xml interface.

<?xml version="1.0" encoding="utf-8"?>
<!-- main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:textColor="#444444"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dip"
        android:text="Name:"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_below="@id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:background="@android:drawable/editbox_background"/>
     <Button
        android:id="@+id/btn_ok"
        android:layout_below="@id/et_name"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="15dip"
        android:text="OK" />
</RelativeLayout>

Another activity interface main_hello.xml (we can copy and paste the main.xml and make changes) is created in Project -> res -> layout. Here we provide a TextView(label) and a Button.

<?xml version="1.0" encoding="utf-8"?>
<!-- main_hello.xml -->
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DDDDDD"
    >
	<TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:textColor="#444444"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dip"
        android:text="Hello, "/>
    <Button
        android:id="@+id/btn_back"
        android:layout_below="@+id/tv_show"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="15dip"
        android:text="OK" />

</RelativeLayout>

On FirstActivity.java, which will show the main.xml, we have to add ClickListener to the button.

package com.multiple.example;

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

public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
	public static String name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText nm=(EditText)findViewById(R.id.et_name);
        Button next = (Button) findViewById(R.id.btn_ok);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	name=nm.getText().toString();
                Intent myIntent = new Intent(view.getContext(), SayHello.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

Another interface is created as SayHello.java (we can copy and paste the FirstActivity.java and make changes) which will display the man_hello.xml components with the message (textbox value). And on the click of the button, it will return to the FirstActivity interface.

package com.multiple.example;

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

public class SayHello extends Activity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_hello);
        Button back = (Button)findViewById(R.id.btn_back);
        TextView msg=(TextView) findViewById(R.id.tv_show);
        msg.setText("Hello, " + FirstActivity.name);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), FirstActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

As we have created new Interface (SayHello.java) ourselves, it should be added into AndroidManifest.xml as given below.

<?xml version="1.0" encoding="utf-8"?>
<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.multiple.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FirstActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<activity android:name=".SayHello"></activity>
    </application>
</manifest>

The directory tree will look like this:

Then Your project is ready to Run as below:

 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:id=”@+id/tv_name”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”10pt”
android:textColor=”#444444″
android:layout_alignParentLeft=”true”
android:layout_marginTop=”20dip”
android:text=”Name:”/>
<EditText
android:id=”@+id/et_name”
android:layout_below=”@id/tv_name”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginLeft=”10dip”
android:background=”@android:drawable/editbox_background”/>
<Button
android:id=”@+id/btn_ok”
android:layout_below=”@id/et_name”
android:layout_width=”100dip”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_marginTop=”15dip”
android:text=”OK” />
</RelativeLayout>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: