(Using Android Studio)Suppose you are designing a login activity for your androi
ID: 3840804 • Letter: #
Question
(Using Android Studio)Suppose you are designing a login activity for your android app. The android app has two activity (LoginActiviy, NewActivity). Write only the Java Code here for LoginActivity. The login activity is show in the following figure
The user name will be “admin” and password “admin”. The user has only 3 attempts to try to login, for each wrong trail the number of attempts left will be decremented by one. Once the attempts left is zero the login button should be disabled.
If the user enters the user name and password correctly and press login, A new activity “NewActivity” should be opened using intent.
it448testlapp Login User Name Password Attempts Left LOGIN 6:00 etxtname etxt passExplanation / Answer
package com.example.login;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//Activity implemenets OnClickListener
public class LoginActivity extends Activity implements OnClickListener {
//Declaring components
EditText user,pwd;
TextView attempts;
Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login(); // Creating a login method
}
private void login() {
// Getting instances of widgets from R.java
user = (EditText) findViewById(R.id.editText1);
pwd = (EditText) findViewById(R.id.editText2);
attempts = (TextView) findViewById(R.id.textView4);
login = (Button) findViewById(R.id.button1);
//Creating and implementing listener
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_login, menu);
return true;
}
//Calling Overriding method called onClick
public void onClick(View view) {
// Get the data from objects
String u = user.getText().toString();
String p = pwd.getText().toString();
String ats = attempts.getText().toString();
Integer at1 = Integer.valueOf(ats);
//Converting String into integer
int at = at1.intValue();
if(at==0){
//if attempts are 0 then disable Button
login.setEnabled(false);
}
// if it matches then Start Intent
//Create NewIntent as Activity
else if(u.equals("admin") && p.equals("admin")){
Intent in = new Intent(this, NewActivity.class);
startActivity(in);
}else{
//Decrease the attempts value when login is failed
// And again set the attempts to Layout
at=at-1;
attempts.setText(new Integer(at).toString());
}
}
}
Explaination:
LoginActivity implements OnClickListener, by invoking components like EditText, TextView and Button.
Initially get the views from R.java using R.id.name with findViewById() method.
Convert user name and password into String by getting objects using getText(),
Store user, pwd and Also attempts into String object. Sinve attempts is of integer type.
Convert attempts into Integer, and inside onClick method, compare user and pwd with admin,
if it is equal then create a intent to link with NewActivity, otherwise decrease the attempts and
again add to the TextView using setText() method. Do till last 3 attempts, if it matches,
fine otherwise disable the login button using setEnabled(false).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.