Skip to main content

AMP PRACTICAL 7


 

Aim: Create an Android application to pass the data (Program Intent passing)

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

tools:context=".MainActivity">

 

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

 

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal"

android:text="Input yourName"></TextView>

<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:stretchColumns="1">

<TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">

 

<TextView android:id="@+id/textView1" android:layout_width="match_parent"

android:layout_height="wrap_content" android:text="FirstName"></TextView>

 

<EditText android:id="@+id/etFName"

android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp">

<requestFocus></requestFocus>

</EditText>

</TableRow>

<TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content"

android:text="LastName"></TextView>

<EditText android:id="@+id/etLName"

android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp"></EditText

> 

</TableRow>

</TableLayout>

<Button

android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Submit"></Button>

</LinearLayout>

</RelativeLayout >

MainActivity.java

 package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;

import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;

import android.widget.QuickContactBadge; import android.widget.TextView;

import android.widget.Toast; import java.util.Random;

 

 

public class MainActivity extends AppCompatActivity { EditText etFName;

EditText etLName;

Button btnSubmit;

 

@Override

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

etFName = (EditText) findViewById(R.id.etFName); etLName = (EditText) findViewById(R.id.etLName); btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v) { Intent intent = new

Intent(getApplicationContext(),MainActivity2.class);

intent.putExtra("fname",etFName.getText().toString());

intent.putExtra("lname",etLName.getText().toString());

 startActivity(intent);

}

});

}

}


Activity_Main2.xml

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

tools:context=".MainActivity2">

 

<TextView android:id="@+id/tvView" android:layout_width="400dp" android:layout_height="85dp"

android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="Output" android:textColor="@android:color/holo_blue_dark" android:textSize="24sp"

android:textStyle="bold">

</TextView>

 

</RelativeLayout>

 

MainActivity2.java

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import android.content.Intent; import android.widget.TextView;

 

public class MainActivity2 extends AppCompatActivity { TextView tvView;

 

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2);

tvView = (TextView) findViewById(R.id.tvView); Intent intent = getIntent();

String fName = intent.getStringExtra("fname"); String lName = intent.getStringExtra("lname");

tvView.setText("Your name is: " + fName + " " + lName);

}

}

 

Output












Comments