
Android is one of the most loved smartphone manufacturers. Android is also a common language used to develop android apps.
Are you a beginner in android? Do you want to kick-start your project in android studio? Here is a tutorial for you. In this tutorial, we will learn how to set up the emulator, how to design the simple form, and take data from the user and store it in a database. We will store the data in MYSQL. Let’s begin the tutorial.
Open the android studio. If you have not downloaded android studio then here is the tutorial for you from my previous post. As soon as you open the android studio, you will see the following welcoming screen.

Click on Start a new Android Studio project.

Now give a suitable name to the application you are developing and click on Next.

Now you have to select the form factors i.e. for which platform you want to develop the application and the minimum SDK for your phone.

Then, you can choose an activity to your application. There are many activities available such as Map activity, Bottom navigation activity, login activity and so on. It is best for beginners to select empty activity as they get to learn more.

Give a name to your activity. I have given the activity name as MainActivity and the layout name is set to activity_main.
Then you will see the following screen. Find res and then drop down the options of layout to find out activity_name.

Click on text as given above if it is clicked on Design.
Then type the following xml code.
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".RegisterActivity" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:background="@drawable/bg"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:drawableStart="@drawable/ic_person_white_24dp" android:ems="10" android:hint="@string/fullname" android:inputType="textPersonName" android:drawableLeft="@drawable/ic_person_white_24dp" /> <EditText android:id="@+id/etEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:drawablePadding="10dp" android:drawableStart="@drawable/outline_email_24" android:ems="10" android:hint="@string/e_mail" android:inputType="textEmailAddress" android:drawableLeft="@drawable/outline_email_24" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="120dp" android:drawablePadding="10dp" android:drawableStart="@drawable/ic_lock_white_24dp" android:ems="10" android:hint="@string/password" android:inputType="textPassword" android:drawableLeft="@drawable/ic_lock_white_24dp" /> <EditText android:id="@+id/etAge" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="180dp" android:ems="10" android:hint="@string/age" android:inputType="number" /> <EditText android:id="@+id/etPhone" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="+0123456789" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="240dp" android:drawableStart="@drawable/outline_phone_24" android:drawablePadding="10dp" android:ems="10" android:hint="@string/phone" android:inputType="phone" android:drawableLeft="@drawable/outline_phone_24" /> <TextView android:id="@+id/tvGender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignStart="@+id/etPhone" android:layout_marginTop="300dp" android:text="@string/gender" android:textSize="18sp" android:layout_alignLeft="@+id/etPhone" /> <RadioGroup android:id="@+id/radioSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvGender" android:layout_toEndOf="@+id/tvGender" android:layout_toRightOf="@+id/tvGender"> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" android:onClick="onRadioButtonClicked"/> <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:onClick="onRadioButtonClicked" android:text="@string/male" /> </RadioGroup> <Button android:id="@+id/btnRegister" style="@style/Base.TextAppearance.AppCompat.Body1" android:layout_width="270dp" android:layout_height="72dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="423dp" android:background="@drawable/btn" android:clickable="true" android:focusable="true" android:padding="16dp" android:text="@string/register" android:textAllCaps="false" android:textColor="#f16f7d" android:textSize="20sp" /> </RelativeLayout> </ScrollView>
Here scrollview is for enabling the scrolling ability in the screen.
The result will be like this:

Then go to the Main Activity which is in a drop down option in Java as shown in the picture above and type the following code. There are some instructions in the code given below . Don’t directly copy and paste. First, read it well.
package com.google.mysql.health;(Package name differs) import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; public class RegisterActivity extends AppCompatActivity { Button btn_reg; EditText username, email, password, age, phone; String gender; AlertDialog.Builder builder; String reg_url="http://10.10.11.205(IP address of your laptop)/officeapplication/register.php(this is a php code for registration, I will come to it soon)"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_register ); btn_reg = findViewById( R.id.btnRegister ); username = findViewById( R.id.etUsername ); email = findViewById( R.id.etEmail ); password = findViewById( R.id.etPassword ); age = findViewById( R.id.etAge ); phone = findViewById( R.id.etPhone ); btn_reg.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { final String str_username = username.getText().toString(); final String str_email = email.getText().toString(); final String str_password = password.getText().toString(); final String str_age = age.getText().toString(); final String str_phone = phone.getText().toString(); final String str_gender = gender; builder= new AlertDialog.Builder( RegisterActivity.this ); if (str_username.equals( "" )||str_email.equals( "" )||str_password.equals( "" )||str_age.equals( "" )||str_phone.equals( "" )|| str_phone.length()!=10) { builder.setTitle( "Something went wrong........." ); builder.setMessage( "Please fill all the fields & phone number should be of Nepal i.e 10 numbers." ); displayAlert("input_error"); } else { StringRequest stringRequest = new StringRequest( Request.Method.POST, reg_url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONArray jsonarray = new JSONArray(response); JSONObject jsonObject= jsonarray.getJSONObject( 0 ); String code=jsonObject.getString( "code" ); String message = jsonObject.getString( "message" ); builder.setTitle( "Server Response..." ); builder.setMessage(message); displayAlert(code); /* Intent intent = new Intent (RegisterActivity.this, login.class); startActivity(intent); finish();*/ } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(RegisterActivity.this,"Error",Toast.LENGTH_LONG).show(); error.printStackTrace(); } } ) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put( "Name", str_username.trim()); params.put( "Email", str_email.trim()); params.put( "Password", str_password.trim()); params.put( "Age", str_age.trim()); params.put( "Phone", str_phone.trim()); params.put( "Gender", gender); return params; } }; MySingleton.getInstance( RegisterActivity.this ).addToRequestque(stringRequest); } } }); } public void onRadioButtonClicked(View view) { //Is the button now Checked? boolean checked = ((RadioButton) view).isChecked(); //Check which radio button was clicked switch (view.getId()) { case R.id.radioMale: if (checked) gender = "Male"; break; case R.id.radioFemale: if (checked) gender = "Female"; break; } } public void displayAlert(final String code) { builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(code.equals( "input_error" )) { password.setText(""); } else if (code.equals ("reg_success")) { finish(); } else if (code.equals ("reg_failed")) { username.setText(""); email.setText(""); password.setText(""); age.setText(""); phone.setText(""); } } } ); AlertDialog alertDialog=builder.create(); alertDialog.show(); } } Now,you have to connect android with the database. We are using MYSQL database for storing the registered user/signed in user.For this purpose we need to install Xampp. As, we don't have a real server to store and manipulate the data,we are storing the data locally. After you have downloaded xampp, you need to setup few things as shown in the figure below:
You need to start Apache and MySQL. If your MYSQL donot start then remenber that the port number that is to be assigned to MYSQL is used by some other software. Open Task Manager to stop the program which is using the same port number as that of MYSQL which is mostly 3306.
Now open notepad++ or sublime text, whichever you have and write the following code for registration of the users and storing data entered by the user in your app.
<?php
include(‘init.php’);
if (isset($_POST[‘Name’],$_POST[“Email”],$_POST[“Password”],$_POST[“Age”],$_POST[“Phone”], $_POST[“Gender”]))
{
$Name = $_POST[“Name”];
$Email = $_POST[“Email”];
$Password= $_POST[“Password”];
$Age = $_POST[“Age”];
$Phone = $_POST[“Phone”];
$Gender = $_POST[“Gender”];$sql = ” select * from user_info where Email = ‘$Email’ “;
$result = mysqli_query($con,$sql);
$response = array();if(mysqli_num_rows($result)>0)
{
$code = “reg_failed”;
$message = “User already exist”;
array_push($response,array(“code”=>$code, “message”=>$message));
echo json_encode($response);
}
else
{
$sql = “insert into user_info(Name, Email, Password, Age, Phone, Gender) values (‘$Name’, ‘$Email’,’$Password’,’$Age’,’$Phone’,’$Gender’)”;mysqli_query($con,$sql);
$code = “reg_success”;
$message = “Thankyou for the registration. Now you can login….”;
array_push($response,array(“code”=>$code, “message”=>$message));
echo json_encode($response);}
}
mysqli_close($con)
?>Then go to the drive where you have saved the xampp software. I have saved it in C Drive. So go to C>xampp>htdocs. In htdocs, create new folder named officeapplication and inside the folder save the above php file which you have just written.
In your browser, type localhost/dashboard and a page like this will appear.

Click on phpmyAdmin which is highlighted yellow in the picture above.
A page like this will then appear now.

Generally the default username is root and the password is a space unless set it to something else. So, go ahead, type the username and root and click on GO.
Now as soon as the new window open, you need to create a new database say user_db and create a table, say user_info ,consisting of the columns which are information asked in the sign up form that you have in your app.
See the picture below for the assistance.

Now your app is ready to run. you can either connect an android mobile and run the app or you can run it in emulator. Running in mobile by connecting it to the laptop is much more faster and easier.

When you will enter the information and hit on register. You will see the information entered by you in the table you just made.

Therefore, in this way the Sign up form is made in android studio and the data entered by the user is stored in MySQL as shown above.
If you have any views regarding this post, do comment below so that we can improve and we can also help you with the problems you are facing.
Also, please subscribe to Webtech YouTube Channel for more important updates and stay tuned!