How To Create Registration Form In Android
Create login and registration form in android studio code, today we are creating a login page in android studio, android login using a retrofit.
If you have no idea how can we design a login and registration form. read this complete article, I explain for some example for creating login and registration form.
What You can learn In this Article?
- You can learn how to design login and registrationform using colors and drawable file.
- How to work retrofit library for web service in android studio.
Login and registration form in android
In this project we will learn how to create a login and registration form in an android studio code using a retrofit library.you can see below step for this project I create come colors and drawable resource files for background design.now we will create a android project and follow this step one by one.
Step for Android Project
- Create an android project and add a retrofit library.
- Define colors and drawable resource files.
- Create a login page design with UI.
- Desgning registration form.
- Create a registration method using a retrofit.
- Login method using a PHP script and retrofit.when user login successfully open user profile with name and email.
How to create a login registration page in the android studio?
Step (1) :Create an android project and add a retrofit library.
- Open Android Studio And Create a new Project, I Created My Project name LoginAndRegistration.
- In this project we will use the Mysql database, so we need to add a retrofit library for working with PHP script.
- Now open app:Gradle file and add retrofit library
| dependencies { implementation fileTree ( dir : 'libs' , include : [ '*.jar' ] ) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' } |
Step (2) :Define colors and drawable resource files.
- We want to adding a new colors in colors.xml file.
- Open value>colors.xml file and adding new colors.
| <? xml version = "1.0" encoding = "utf-8" ?> < resources > < color name = "colorPrimary" > #6200EE</color> < color name = "colorPrimaryDark" > #3700B3</color> < color name = "colorAccent" > #03DAC5</color> < color name = "colorWhite" > #FFFFFF</color> < / resources > |
Create background shape login and registration form.
- Create a new drawable resource file, add shape, solid and corners.
- Open res folder drawable>new>drawable resource file, create a new file.
- You can see bellow code for header and bottom design for login and registration.
header_bg.xml file code
| <? xml version = "1.0" encoding = "utf-8" ?> < shape xmlns : android = "http://schemas.android.com/apk/res/android" android : shape = "rectangle" > < solid android : color = "@color/colorPrimaryDark" / > < corners android : bottomLeftRadius = "200dp" / > < / shape > |
bottom_bg.xml file code
| <? xml version = "1.0" encoding = "utf-8" ?> < shape xmlns : android = "http://schemas.android.com/apk/res/android" android : shape = "rectangle" > < solid android : color = "@color/colorPrimaryDark" / > < corners android : topRightRadius = "200dp" / > < / shape > |
Step (3) :Create a login page design with UI.
- Open activity_main.xml file,In this step we will create a new layout file and include in activity_main.xml file
- In the res>layout>layout resorce file,create a new layout and name it loginform.xml.
- In this loginform we want to adding EditText and Button,you can see below code.
loginform.xml file code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <? xml version = "1.0" encoding = "utf-8" ?> < androidx . cardview . widget . CardView xmlns : android = "http://schemas.android.com/apk/res/android" android : layout_width = "match_parent" android : layout_height = "wrap_content" > < LinearLayout android : layout_width = "match_parent" android : layout_height = "300dp" android : gravity = "center" android : orientation = "vertical" > < EditText android : id = "@+id/etemail" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : ems = "10" android : hint = "Email" android : inputType = "textEmailAddress" / > < EditText android : id = "@+id/mypass" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : ems = "10" android : hint = "Password" android : inputType = "textPassword" / > < Button android : id = "@+id/btnlogin" android : textAllCaps = "false" android : layout_marginTop = "10dp" android : textColor = "@color/colorWhite" android : background = "@drawable/botton_shape" android : text = "Log In" android : layout_width = "wrap_content" android : layout_height = "wrap_content" / > < TextView android : id = "@+id/createnewac" android : layout_marginTop = "10dp" android : textSize = "20sp" android : textStyle = "bold" android : textColor = "@color/colorPrimaryDark" android : layout_gravity = "center" android : text = "Create a new Acount!" android : layout_width = "wrap_content" android : layout_height = "wrap_content" / > < / LinearLayout > < / androidx . cardview . widget . CardView > |
Now,This loginform.xml file include in activity_main.xml file.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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" > < View android : id = "@+id/header_view" android : layout_width = "match_parent" android : layout_height = "200dp" android : background = "@drawable/header_bg" > < / View > < TextView android : layout_marginTop = "35dp" android : textColor = "@color/colorWhite" android : textSize = "25sp" android : textAlignment = "center" android : text = "Unique Andro Code" android : textStyle = "bold" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : gravity = "center_horizontal" / > < LinearLayout android : layout_width = "match_parent" android : layout_height = "wrap_content" android : layout_below = "@+id/header_view" android : orientation = "vertical" > < include layout = "@layout/loginform" / > < / LinearLayout > < LinearLayout android : gravity = "bottom" android : layout_width = "match_parent" android : layout_height = "match_parent" > < View android : layout_width = "match_parent" android : layout_height = "200dp" android : background = "@drawable/bottom_bg" / > < / LinearLayout > < / RelativeLayout > |
Run this Project,you can see the below result.
Step (4) :Desgning registration form in android code.
- Create a new empty Activity, name it SignUp Activity.
- We need to one another layout for Registration Design.
- Right-click on layout and create a new signupform.xml file.
signupform.xml file code.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <? 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" > < EditText android : id = "@+id/editName" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "150dp" android : ems = "10" android : hint = "Name" android : inputType = "textPersonName" app : layout_constraintBottom_toTopOf = "@+id/editEmail" app : layout_constraintEnd_toEndOf = "parent" app : layout_constraintStart_toStartOf = "parent" app : layout_constraintTop_toTopOf = "parent" app : layout_constraintVertical_bias = "0.13" / > < EditText android : id = "@+id/editEmail" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : ems = "10" android : hint = "Email" android : inputType = "textEmailAddress" app : layout_constraintBottom_toTopOf = "@+id/editPass" app : layout_constraintEnd_toEndOf = "parent" app : layout_constraintStart_toStartOf = "parent" app : layout_constraintTop_toBottomOf = "@+id/editName" / > < EditText android : id = "@+id/editPass" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : ems = "10" android : hint = "Password" android : inputType = "textPassword" app : layout_constraintBottom_toTopOf = "@+id/buttonAcount" app : layout_constraintEnd_toEndOf = "parent" app : layout_constraintStart_toStartOf = "parent" app : layout_constraintTop_toBottomOf = "@+id/editEmail" / > < Button android : id = "@+id/buttonAcount" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginBottom = "300dp" android : background = "@drawable/botton_shape" android : text = "Sign Up" android : textAllCaps = "false" android : textColor = "@color/colorWhite" app : layout_constraintBottom_toBottomOf = "parent" app : layout_constraintEnd_toEndOf = "parent" app : layout_constraintHorizontal_bias = "0.498" app : layout_constraintStart_toStartOf = "parent" app : layout_constraintTop_toBottomOf = "@+id/editPass" / > < TextView android : id = "@+id/textView" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "16dp" android : layout_marginBottom = "13dp" android : text = "Create a New Acount" android : textColor = "#303F9F" android : textSize = "25sp" android : textStyle = "bold" app : layout_constraintBottom_toTopOf = "@+id/editName" app : layout_constraintEnd_toEndOf = "parent" app : layout_constraintStart_toStartOf = "parent" app : layout_constraintTop_toTopOf = "parent" / > < / androidx . constraintlayout . widget . ConstraintLayout > |
Now we will use this xml file in SignUp Activity,you can see below code.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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 = ".SignUpActivity" > < View android : id = "@+id/header_view" android : layout_width = "match_parent" android : layout_height = "200dp" android : background = "@drawable/background" / > < LinearLayout android : layout_marginTop = "10dp" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : layout_below = "@+id/header_view" android : orientation = "vertical" > < include layout = "@layout/signup_form" / > < / LinearLayout > < LinearLayout android : gravity = "bottom" android : layout_width = "match_parent" android : layout_height = "match_parent" > < View android : layout_width = "match_parent" android : layout_height = "200dp" android : background = "@drawable/bottom_bg" / > < / LinearLayout > < / RelativeLayout > |
- Okay, our Login and Registration form is complete, now we will Create a retrofit class for login and registration method.
Step (5):Create a registration method using a retrofit.
- Under the Main Java package, create a new package and name it api.
- Create Model class.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com . uniqueandrocode . loginandregistration . model ; import com . google . gson . annotations . SerializedName ; public class MyUserData { @ SerializedName ( "name" ) public String name ; @ SerializedName ( "email" ) public String email ; public MyUserData ( String name , String email ) { this . name = name ; this . email = email ; } public String getName ( ) { return name ; } public String getEmail ( ) { return email ; } @ Override public String toString ( ) { return "MyUserData{" + "name='" + name + '\'' + ", email='" + email + '\'' + '}' ; } } |
- Create 2 new java classes under api package.
MyApi.java code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com . uniqueandrocode . loginandregistration . api ; import com . uniqueandrocode . loginandregistration . model . MyUserData ; import com . uniqueandrocode . loginandregistration . model . UserData ; import okhttp3 . ResponseBody ; import retrofit2 . Call ; import retrofit2 . http . Field ; import retrofit2 . http . FormUrlEncoded ; import retrofit2 . http . POST ; public interface MyApi { @ FormUrlEncoded @ POST ( "signup.php" ) Call < ResponseBody > createNewAcount ( @ Field ( "name" ) String username , @ Field ( "email" ) String email , @ Field ( "password" ) String pass ) ; @ FormUrlEncoded @ POST ( "login.php" ) Call < MyUserData > logIn ( @ Field ( "email" ) String email , @ Field ( "password" ) String pass ) ; } |
now our Retrofit class is complate and we create registraion method in signUp Activity.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package com . uniqueandrocode . loginandregistration ; import androidx . appcompat . app . AppCompatActivity ; import android . os . Bundle ; import android . text . TextUtils ; import android . view . View ; import android . widget . Button ; import android . widget . EditText ; import android . widget . Toast ; import com . uniqueandrocode . loginandregistration . api . MyRetrofit ; import java . io . IOException ; import okhttp3 . ResponseBody ; import retrofit2 . Call ; import retrofit2 . Callback ; import retrofit2 . Response ; public class SignUpActivity extends AppCompatActivity { EditText etname , etemail , etpass ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_sign_up ) ; etname = findViewById ( R . id . editName ) ; etemail = findViewById ( R . id . editEmail ) ; etpass = findViewById ( R . id . editPass ) ; Button btn_new = findViewById ( R . id . buttonAcount ) ; final String name = etname . getText ( ) . toString ( ) . trim ( ) ; final String email = etemail . getText ( ) . toString ( ) . trim ( ) ; final String pass = etpass . getText ( ) . toString ( ) . trim ( ) ; btn_new . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { createNewAcount ( name , email , pass ) ; } } ) ; } private void createNewAcount ( String name , String email , String pass ) { if ( TextUtils . isEmpty ( name ) ) { etname . setError ( "Please Enter Name" ) ; etname . requestFocus ( ) ; return ; } if ( TextUtils . isEmpty ( email ) ) { etemail . setError ( "Please Enter Email" ) ; etemail . requestFocus ( ) ; return ; } if ( TextUtils . isEmpty ( pass ) ) { etpass . setError ( "Please Enter Password" ) ; etpass . requestFocus ( ) ; return ; } Call < ResponseBody > call = MyRetrofit . getInstance ( ) . getMyApi ( ) . createNewAcount ( name , email , pass ) ; call . enqueue ( new Callback < ResponseBody > ( ) { @ Override public void onResponse ( Call < ResponseBody > call , Response < ResponseBody > response ) { try { String hi = response . body ( ) . string ( ) ; Toast . makeText ( getApplicationContext ( ) , hi , Toast . LENGTH_LONG ) . show ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } @ Override public void onFailure ( Call < ResponseBody > call , Throwable t ) { } } ) ; } } |
Dont forgate INTERNET permition in manifests.xml file.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns : android = "http://schemas.android.com/apk/res/android" package = "com.uniqueandrocode.loginandregistration" > < uses - permission android : name = "android.permission.INTERNET" / > < application android : allowBackup = "true" android : icon = "@mipmap/ic_launcher" android : label = "@string/app_name" android : roundIcon = "@mipmap/ic_launcher_round" android : supportsRtl = "true" android : theme = "@style/AppTheme" > < activity android : name = ".ProfileActivity" > < / activity > < activity android : name = ".SignUpActivity" / > < activity android : name = ".MainActivity" > < intent - filter > < action android : name = "android.intent.action.MAIN" / > < category android : name = "android.intent.category.LAUNCHER" / > < / intent - filter > < / activity > < / application > < / manifest > |
- Now run this Project and test signup method.
Step (6):Login method using PHP script and retrofit.
- Open MainActivity.java file and write the following code.
- In this MainActivity we will learn how to create a login method using Retrofit.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | package com . uniqueandrocode . loginandregistration ; import androidx . appcompat . app . AppCompatActivity ; import android . content . Intent ; import android . os . Bundle ; import android . text . TextUtils ; import android . view . View ; import android . widget . Button ; import android . widget . EditText ; import android . widget . TextView ; import android . widget . Toast ; import com . uniqueandrocode . loginandregistration . api . MyRetrofit ; import com . uniqueandrocode . loginandregistration . model . MyUserData ; import com . uniqueandrocode . loginandregistration . model . UserData ; import java . io . IOException ; import okhttp3 . ResponseBody ; import retrofit2 . Call ; import retrofit2 . Callback ; import retrofit2 . Response ; public class MainActivity extends AppCompatActivity { EditText etemail , etpass ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; TextView createnewac = findViewById ( R . id . createnewac ) ; etemail = findViewById ( R . id . etemail ) ; etpass = findViewById ( R . id . mypass ) ; // final String email=etemail.getText().toString().trim(); //final String pass=etpass.getText().toString().trim(); Button btnlogin = findViewById ( R . id . btnlogin ) ; btnlogin . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { logIn ( ) ; } } ) ; createnewac . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { startActivity ( new Intent ( MainActivity . this , SignUpActivity . class ) ) ; } } ) ; } private void logIn ( ) { String email = etemail . getText ( ) . toString ( ) . trim ( ) ; String pass = etpass . getText ( ) . toString ( ) ; if ( TextUtils . isEmpty ( email ) ) { etemail . setError ( "Please Enter Email" ) ; etemail . requestFocus ( ) ; return ; } if ( TextUtils . isEmpty ( pass ) ) { etpass . setError ( "Please Enter Password" ) ; etpass . requestFocus ( ) ; return ; } Call < MyUserData > call = MyRetrofit . getInstance ( ) . getMyApi ( ) . logIn ( email , pass ) ; call . enqueue ( new Callback < MyUserData > ( ) { @ Override public void onResponse ( Call < MyUserData > call , Response < MyUserData > response ) { String myname = response . body ( ) . getName ( ) ; String email = response . body ( ) . getEmail ( ) ; Intent intent = new Intent ( MainActivity . this , ProfileActivity . class ) ; intent . putExtra ( "name" , myname ) ; intent . putExtra ( "email" , email ) ; startActivity ( intent ) ; } @ Override public void onFailure ( Call < MyUserData > call , Throwable t ) { } } ) ; } } |
- We want to when users login succsesfully,open User ProfileActivty,so we are create a new empty Activity,name this ProfileActivty.
- We will display users name and Email In this profile activity.
activity_profile.xml file code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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 = ".ProfileActivity" > < View android : layout_width = "match_parent" android : layout_height = "300dp" android : background = "@drawable/background" / > < LinearLayout android : layout_marginTop = "200dp" android : layout_centerHorizontal = "true" android : layout_width = "match_parent" android : layout_height = "300dp" android : orientation = "vertical" android : gravity = "center" > < TextView android : textStyle = "bold" android : textAlignment = "center" android : textColor = "#111" android : textSize = "25sp" android : id = "@+id/username" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : gravity = "center_horizontal" / > < TextView android : textAlignment = "center" android : textColor = "#453E3E" android : textStyle = "bold" android : textSize = "15sp" android : layout_marginTop = "15dp" android : id = "@+id/useremail" android : layout_width = "match_parent" android : layout_height = "wrap_content" android : gravity = "center_horizontal" / > < / LinearLayout > < LinearLayout android : gravity = "bottom" android : layout_width = "match_parent" android : layout_height = "match_parent" > < View android : layout_width = "match_parent" android : layout_height = "200dp" android : background = "@drawable/bottom_bg" / > < / LinearLayout > < / RelativeLayout > |
- After login pass name and email to ProfileActivity using Intent.
- Write the code for display Name and Email.
- Run This Project, You can get bellow results.
- This result you can see how it work and display login and registration form in android login using retrofit.
- If you like this article Please share it.
Download project source code
Android tutorial for beginners
- Android login and registration in android studio using volley.
- Shared Preference Example in Android Login Logout
- Carousel slider in android using retrofit.
- Simple Recyclerview Android Example.
- Android Custom Notification Layout.
Thank You for reading login and registration form in android studio code
How To Create Registration Form In Android
Source: https://uniqueandrocode.com/login-and-registration-form-in-android-studio-code/
Posted by: williamswasioneating.blogspot.com

0 Response to "How To Create Registration Form In Android"
Post a Comment