TO DOWNLOAD COMPLETE CODE
CLICK ON LINK BELOW
Screen 1 layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="15dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:text="Login Screen"
android:textSize="30sp"
android:layout_margin="20dp"
android:layout_height="wrap_content"></TextView>
<EditText
android:layout_margin="15dp"
android:layout_width="match_parent"
android:id="@+id/etUsername"
android:hint="Enter Your ID"
android:layout_height="wrap_content"></EditText>
<EditText
android:inputType="numberPassword"
android:layout_margin="15dp"
android:layout_width="match_parent"
android:id="@+id/etPassword"
android:hint="Enter Your Password"
android:layout_height="wrap_content"></EditText>
<Button
android:onClick="btnLoginClick"
android:layout_width="wrap_content"
android:text="Login"
android:id="@+id/btnLogin"
android:layout_height="wrap_content"></Button>
</LinearLayout>
.............
Screen 1 Java File
package com.example.intents;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import javax.xml.transform.Source;
public class Screen1 extends AppCompatActivity
{
EditText etUsername;
EditText etPassword;
String myUsername = "bs130200310";
String myPassword = "111111111";
String myName = "Rehan Attari";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen1);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
}
public void btnLoginClick(View view)
{
String inputUsername = etUsername.getText().toString();
String inputPassword = etPassword.getText().toString();
if (inputUsername.equals(myUsername) && inputPassword.equals(myPassword))
{
Toast.makeText(this, "Login Success", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Screen1.this, Screen2.class);
intent.putExtra("key", myName);
startActivity(intent);
} else
{
Toast.makeText(this, "Invalid Username Or Password. Please Try again", Toast.LENGTH_SHORT).show();
}
}
}
..........
Screen 2 layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:textSize="30sp"
android:layout_width="wrap_content"
android:text="Welcome"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
......
Screen 2 Java File
package com.example.intents;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Screen2 extends AppCompatActivity
{
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen2);
tv = findViewById(R.id.tv);
Intent intent = getIntent();
String receivedName = intent.getStringExtra("key");
tv.setText("Welcome : " + receivedName);
}
}