How to Create Simple Login Form in Android Studio Without using Database?
if you guyz are just the beginners in android programming and you don’t know how to work with android studio and how to design the forms in android and also how to define, initialize variables in android studio then this tutorial is for you guyz.
Let’s start.
STEPS:
[sociallocker]
1. First of all you need to have android studio in your computer, if not download and install it from here: CLICK HERE and then goto Tools>Andorid>SDK Manager and install all SDK Platforms and SDK Tools and then click shift+f10 new window will appear after then click on create new virtual device and then select phone from the left pane and then choose any name you want like nexus 5 and then click next choose any android version in the next window and the click next and then click finish; this will create the virtual device in your android studio so that you can run your apps or you can run the apps in your android phone too. For this you need to install the phone specific driver in your PC, and then go to the setting of your phone and enable usb debugging mode, and then connect your phone with PC through the help of USB cable then you will be able to run app in your phone. Or you can install third party AVD in your Computer to run your apps like genymotion. i explain the process of running the apps because in other programming langue you do not to take any thing simply click on the play button and your program will start to execute but in android studio we are create apps for mobile devices so we need mobile device to run the apps or virtual mobile device to run it. i think you may understand this.
2. Open Android Studio, go to file>new>new project; give application name click next, select phone and tablets;click next, select empty activity click next and finally click finish.
Now you project will open in android studio. Go to res>layout>activity_main.xml. Double click on activity_mail.xml file, select design section. And drag and drop two edittext and one button and change their properties as shown in the video. OR simply copy following code; keep in mind that you need to change the package name and application name.
Designing code:
<?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:id="@+id/activity_main" 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="ran.com.loginformsimple.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:layout_marginTop="23dp" android:id="@+id/txtuser" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:hint="Username" android:textSize="20sp" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:layout_marginTop="22dp" android:id="@+id/txtpass" android:hint="Password" android:textSize="20sp" android:layout_below="@+id/txtuser" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:text="Login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:id="@+id/btnlogin" android:textSize="20sp" android:layout_below="@+id/txtpass" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
3. Now goto java>yourpackagename.com.applicationname in my case java>ran.com.loginformsimple>MainActivity
a. Inside the class declare the following variable as:
Button login; EditText username, password;
b. Inside the onCreate method initialize each control as:
login=(Button)findViewById(R.id.btnlogin); username=(EditText)findViewById(R.id.txtuser); password=(EditText)findViewById(R.id.txtpass);
c. Now you have two option: either you can write the following piece of code just by creating the listening event of button or you can create one method inside the class but outside the onCreate method and call this method inside the listening event of the button.
So, i am gonna use the second one. but if you want to use the first method, you only need to create the listening event of the button and the copy the following code removing the method i.e. exclude public void login(){}
d. Create one method and write the following code inside that method:
public void login(){ String user=username.getText().toString().trim(); String pass=password.getText().toString().trim(); if(user.equals("techsupportnep")&amp;&amp; pass.equals("techsupportnep")) { Toast.makeText(this,"username and password matched!",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"username and password do not matched!",Toast.LENGTH_LONG).show(); } }
e. Now go inside the onCreate method and create the listening event of button and call this method as just below the initialization of editText and Button:
login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } });
f. Press shift+f10 or simply click in the play button below the menu of the android studio and t
[/sociallocker]
hen select your AVD either builtin AVD , or third party AVD or your phone device and then click proceed.
That’s it if you have any confusion you can watch the following video: