📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create a Login application (check username and password). On successful login, pop up a message. ("Welcome username")
  • Code
  • Output
  1. Android

Q10

Question 10

PreviousQ9NextQ11

Last updated 4 years ago

Create a Login application (check username and password). On successful login, pop up a message. ("Welcome username")

App Insights:

Hard coded username and password values, use EditText, TextView and Button. Added Toast widget for both successful and failed login attempt

Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Login Application"
        android:layout_marginTop="25dp"
        android:textSize="25dp"
        android:textColor="@color/black"
        />

    <TextView
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="250dp"
        android:text="Username"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/t1"
        android:layout_marginLeft="50dp" />

    <TextView
        android:id="@+id/t3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="280dp"
        android:text="Password"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/t2" />

    <EditText
        android:id="@+id/e1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="230dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@id/t2"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="50dp" />

    <EditText
        android:id="@+id/e2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="260dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintLeft_toRightOf="@id/t3"
        app:layout_constraintTop_toBottomOf="@id/t2" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="check"
        android:text="Login"
        android:layout_marginTop="230dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>
package com.example.q10_basic_login_app;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

    public void check(View v) {
        String txt = "Welcome ";
        EditText ed1 = (EditText) findViewById(R.id.e1);
        EditText ed2 = (EditText) findViewById(R.id.e2);

        String a=ed1.getText().toString();
        String b=ed2.getText().toString();
        if(a.equals("Jatin") && b.equals("8647"))
        {
            Toast.makeText(this, txt + a, 
            Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(this,"Invalid User", 
            Toast.LENGTH_LONG).show();
        }
    }
}

Output

Download to Run and Test Locally

Q10_Basic_Login_App.apk
Browse Source Code
Studio Screenshot Main Activity (Login Page) (1/9)
App Screenshot Main Activity (Login Page) (2/9)
Studio Screenshot Taking User Credentials (3/9)
App Screenshot Taking User Credentials (4/9)
Studio Screenshot Taking User Credentials (5/9)
Studio Screenshot Validating User With Welcome Toast (6/9)
App Screenshot Validating User With Welcome Toast (7/9)
Studio Screenshot Validating User With Invalid Toast (8/9)
App Screenshot Validating User With Welcome Toast (9/9)