package com.example.abbu.explicit_intent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editTextfield;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Casting
editTextfield = (EditText) findViewById(R.id.editText_field);
btnSend = (Button) findViewById(R.id.button_send);
intent();
}
public void intent(){
btnSend.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = editTextfield.getText().toString();
Intent i = new Intent(MainActivity.this, ReceiverActivity.class);
i.putExtra("message", value);
startActivity(i);
}
}
);
}
}
File Name: activity_main.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:id="@+id/activity_main"
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.abbu.explicit_intent.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="76dp"
android:background="#ECF0F1"
android:id="@+id/editText_field"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:id="@+id/button_send"
android:background="#95A5A6"
android:layout_below="@+id/editText_field"
android:layout_centerHorizontal="true" />
<TextView
android:text="Enter Your Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText_field"
android:layout_alignStart="@+id/editText_field"
android:layout_marginBottom="29dp"
android:id="@+id/textView" />
</RelativeLayout>
File Name: strings.xml
<resources>
<string name="app_name">Explicit_Intent</string>
</resources>
File Name: ReceiverActivity.java
package com.example.abbu.explicit_intent;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Abbu on 4/15/2017.
*/
public class ReceiverActivity extends MainActivity{
TextView tv_receive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver_activity);
// Casting
tv_receive = (TextView) findViewById (R.id.textViewreceive);
Intent in = getIntent();
String string = in.getStringExtra("message");
tv_receive.setText(string);
}
}
File Name: receiver_activity.xml
<?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:text="Hello:"
android:layout_marginTop="200dp"
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView4" />
<TextView
android:layout_marginLeft="100dp"
android:background="#ECF0F1"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewreceive" />
</LinearLayout>
No comments:
Post a Comment