About Example:- Here is the example that "How to do SignUp | Login and Update user's Profile in Android Application By using Coding"
Basic Requirement:-
1. MainActivity.java | activity_main.xml
2.Login | login.xml
3.Signup | signup.xml
4.UserProfile | user_profile.xml
5.UserProfileUpdate | user_profile_update.xml
6.SharedConstant
Steps are:-
Step 1: Add a new project and open activity_main.xml and write following code:-
Basic Requirement:-
1. MainActivity.java | activity_main.xml
2.Login | login.xml
3.Signup | signup.xml
4.UserProfile | user_profile.xml
5.UserProfileUpdate | user_profile_update.xml
6.SharedConstant
Steps are:-
Step 1: Add a new project and open activity_main.xml and write following code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:id="@+id/uerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:text="Welcome"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:id="@+id/uerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:text="Welcome"/>
</LinearLayout>
Steps 2: MainActivity.java
package com.example.supriyabharti.profilesettingexmp; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { SharedConstant sharedConstant; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedConstant=new SharedConstant(MainActivity.this); new Handler().postDelayed(new Runnable(){ public void run(){ String strUserPhone=sharedConstant.getStringPref("userPhone"); if(strUserPhone.matches("")){ startActivity(new Intent(MainActivity.this,SignUp.class)); finish(); }else { startActivity(new Intent(MainActivity.this, UserProfile.class)); finish(); } } },3000 ); } }
Steps 3: SharedConstant.javaSteps 4:- SignUp.java
package com.example.supriyabharti.profilesettingexmp; import android.content.Context; import android.content.SharedPreferences; public class SharedConstant { SharedPreferences preferences; SharedPreferences.Editor editor; public SharedConstant(Context context) { preferences=context.getSharedPreferences("MyData", Context.MODE_PRIVATE); } public void setPrefString(String key,String value){ editor=preferences.edit(); editor.putString(key,value); editor.commit(); } public String getStringPref(String key){ return preferences.getString(key,""); } }
package com.example.supriyabharti.profilesettingexmp; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; /** * Created by supriyabharti on 05/09/17. */ public class SignUp extends AppCompatActivity { TextView tvLogin; EditText edName,edPhone; Button btnSignup; @OverrideLoginprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.signup); final SharedConstant pref=new SharedConstant(SignUp.this); tvLogin=(TextView)findViewById(R.id.login); edName=(EditText)findViewById(R.id.userName); edPhone=(EditText)findViewById(R.id.userPhone); btnSignup=(Button) findViewById(R.id.signUp); btnSignup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(edName.getText().toString().isEmpty() && edPhone.getText().toString().isEmpty()){ Toast.makeText(getApplicationContext(),"Please enter Value",Toast.LENGTH_LONG).show(); }else{ pref.setPrefString("userName",edName.getText().toString()); pref.setPrefString("userPhone",edPhone.getText().toString()); pref.setPrefString("userLocation",""); startActivity(new Intent(SignUp.this,UserProfile.class)); finish(); } } }); tvLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(SignUp.this,Login.class)); finish(); } }); } }Steps 5:- signup.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"> <EditText
android:id="@+id/userName"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"/> <EditText
android:id="@+id/userPhone"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"/> <Button
android:id="@+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"/> <TextView
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
android:layout_marginTop="20dp"/> </LinearLayout>Steps 6:- Login.java
OUTPUT:-
package com.example.supriyabharti.profilesettingexmp; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; /** * Created by supriyabharti on 05/09/17. */ public class Login extends AppCompatActivity { TextView tvSignup; EditText edPhone; Button btnLoginup; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); final SharedConstant sharedConstant=new SharedConstant(Login.this); tvSignup=(TextView)findViewById(R.id.userSignup); btnLoginup=(Button) findViewById(R.id.userLogin); edPhone=(EditText) findViewById(R.id.userPhone); btnLoginup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(edPhone.getText().toString().matches("")){ Toast.makeText(getApplicationContext(),"Please enter value",Toast.LENGTH_LONG).show(); }else{ String str=sharedConstant.getStringPref("userPhone"); if(str.equalsIgnoreCase(edPhone.getText().toString())){ startActivity(new Intent(Login.this,UserProfile.class)); finish(); }else { Toast.makeText(getApplicationContext(),"Invalid Login",Toast.LENGTH_LONG).show(); } } } }); tvSignup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(Login.this,SignUp.class)); finish(); } }); } }Steps 7:- login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent"> <EditText android:id="@+id/userPhone" android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Phone"/> <Button android:id="@+id/userLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login"/> <TextView android:id="@+id/userSignup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SignUp" android:textSize="20sp" android:layout_marginTop="20dp"/> </LinearLayout>
Steps 8:- UserProfile.java
package com.example.supriyabharti.profilesettingexmp; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * Created by supriyabharti on 05/09/17. */ public class UserProfile extends AppCompatActivity { TextView logout,userName; Button viewProfile; SharedConstant sharedConstant; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_profile); logout=(TextView)findViewById(R.id.logout); userName=(TextView)findViewById(R.id.uerName); viewProfile=(Button) findViewById(R.id.viewProfile); sharedConstant=new SharedConstant(UserProfile.this); String s=sharedConstant.getStringPref("userName"); userName.setText(s); logout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sharedConstant.setPrefString("userName",""); sharedConstant.setPrefString("userPhone",""); startActivity(new Intent(UserProfile.this,SignUp.class)); finish(); } }); viewProfile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(UserProfile.this,UserProfileUpdate.class)); } }); } }Steps 9:-user_profile.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical"android:layout_height="match_parent"> <TextViewandroid:id="@+id/uerName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="30dp"android:text="Name"android:textSize="25sp"android:textStyle="bold"android:textColor="#000"/> <Buttonandroid:id="@+id/viewProfile"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="View Full Profile"android:layout_gravity="center"/> <TextViewandroid:id="@+id/logout"android:layout_width="match_parent"android:gravity="center"android:layout_marginTop="20dp"android:layout_height="wrap_content"android:text="Logout"/> </LinearLayout>Steps 10:- UserProfileUpdate.java
package com.example.supriyabharti.profilesettingexmp; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; /** * Created by supriyabharti on 05/09/17. */ public class UserProfileUpdate extends AppCompatActivity { EditText edPhone,edName,edLocation; Button btnEdit,btnSave; @Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_profile_update); final SharedConstant sharedConstant=new SharedConstant(UserProfileUpdate.this); btnEdit=(Button) findViewById(R.id.userLogin); btnSave=(Button) findViewById(R.id.userSave); edPhone=(EditText) findViewById(R.id.userPhone); btnSave.setVisibility(View.GONE); btnEdit.setVisibility(View.VISIBLE); edName=(EditText) findViewById(R.id.userName); edLocation=(EditText) findViewById(R.id.userLocation); edName.setText(sharedConstant.getStringPref("userPhone")); edPhone.setText(sharedConstant.getStringPref("userName")); edLocation.setText(sharedConstant.getStringPref("userLocation")); edName.setEnabled(false); edPhone.setEnabled(false); edLocation.setEnabled(false); btnEdit.setOnClickListener(new View.OnClickListener() { @Overridepublic void onClick(View view) { edName.setEnabled(true); edPhone.setEnabled(true); edLocation.setEnabled(true); btnSave.setVisibility(View.VISIBLE); btnEdit.setVisibility(View.GONE); } }); btnSave.setOnClickListener(new View.OnClickListener() { @Overridepublic void onClick(View view) { sharedConstant.setPrefString("userName",edName.getText().toString()); sharedConstant.setPrefString("userPhone",edPhone.getText().toString()); sharedConstant.setPrefString("userLocation",edLocation.getText().toString()); edName.setEnabled(false); edPhone.setEnabled(false); edLocation.setEnabled(false); btnSave.setVisibility(View.GONE); btnEdit.setVisibility(View.VISIBLE); } }); } }Steps 11:- user_profile_update.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"> <TextViewandroid:layout_width="match_parent"android:gravity="center"android:layout_marginTop="20dp"android:textStyle="bold"android:textColor="#000"android:textSize="25sp"android:layout_height="wrap_content"android:text="User Name"/> <EditTextandroid:id="@+id/userName"android:layout_margin="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Name"/> <EditTextandroid:id="@+id/userPhone"android:layout_margin="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Phone"/> <EditTextandroid:id="@+id/userLocation"android:layout_margin="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Location"/> <Buttonandroid:id="@+id/userLogin"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Edit"android:visibility="gone"/> <Buttonandroid:id="@+id/userSave"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="save"android:visibility="gone"/> </LinearLayout>
No comments:
Post a Comment