Q11
Question 11
Last updated
Question 11
Last updated
App Insights:
Used functional code of previous program with hard coded credentials, added a login activity on successful login, used DialogInterface and AlertDialog for logout functionality
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Login Application"
android:layout_marginTop="25dp"
android:textSize="25dp"
android:textColor="@color/black"
/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="220dp"
android:text="Username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/t1"
android:layout_marginLeft="50dp" />
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="250dp"
android:text="Password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/t2" />
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="200dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@id/t2"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="50dp" />
<EditText
android:id="@+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="230dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintLeft_toRightOf="@id/t3"
app:layout_constraintTop_toBottomOf="@id/t2" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="check"
android:text="Login"
android:layout_marginTop="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="50dp"
android:text="Login Successful"
android:textSize="25dp"
android:textColor="@color/black"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginStart="165dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="8dp"
android:onClick="onCL"
android:text="Logout"
android:layout_marginLeft="150dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.q11_advanced_login_app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Q11_Advanced_Login_App">
<activity android:name=".LoginActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:
name="android.intent.action.MAIN" />
<category android:
name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.q11_advanced_login_app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void check(View v) {
String txt = "Welcome ";
EditText ed1 = (EditText) findViewById(R.id.e1);
EditText ed2 = (EditText) findViewById(R.id.e2);
String a=ed1.getText().toString();
String b=ed2.getText().toString();
if(a.equals("Jatin") && b.equals("8647")) {
Intent intent=new Intent(this,
LoginActivity.class);
intent.putExtra("username",a);
startActivity(intent);
} else {
Toast.makeText(this,"Invalid User",
Toast.LENGTH_LONG).show();
}
}
}
package com.example.q11_advanced_login_app;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
textView=findViewById(R.id.textView2);
Intent f=getIntent();
String d=f.getStringExtra("username");
textView.setText("Hello "+d);
Toast.makeText(this,"Login Successful",
Toast.LENGTH_LONG).show();
}
public void onCL(View v) {
AlertDialog.Builder dbox =
new AlertDialog.Builder(this);
dbox.setTitle("Confirm Dialog Box");
dbox.setMessage("Do you want to log out?");
dbox.setPositiveButton("YES", (dialog, which) -> {
Intent i=new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
});
dbox.setNegativeButton("NO", (dialog, which) ->
Toast.makeText(getApplicationContext(),
"Application not exited" , Toast.LENGTH_SHORT).show());
dbox.show();
}
}
Download to Run and Test Locally