Send data to an Activity

Send data to an Activity in Android

MainCLASS FILE

package com.example.malik.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {

EditText data;
public final static String MESSAGE_KEY = “com.example.malik.test.message_key”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void sendMessage(View view){
data = (EditText) findViewById(R.id.editText_data);
String message = data.getText().toString();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY, message);
startActivity(intent);
}

}

MainXML FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<EditText
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/editText_data” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Send”
android:id=”@+id/bSend”
android:layout_gravity=”center_horizontal”
android:onClick=”sendMessage”
/>
</LinearLayout>

 

SecondActivity CLASS

package com.example.malik.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

public final static String MESSAGE_KEY = “com.example.malik.test.message_key”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
textView.setText(message);
setContentView(textView);

}
}

 

SecondActivity XML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.SecondActivity”>

</RelativeLayout>

 

Manifest FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.malik.test”>

<uses-permission android:name=”android.permission.INTERNET” />

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.SecondActivity”></activity>
</application>

</manifest>

 

Output:

pic1 pic2

 

 

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.