Mobile Application Development Practical 10
Question 1
Write a program to create a login form for a social networking site.
activity_main.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=".MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:text="Username:" />
<EditText
android:id="@+id/username"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:hint="Enter username here" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
>
<TextView
android:id="@+id/TextView2"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/pass"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:hint="Enter password here"
android:password="true"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp">
<Button
android:id="@+id/loginbtn"
android:layout_width="163dp"
android:layout_height="match_parent"
android:layout_column="1"
android:text="Login"
android:layout_marginLeft="25dp"
android:layout_marginRight="50dp"/>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical10;
import androidx.appcompat.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 {
Button b;
EditText uname,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = findViewById(R.id.loginbtn);
uname = findViewById(R.id.username);
pass = findViewById(R.id.pass);
View.OnClickListener cl= new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = uname.getText().toString();
String pw = pass.getText().toString();
if (user.equals("admin") && pw.equals("Admin"))
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Invalid id or password", Toast.LENGTH_LONG).show();
}
};
b.setOnClickListener(cl);
}
}
Output
