Android Simple Login App
So lets gets started with our basic Android Simple Login App:
- Open your Android Studio.
- Click on Start a New Android Studio Project.
- Write your Application Name “LoginApp” and leave other fields blank as it is, then click NEXT.
- Select the Minimum SDK “API 15: Android 4.0.3(IceCreamSandwich)”. I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features. After selecting your API click on NEXT.
- Select the blank activity and click NEXT.
- Write the activity name “Login” and leave everything as it is. Click Finish.
- After clicking Finish, it takes around 5 minutes to build your first activity, which is Login Activity
- .
- Now we will create another activity called “Main”. This activity will redirect user to “User screen” after he provides valid Login and password.
- To create a new activity go to app -> res -> layout. Right click on layout and go to new -> Activity -> Blank Activity.
- You will see the new activity screen.
- Write the name of your new activity as “Main” and leave everything as it is. Click Finish.
- Now we have two activities, which you can see in app -> res -> layout .
- activity_login.xml
- activity_main.xml
- Go to the activity_main.xml file. You can see the path of your user activity.
- activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"tools:context="com.example.nupur.loginapp.MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textSize="20dp"android:paddingLeft="10dp"android:textStyle="bold"android:text="Welcome to Formula/JEE Medical"android:id="@+id/textView"/></RelativeLayout>
- Go to AndroidManifest.xml file.
- We will start with designing our Login Activity for our Android Simple Login App. Go to app -> res -> layout -> activity_login.xml.
- Pick 5 large text elements, 1 person name element, 1 Button and 1 password element from palette. Arrange it as shown in the image below:
- Change the text and id as below:
- Text: Login Page, Id: textView_login
- Text: Username, Id: textView_username
- Text: Password, Id: textView_password
- Text: ” “, Id: editText_user
- Text: ” “, id: editText_password
- Text: Attempt, id: textView_attempt
- Text: ” “, Id: textView_attempt_count
- Text: “Login“, Id: button_login
- Define strings constant for all the elements in strings.xml file. You can find string.xml file in app -> res -> values ->strings.xml.
- Now we will design the second activity which is user activity. Go to app -> res ->layout -> activity_user.xml.
- Select large text element from the palette. Edit its text to “welcome to Formula/JEE Medical “.
- In this part we will see how to write the code behind login button. And how to validate the username and password.
- Go to the java file in which we will do code for the action performed by button.
- Go to app -> Java -> Com.abhinavhackpundit.loginapp ->login.
- The code or logic behind the action of app is shown below:
- login.java
- Click on Run. You can see your Android Login App running.
You will see your both activities. One is login activity which is also the launcher activity . Other is user activity. Here we set the Intent filter for our second activity which is also called as user activity.
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.nupur.loginapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Login"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"></activity> </application> </manifest>
strings.xml
<resources>
<string name="app_name">LoginApp</string>
</resources>
package com.example.nupur.loginapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class Login extends AppCompatActivity { private static EditText username; private static EditText password; private static TextView attempt; private static Button login_button; int attempt_counter=5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); username = (EditText)findViewById(R.id.editText_user); password = (EditText)findViewById(R.id.editText_password); attempt = (TextView)findViewById(R.id.textView_attempt); login_button = (Button)findViewById(R.id.button_login); LoginButton(); } public void LoginButton(){ attempt.setText(Integer.toString(attempt_counter)); login_button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (username.getText().toString().equals("admin")&& password.getText().toString().equals("pass123")){ Toast.makeText(Login.this, "Username and password is correct",Toast.LENGTH_SHORT).show(); //Here intent is used to goto Welcome page or start new activity i.e. MainActivity Intent intent = new Intent(Login.this,MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(Login.this,"Username and password is NOT correct", Toast.LENGTH_SHORT).show(); attempt_counter--; attempt.setText(Integer.toString(attempt_counter)); if(attempt_counter==0) login_button.setEnabled(false); } } } ); } }
Thank you for reading this article. If you face any problem or having any doubts, just let me know in comment box
Give us a graphical representation for better understanding
ReplyDelete