login activity

Trrieu112233 avatar
Trrieu112233
plain_text
04/21/2026 2:26 AM
4.8 KB
12
Indexable
package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.autofill.AutofillManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    private EditText edtUsername, edtPassword;
    private Button btnLogin, btnRegister;

    private void loginUser(String inputUser, String inputPass) {
        SharedPreferences pref = getSharedPreferences("AppPrefs", MODE_PRIVATE);
        String jsonString = pref.getString("user_list", "[]");

        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            boolean isFound = false;

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject userObj = jsonArray.getJSONObject(i);

                String savedUser = userObj.getString("username");
                String savedPass = userObj.getString("password");

                if (savedUser.equals(inputUser) && savedPass.equals(inputPass)) {
                    isFound = true;
                    break;
                }
            }
            if (isFound) {
                Toast.makeText(this, "Login success !", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("user_key", inputUser);

                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "Wrong username or password. Please try again !", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

        edtUsername = findViewById(R.id.editLoginUsername);
        edtPassword = findViewById(R.id.editLoginPassword);

        btnLogin = findViewById(R.id.logButton);
        btnRegister = findViewById(R.id.regButton);

        edtUsername.setOnClickListener(v -> {
            if(edtUsername.getText().toString().isEmpty()){
                AutofillManager afm = getSystemService(AutofillManager.class);
                if(afm != null && afm.isEnabled()){
                    afm.requestAutofill(edtUsername);
                }
            }
        });

        edtPassword.setOnClickListener(v -> {
            if(edtPassword.getText().toString().isEmpty()){
                AutofillManager afm = getSystemService(AutofillManager.class);
                if(afm != null && afm.isEnabled()){
                    afm.requestAutofill(edtPassword);
                }
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input_user = edtUsername.getText().toString().trim();
                String input_pass = edtPassword.getText().toString().trim();

                loginUser(input_user, input_pass);
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}
Editor is loading...
Leave a Comment