0% found this document useful (0 votes)
64 views4 pages

Mcda

The document describes creating a login activity in Android that takes a username and password from the user. If the username and password match valid credentials, it will display a welcome message on a new activity. The code provided contains the XML layout files for the login screen and welcome screen activities, as well as the Java code for the login activity class which handles validating the username and password and launching the welcome activity if valid.

Uploaded by

mm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Mcda

The document describes creating a login activity in Android that takes a username and password from the user. If the username and password match valid credentials, it will display a welcome message on a new activity. The code provided contains the XML layout files for the login screen and welcome screen activities, as well as the Java code for the login activity class which handles validating the username and password and launching the welcome activity if valid.

Uploaded by

mm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical:-9

Aim: Using android create a login activity it asks Username and


Password from user. If username and password are valid, it display
welcome message using new activity.

Code

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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"

android:layout_marginLeft="42dp"
android:layout_marginTop="56dp"
android:text="User Name" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="35dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:hint="Enter the user name " >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="21dp"
android:text="Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"

android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:hint=" enter the password"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"

android:text="submit" />
</RelativeLayout>

activity_main2.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="223dp"
android:layout_height="91dp"
android:layout_marginBottom="288dp"
android:text="WELCOME "
android:textSize="30dp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.611"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical9;

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;

public class MainActivity extends AppCompatActivity {


Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
String id = et1.getText().toString();
String password = et2.getText().toString();
if(id.equals("sem") && password.equals("1234"))
{
Intent i= new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
else
{
Toast.makeText(MainActivity.this, "Invalid User name or Password..",
Toast.LENGTH_SHORT).show();

}
}
});
}
}
Output

You might also like