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

Exp 28

The document contains XML layout code for an Android login activity, including UI elements like EditText for username and password, a Button for login, and a ProgressBar for loading. It also includes Java code for the LoginActivity class that handles user input, validates login credentials, and updates the UI based on login results. The LoginResult class is defined to encapsulate the authentication result, either success with user details or an error message.

Uploaded by

sawantkrish562
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)
9 views4 pages

Exp 28

The document contains XML layout code for an Android login activity, including UI elements like EditText for username and password, a Button for login, and a ProgressBar for loading. It also includes Java code for the LoginActivity class that handles user input, validates login credentials, and updates the UI based on login results. The LoginResult class is defined to encapsulate the authentication result, either success with user details or an error message.

Uploaded by

sawantkrish562
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/ 4

EXP 28

android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLay android:autofillHints="@string/prompt_passwor
out d"
android:hint="@string/prompt_password"
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:imeActionLabel="@string/action_sign_in
xmlns:app="http://schemas.android.com/apk/re _short"
s-auto" android:imeOptions="actionDone"
android:inputType="textPassword"
xmlns:tools="http://schemas.android.com/tools" android:selectAllOnFocus="true"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintStart_toStartOf="parent"

android:paddingLeft="@dimen/activity_horizont app:layout_constraintTop_toBottomOf="@+id/us
al_margin" ername" />

android:paddingTop="@dimen/activity_vertical_ <Button
margin" android:id="@+id/login"
android:layout_width="wrap_content"
android:paddingRight="@dimen/activity_horizon android:layout_height="wrap_content"
android:layout_gravity="start"
tal_margin"
android:layout_marginStart="48dp"
android:paddingBottom="@dimen/activity_verti android:layout_marginTop="16dp"
cal_margin" android:layout_marginEnd="48dp"
tools:context=".ui.login.LoginActivity"> android:layout_marginBottom="64dp"
android:enabled="false"
<EditText android:text="@string/action_sign_in"
android:id="@+id/username"
android:layout_width="0dp" app:layout_constraintBottom_toBottomOf="pare
android:layout_height="wrap_content" nt"
android:layout_marginStart="24dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="96dp"
android:layout_marginEnd="24dp" app:layout_constraintStart_toStartOf="parent"

android:autofillHints="@string/prompt_email" app:layout_constraintTop_toBottomOf="@+id/p
android:hint="@string/prompt_email" assword"
android:inputType="textEmailAddress" app:layout_constraintVertical_bias="0.2" />
android:selectAllOnFocus="true" <TextView
android:id="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" android:text="asdf"
/> tools:layout_editor_absoluteX="46dp"
tools:layout_editor_absoluteY="414dp" />
<EditText <ProgressBar
android:id="@+id/password" android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_gravity="center"
android:layout_marginStart="32dp" ry;
android:layout_marginTop="64dp"
android:layout_marginEnd="32dp" public class LoginActivity extends
android:layout_marginBottom="64dp" AppCompatActivity {
android:visibility="gone"
private LoginViewModel loginViewModel;
app:layout_constraintBottom_toBottomOf="pare
nt" @Override
public void onCreate(Bundle
app:layout_constraintEnd_toEndOf="@+id/pass savedInstanceState) {
word" super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
app:layout_constraintStart_toStartOf="@+id/pas loginViewModel =
sword" ViewModelProviders.of(this, new
app:layout_constraintTop_toTopOf="parent" LoginViewModelFactory())
app:layout_constraintVertical_bias="0.3" /> .get(LoginViewModel.class);
final TextView tv =
(TextView)findViewById(R.id.textView);
</androidx.constraintlayout.widget.ConstraintLa final EditText usernameEditText =
yout> findViewById(R.id.username);
final EditText passwordEditText =
findViewById(R.id.password);
final Button loginButton =
findViewById(R.id.login);
final ProgressBar loadingProgressBar =
LoginActivity.java
findViewById(R.id.loading);
package com.example.login2.ui.login; tv.setText(" ");

import android.app.Activity; loginViewModel.getLoginFormState().observe(this


, new Observer<LoginFormState>() {
import androidx.lifecycle.Observer; @Override
import androidx.lifecycle.ViewModelProviders; public void onChanged(@Nullable
LoginFormState loginFormState) {
import android.os.Bundle; if (loginFormState == null) {
return;
import androidx.annotation.Nullable; }
import androidx.annotation.StringRes;
import loginButton.setEnabled(loginFormState.isDataVali
androidx.appcompat.app.AppCompatActivity; d());
if (loginFormState.getUsernameError() !=
import android.text.Editable; null) {
import android.text.TextWatcher;
import android.view.KeyEvent; usernameEditText.setError(getString(loginFormSta
import android.view.View; te.getUsernameError()));
import android.view.inputmethod.EditorInfo; }
import android.widget.Button; if (loginFormState.getPasswordError() !=
import android.widget.EditText; null) {
import android.widget.ProgressBar;
import android.widget.TextView; passwordEditText.setError(getString(loginFormStat
import android.widget.Toast; e.getPasswordError()));
}
import com.example.login2.R; }
import });
com.example.login2.ui.login.LoginViewModel;
import
com.example.login2.ui.login.LoginViewModelFacto loginViewModel.getLoginResult().observe(this,
new Observer<LoginResult>() {
@Override
public void onChanged(@Nullable @Override
LoginResult loginResult) { public boolean onEditorAction(TextView v,
if (loginResult == null) { int actionId, KeyEvent event) {
return; if (actionId ==
} EditorInfo.IME_ACTION_DONE) {

loadingProgressBar.setVisibility(View.GONE); loginViewModel.login(usernameEditText.getText()
if (loginResult.getError() != null) { .toString(),

showLoginFailed(loginResult.getError()); passwordEditText.getText().toString());
} }
if (loginResult.getSuccess() != null) { return false;
}
updateUiWithUser(loginResult.getSuccess()); });
}
setResult(Activity.RESULT_OK); loginButton.setOnClickListener(new
View.OnClickListener() {
//Complete and destroy login activity @Override
once successful public void onClick(View v) {
finish(); String name,pass;
} name =
}); usernameEditText.getText().toString();
pass =
TextWatcher afterTextChangedListener = new passwordEditText.getText().toString();
TextWatcher() {
@Override if(name.equalsIgnoreCase("MHSSP") &&
public void pass.equalsIgnoreCase("MHSSP123"))
beforeTextChanged(CharSequence s, int start, int {
count, int after) {
// ignore //Toast.makeText(getApplicationContext(),"Login
} Successful",Toast.LENGTH_LONG).show();
tv.setText("Login Successful");
@Override }
public void onTextChanged(CharSequence else
s, int start, int before, int count) { {
// ignore
} Toast.makeText(getApplicationContext(),"Login
Unsuccessful",Toast.LENGTH_LONG).show();
@Override //tv.setText("Login Successful");
public void afterTextChanged(Editable s) { }
}
loginViewModel.loginDataChanged(usernameEdit });
Text.getText().toString(), }

passwordEditText.getText().toString()); private void


} updateUiWithUser(LoggedInUserView model) {
}; String welcome = getString(R.string.welcome)
+ model.getDisplayName();
usernameEditText.addTextChangedListener(afterT // TODO : initiate successful logged in
extChangedListener); experience
Toast.makeText(getApplicationContext(),
passwordEditText.addTextChangedListener(afterT welcome, Toast.LENGTH_LONG).show();
extChangedListener); }

passwordEditText.setOnEditorActionListener(new private void showLoginFailed(@StringRes


TextView.OnEditorActionListener() { Integer errorString) {
Toast.makeText(getApplicationContext(),
errorString, Toast.LENGTH_SHORT).show();
}
}

LoginResult.java

package com.example.login2.ui.login;

import androidx.annotation.Nullable;

/**
* Authentication result : success (user details) or
error message.
*/
class LoginResult {
@Nullable
private LoggedInUserView success;
@Nullable
private Integer error;

LoginResult(@Nullable Integer error) {


this.error = error;
}

LoginResult(@Nullable LoggedInUserView
success) {
this.success = success;
}

@Nullable
LoggedInUserView getSuccess() {
return success;
}

@Nullable
Integer getError() {
return error;
}
}

You might also like