Multiple Interface Example Android

This example will show how multiple interface communication will be done in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it MultipleInterfaceExample.

2.) Write following into main.xml:

 

<?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>

 

 

3.) Create and write following into main_hello.xml:

 

<?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>

 

 

4.) Create and write following into src/SayHello.java:

 

package com.example.multipleinterfaceexample;

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, " + MultipleInterfaceExampleActivity.name);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MultipleInterfaceExampleActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}

 

 

5.) Add following into manifest file:

 

<activity android:name=".SayHello"></activity>

 

 

6.) Run for output.

Steps:

1.) Create a project named MultipleInterfaceExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: MultipleInterfaceExample
Package Name: com. example. MultipleInterfaceExample
Activity Name: MultipleInterfaceExampleActivity
Min SDK Version: 2.2

2.) Open MultipleInterfaceExampleActivity.java file and write following code there:

 

package com.example.multipleinterfaceexample;

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 MultipleInterfaceExampleActivity 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);
}
});
}
}

 

 

3.) Compile and build the project.

Output

Leave a Comment:

2 comments
mudit srivastava says March 20, 2013

the above code is not running …..

Reply
giamp says May 23, 2013

Hi,
The above code is working but its for another example.
It’s an app taking your name from a text field and typing “hello, yourName”.
Please correct the code!
Thanks.

Reply
Add Your Reply