0% found this document useful (0 votes)
8 views5 pages

LAB1

The document provides steps to create a login screen with Android Studio using Java. It includes adding colors, creating a custom edittext drawable with rounded corners, designing the layout with cardview and textviews, and adding code to the activity to check username and password on login button click and display a toast.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

LAB1

The document provides steps to create a login screen with Android Studio using Java. It includes adding colors, creating a custom edittext drawable with rounded corners, designing the layout with cardview and textviews, and adding code to the activity to check username and password on login button click and display a toast.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB1: TẠO FORM ĐĂNG NHẬP LOGIN VỚI ANDROID STUDIO BẰNG JAVA

Bước 1: Mở Android Studio, Tạo dự án mới và chọn Hoạt động trống. Bấm vào Kết thúc.

Bước 2: Thêm Màu vào thư mục res ->values->colors.xml

<color name="purple">#8692f7</color>

Bước 3: Tạo custom_edittext.xml

Right-click on the drawable folder under the res folder then click on New -> Drawable
Resource File. The round corners are 20dp, you can customize them as per your
requirements.

Nhập mã dưới đây:

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


<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
android:width="3dp"
android:color="@color/purple"/>
<corners
android:radius="20dp"/>
</shape>
Bước 4: Nhập mã bên dưới vào activity_main.xml

we have added the CardView background as custom_edittext.xml which will provide


round corners with purple color

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


<LinearLayout

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"
xmlns:card_view="http://schemas.android.com/apk/res-
auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/loginbkg"
tools:context=".MainActivity">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp"
android:background="@drawable/custom_edittext">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="24dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/loginText"
android:textSize="36sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/purple"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/username"

android:background="@drawable/custom_edittext"
android:drawableLeft="@drawable/ic_baseline_person_24"
android:drawablePadding="8dp"
android:hint="Username"
android:padding="8dp"
android:textColor="@color/black"

android:textColorHighlight="@color/cardview_dark_background"
android:layout_marginTop="40dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/password"

android:background="@drawable/custom_edittext"

android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:hint="Password"
android:padding="8dp"
android:inputType="textPassword"
android:textColor="@color/black"

android:textColorHighlight="@color/cardview_dark_background"
android:layout_marginTop="20dp"/>

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/loginButton"
android:text="Login"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/purple"
app:cornerRadius = "20dp"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Not yet registered? SignUp Now"
android:textSize="14sp"
android:textAlignment="center"
android:id="@+id/signupText"
android:textColor="@color/purple"
android:layout_marginBottom="20dp"/>

</LinearLayout>
Bước 5: Nhập nội dung code vào MainActivity.java

We have set the username as “user” and the password as “1234”, so if the string matches
with the user’s string then the toast will be shown as Login Successful but if the user’s string
does not match with the static string then the toast will be shown as “Login Failed!”.

package com.example.loginscreen;

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 {

EditText username;
EditText password;
Button loginButton;

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

username = findViewById(R.id.username);
password = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);

loginButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
if
(username.getText().toString().equals("user") &&
password.getText().toString().equals("1234")) {
Toast.makeText(MainActivity.this,
"Login Successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Login Failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
}

Đầu ra

You might also like