Skip to main content

Registration And Loging Activity Handle By SharedPrefrence In Android

*****************************MainActivity.Java******************************************
    public class MainActivity extends Activity {
    EditText txtname,txtuname,txtpass;
    Button btnsubmit;
    SharedPreferences pref;
    boolean hasLoggedIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        txtname=(EditText)findViewById(R.id.editText1);
        txtuname=(EditText)findViewById(R.id.editText2);
        txtpass=(EditText)findViewById(R.id.editText3);
        btnsubmit=(Button)findViewById(R.id.button1);
       
        pref=getSharedPreferences("Mydemo", Context.MODE_PRIVATE);
        hasLoggedIn = pref.getBoolean("ISLogin", false);
       
        if(hasLoggedIn)
        {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            MainActivity.this.finish();
        }
        else
        {
        btnsubmit.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                pref=getSharedPreferences("Mydemo", Context.MODE_PRIVATE);
                Editor ed=pref.edit();
                ed.putString("name", txtuname.getText().toString());
                ed.putString("uname", txtuname.getText().toString());
                ed.putString("pass", txtpass.getText().toString());
                ed.putBoolean("ISLogin", true);
                ed.commit();
                Toast.makeText(MainActivity.this, "Registration Successfully", Toast.LENGTH_LONG).show();
               
                Intent i=new Intent(MainActivity.this,HomeActivity.class);
                startActivity(i);
            }
        });
        }
    }
}
*******************************LoginActivity.java***************************************
public class LoginActivity extends Activity {
    SharedPreferences pref;
    EditText txtuname,txtpass;
    Button btn;
    String suname;
    String spass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        txtuname=(EditText)findViewById(R.id.editText1);
        txtpass=(EditText)findViewById(R.id.editText2);
        btn=(Button)findViewById(R.id.button1);
        pref=getSharedPreferences("Mydemo", Context.MODE_PRIVATE);
       
       
        btn.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                suname=pref.getString("uname",null);
                spass=pref.getString("pass", null);
                if(suname.equals(txtuname.getText().toString()) && spass.equals(txtpass.getText().toString()) )
                {
                    Intent i=new Intent(LoginActivity.this,HomeActivity.class);
                    startActivity(i);
                }
                else
                {
                    Toast.makeText(LoginActivity.this, "Please Enter Correct Username And Password", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
***************************HomeActivity.Java******************************************
public class HomeActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }
}

**************************activity_main.xml********************************************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="38dp"
        android:background="@drawable/round"
        android:hint="Enter Your Name"
        android:ems="10">     
 </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:text="Registration Form"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="21dp"
        android:hint="Enter User Name"
        android:background="@drawable/round"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="22dp"
        android:background="@drawable/round"
        android:hint="Enter Password"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button1"
        style="@style/ButtonWhiteText"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundedbutton"
        android:text="Submit" />

</RelativeLayout>
******************************second.xml*******************************************
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="38dp"
        android:background="@drawable/round"
        android:ems="10"
        android:hint="Enter USerName" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
        android:background="@drawable/round"
        android:ems="10"
        android:hint="Enter YourPassword" >
      </EditText>

    <Button
        android:id="@+id/button1"
        style="@style/ButtonWhiteText"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:background="@drawable/roundedbutton"
        android:text="Button" />

</RelativeLayout>
******************************home.xml*********************************************
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="91dp"
        android:layout_marginTop="117dp"
        android:text="Wel Come To Home Page"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
***********************************************************************************
                                                             OUTPUT 





Download Source Code:Click Here

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"