Login Screen Example

Login Screen Example in Android

MainActivity CLASS

package com.example.malik.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

EditText username, password;
TextView attempts;
Button login;
int attempt_counter = 5;

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

LoginButton();
}

private void LoginButton() {

username = (EditText) findViewById(R.id.etUserName);
password = (EditText) findViewById(R.id.etPassword);
attempts = (TextView) findViewById(R.id.tvAttempts);
login = (Button) findViewById(R.id.bLogin);
attempts.setText(Integer.toString(attempt_counter));

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals(“user”) && password.getText().toString().equals(“pass”)){
Toast.makeText(getApplicationContext(), “Username and password is correct”, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(“com.example.malik.test.User”);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), “Username and password is not correct”, Toast.LENGTH_SHORT).show();
attempt_counter–;
attempts.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0){
login.setEnabled(false);
}
}
}
});
}
}

 

MainActivity XML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”@string/login_head”
android:id=”@+id/textView”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”@string/username”
android:id=”@+id/textView2″
android:layout_below=”@+id/textView”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”36dp” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”@string/password”
android:id=”@+id/textView3″
android:layout_below=”@+id/textView2″
android:layout_alignRight=”@+id/textView2″
android:layout_alignEnd=”@+id/textView2″
android:layout_marginTop=”30dp” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:inputType=”textPersonName”
android:ems=”10″
android:id=”@+id/etUserName”
android:layout_alignTop=”@+id/textView2″
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/etPassword”
android:layout_alignBottom=”@+id/textView3″
android:layout_alignLeft=”@+id/etUserName”
android:layout_alignStart=”@+id/etUserName”
android:layout_alignRight=”@+id/etUserName”
android:layout_alignEnd=”@+id/etUserName” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”@string/attemptes”
android:id=”@+id/textView4″
android:layout_centerVertical=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:id=”@+id/tvAttempts”
android:layout_alignTop=”@+id/textView4″
android:layout_alignLeft=”@+id/etPassword”
android:layout_alignStart=”@+id/etPassword”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Login”
android:id=”@+id/bLogin”
android:layout_below=”@+id/textView4″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”60dp” />

</RelativeLayout>

 

User CLASS

package com.example.malik.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class User extends AppCompatActivity {

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

 

User XML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.User”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”Welcome to the screen !!!”
android:id=”@+id/textView5″
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”65dp” />
</RelativeLayout>

 

Manifest FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.malik.test”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.User”>
<intent-filter>
<action android:name=”com.example.malik.test.User” />

<category android:name=”android.intent.category.DEFAULT” />
</intent-filter></activity>
</application>

</manifest>

 

OutPut :

pic1 pic2 pic3

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.