Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Android Studio*/ An app consists of two screens: 1) a Welcome screen and 2) a

ID: 3886567 • Letter: #

Question

/* Android Studio*/

An app consists of two screens: 1) a Welcome screen and 2) an AccessControl screen.

First open the app with Welcome screen, containing a TextView widget that displays the message ”This app is locked” and a Button labeled ”open”. When the ”open” button is clicked, the AccessControl screen is displayed.

The AccessControl screen has 4 Buttons for the digits 1-4, and a ”enter” Button. The digit buttons are to enter the access code, while the ”enter” Button checks the code and returns to the Welcome screen regardless of whether the code is correct. If the code is correct the TextView in the Welcome screen should display the ”The App is Unlocked.” message. However, if the code is incorrect, the TextView should display the locked message.

Password is : 1234

Explanation / Answer

Step 1: LayOut Screen 1

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="0dp"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context=".LoginActivity"

android:orientation="vertical"

android:gravity="top"

android:background="@android:color/background_light">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:weightSum="2"

android:gravity="center"

android:orientation="vertical"

>

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1" >

<TextView

android:text=""

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:id="@+id/TVStatus"

/>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1">

<Button

android:text="Open"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btnOpen" />

</LinearLayout>

</LinearLayout>

  

  

</LinearLayout>

Step 2: LayOut Screen 2

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="0dp"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context=".AccesscontrolActivity"

android:orientation="vertical"

android:gravity="top"

android:background="@android:color/background_light">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:weightSum="2"

android:gravity="center"

android:orientation="vertical"

>

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="horizontal">

  

<Button

android:text="1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btn1" />

<Button

android:text="2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btn2" />

<Button

android:text="3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btn3" />

<Button

android:text="4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btn4" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1">

<Button

android:text="Enter"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/btnEnter" />

</LinearLayout>

</LinearLayout>

  

  

</LinearLayout>

Step 3: LoginActivity Code

package com.example.mycompany.accesscontrol;

import android.content.Intent;

import android.content.SharedPreferences;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class LoginActivity extends ActionBarActivity {

//Declare Variables

Button btnOpen;

String displayText;

TextView TextViewDisplay;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

//Assign controls from layout to variables

btnOpen = (Button) findViewById(R.id.btnOpen) ;

TextViewDisplay = (TextView) findViewById(R.id.TVStatus);

TextViewDisplay.setvisibility(View.VISIBLE);

// SharedPreferences to store login status

SharedPreferences preflogstat = getApplicationContext().getSharedPreferences("LogPref", MODE_PRIVATE);

  

String strPref = preflogstat.getString("logstatus", null);

//Ckeck shared preference value and display appropriate message

if(strPref != null) {

if(strPref.equals("open"))

{

//Display unlocked or success message

TextViewDisplay.setText("The app is Unlocked!")

}

else

{

//Display locked message

TextViewDisplay.setText("This app is locked!")

}

}

else

{

//Display locked message

TextViewDisplay.setText("This app is locked!")

}

btnOpen.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Initialise status variable to closed

SharedPreferences preflogstat = getApplicationContext().getSharedPreferences("LogPref", MODE_PRIVATE);

SharedPreferences.Editor editor = preflogstat.edit();

editor.putString("logstatus","closed");

editor.commit();

Intent move=new Intent(LoginActivity.this,Accesscontrol.class);

startActivity(move);

  

}

});

}

}

Step 4: AccesscontrolActivity Code

package com.example.mycompany.accesscontrol;

import android.content.Intent;

import android.content.SharedPreferences;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class AccesscontrolActivity extends ActionBarActivity {

//Declare Variables

Button btn1;

Button btn2;

Button btn3;

Button btn4;

Button btnEnter;

String strpswd;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_accesscontrol);

//Assign controls from layout to variables

btn1 = (Button) findViewById(R.id.btn1) ;

btn2 = (Button) findViewById(R.id.btn2) ;

btn3 = (Button) findViewById(R.id.btn3) ;

btn4 = (Button) findViewById(R.id.btn4) ;

btnEnter = (Button) findViewById(R.id.btnEnter) ;

strpswd = "";

//Btn1 Click Event

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Add password value to the string variable

strpswd = strpswd + "1";

}

});

btn2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Add password value to the string variable

strpswd = strpswd + "2";

}

});

btn3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Add password value to the string variable

strpswd = strpswd + "3";

}

});

btn4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Add password value to the string variable

strpswd = strpswd + "4";

}

});

btnEnter.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

{

//Check or compare password value to the string variable

if(strpswd.equals("1234")){

//Make status of status variable to open

SharedPreferences preflogstat = getApplicationContext().getSharedPreferences("LogPref", MODE_PRIVATE);

SharedPreferences.Editor editor = preflogstat.edit();

editor.putString("logstatus","open");

editor.commit();

//Move to Login page

Intent move=new Intent(LoginActivity.this,Login.class);

startActivity(move);

}

else

{

//If password is incorrect, display error message

Toast.makeText(getApplicationContext(),"Incorrect Password!" ,Toast.LENGTH_LONG).show();

}

  

}

});

}

}