Login Using Android
In this tutorial you will learn to do login using Android studio.In this tutorial back end is developed using PHP and database is Mysql.In this tutorial volley is used as library. Lets check the code step by step.
First Lets check the .XML code ,which we also discussed in the tutorial of "Simple Login App Design Using Android Studio".
The code is very simple ,here we are using Constrain layout and Relative layout. The code is as follows:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="219dp"
android:layout_marginStart="141dp"
android:layout_marginLeft="141dp"
android:layout_marginEnd="141dp"
android:layout_marginRight="141dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/mainlogo"
tools:layout_editor_absoluteY="2dp" />
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="220dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView4"
android:layout_width="72dp"
android:layout_height="64dp"
app:srcCompat="@drawable/user" />
<EditText
android:id="@+id/editText3"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView4"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:paddingTop="22dp" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="70dp"
android:paddingTop="10dp"
android:layout_below="@id/imageView4"
android:layout_height="59dp"
app:srcCompat="@drawable/pass" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText3"
android:paddingTop="30dp"
android:layout_toRightOf="@id/imageView5"
android:hint="Enter Password"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText4"
android:layout_centerInParent="true"
android:text="Log In" />
</RelativeLayout>
As the above code you can see that the id of the edittext is editText3 and editText4.Also the id for the button is button2.Lets check the code of the mainactivity:
package com.example.login; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText t1, t2; Button b1; TextView t3; String un, pa; String url = "http://192.168.43.99/Android/login.php";//this is the url of the php code @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_login); t1=(EditText) findViewById(R.id.editText3); t2=(EditText) findViewById(R.id.editText4); b1=(Button) findViewById(R.id.button2); b1.setOnClickListener(this); } @Override public void onClick(View v) { final String uname=t1.getText().toString(); final String pass=t2.getText().toString(); RequestQueue queue = Volley.newRequestQueue(MainActivity.this); StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if(response.equalsIgnoreCase("Logged IN")) { Intent intentBundle=new Intent(MainActivity.this,dashboard.class); Bundle bundle=new Bundle(); bundle.putString("user",uname); intentBundle.putExtra("data",bundle); startActivity(intentBundle); } else { Toast.makeText(MainActivity.this, "dfdsfsd" + response, Toast.LENGTH_SHORT).show(); Log.i("My success", "" + response); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "my error :" + error, Toast.LENGTH_LONG).show(); Log.i("My error", "" + error); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("username",uname); map.put("password",pass); return map; } }; queue.add(request); } }
For the connection with mysql here we are using the database "youtube".In the said database a table is designed named registration.Please create a database named "youtube" and a table named "registration".The structure of the table is as follows:-
Column Name | Type |
---|---|
rid | int(11) |
name | varchar(100) |
fathersname | varchar(100) |
mothersname | varchar(100) |
address | varchar(200) |
varchar(200) | |
uname | varchar(200),Primary Key |
password | varchar(200) |
Now lets check the php code for the login(In the URl the name of the php code is mentioned)
<?php
$con=mysqli_connect("localhost","root","","youtube");
$uname=$_POST["username"];
$passwd=$_POST["password"];
$q="SELECT count(*) FROM `registration` WHERE uname='$uname' and password='$passwd'";
$r1=mysqli_query($con,$q);
$r2=mysqli_fetch_row($r1);
if($r2[0]==1)
{
echo "Logged IN";
}
else
{
echo "Error";
}
?>
if log in is successful than we are sending back "Logged IN" as response from the php file.
Now check the android code ,where you will get a highlighted line of code ,in that code we
are checking the response from the php code.Enjoy the coding.
Comments
Post a Comment