Intent demo pass with text demo in Android
- First Create Two acitvity
- layout file name
- firstscreen.xml
- second_screen.xml
- java filename
- FirstScreen.java
- SecondScreen.java
firstscreen.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/firstscreen"
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.krbutani.intentpasswithdatademo.Firstscreen">
<Button
android:text="Goto Second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btnsecond" />
</RelativeLayout>
FirstScreen.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class Firstscreen extends AppCompatActivity {
Button second;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);
second = (Button) findViewById(R.id.btnsecond);
second.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent intent1 = new Intent(Firstscreen.this,SecondScreen.class);
intent1.putExtra("first_name","kartik");
intent1.putExtra("last_name","butani");
startActivityForResult(intent1,1);
}
});
}
}
second_screen.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/second_screen"
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.krbutani.intentpasswithdatademo.SecondScreen">
<Button
android:text="Goto First"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btnfirst" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lbldata"
android:layout_below="@+id/btnfirst"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp" />
</RelativeLayout>
SecondScreen.java
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.TextView;
public class SecondScreen extends AppCompatActivity {
TextView lbldata;
Button btnfirst;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_screen);
Intent intent2 = getIntent();
Bundle e = intent2.getExtras();
String firstname = e.getString("first_name").toString();
String lastname = e.getString("last_name").toString();
lbldata = (TextView) findViewById(R.id.lbldata);
lbldata.setText(firstname + "\n" + lastname);
btnfirst = (Button) findViewById(R.id.btnfirst);
btnfirst.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent intent1 = new Intent(SecondScreen.this,Firstscreen.class);
startActivity(intent1);
}
});
}
}
Comments
Post a Comment