splash screen and shared preference in android

Screenshots






Designing part of splash screen and shared preference in android 


splashfile.xml

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

    <ImageView
        android:id="@+id/logo_id"
        android:layout_width="300dp"
        android:layout_height="700dp"
        android:layout_centerInParent="true"
        android:src="@drawable/aaa"/>



</RelativeLayout>






activity_main.xml


<?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="com.example.anshu.sharedpreference.MainActivity">



    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/etName"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:hint="Enter Name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/etPassword"
        android:layout_below="@+id/etName"
        android:layout_centerHorizontal="true"
        android:hint="Enter Password" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/btnLogin"
        android:layout_below="@+id/etPassword"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_alignStart="@+id/etPassword" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/btnRegister"
        android:layout_alignTop="@+id/btnLogin"
        android:layout_toRightOf="@+id/btnLogin"
        android:layout_toEndOf="@+id/btnLogin" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:textSize="30dp"
        android:text="Login" />

</RelativeLayout>





register.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/etNewName"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp"
        android:hint="Enter Username" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/etNewPassword"
        android:layout_below="@+id/etNewName"
        android:layout_centerHorizontal="true"
        android:hint="Enter Password" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/etcompassword"
        android:layout_below="@+id/etNewPassword"
        android:layout_centerHorizontal="true"
        android:hint="Re Enter Password" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etNewEmail"
        android:layout_below="@+id/etcompassword"
        android:layout_alignLeft="@+id/etNewPassword"
        android:layout_alignStart="@+id/etNewPassword"
        android:layout_alignRight="@+id/etNewPassword"
        android:layout_alignEnd="@+id/etNewPassword"
        android:hint="Enter Name" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/btnNewRegister"
        android:layout_below="@+id/etNewEmail"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnNewRegister"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/btnNewRegister"
        android:text="Sign up"
        android:textSize="30dp" />


</RelativeLayout>




display_info


<?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"
    android:weightSum="1">


    <TextView
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="New Text"
        android:id="@+id/textViewName"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp" />


    <TextView
        android:id="@+id/textViewName2"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/textViewName"
        android:layout_alignStart="@+id/textViewName"
        android:layout_below="@+id/textViewName"
        android:layout_marginTop="25dp"
        android:text="New Text"
        android:textSize="20sp"
        android:textStyle="bold" />


    <Button
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:textSize="20dp"
        android:text="Logout"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:id="@+id/logout"/>

</RelativeLayout>


----------------------------------------------------------------------------------------------------------------------



Coding part of  splash screen and shared preference in android 


SplashActivity.java

package com.example.anshu.sharedpreference;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

/**
 * Created by Anshu on 12/13/2017.
 */

public class SplashActivity extends Activity {

    SharedPreferences sharedpreferences;
    public static String str_login_test;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashfile);


        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {


        SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
        str_login_test = preferences.getString("Username","");


        final String userName = preferences.getString("Username", "");
        final String password1 = preferences.getString("Password", "");

        if (!userName.equalsIgnoreCase("") && !password1.equalsIgnoreCase("")) {
            startActivity(new Intent(SplashActivity.this, DisplayScreen.class));
            finish();

        }
        else {
            Intent SplashActivity = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(SplashActivity);
            finish();
        }



        /*

        final String userName = preferences.getString("Username", "");
        final String password1 = preferences.getString("Password", "");
*/

                /*Intent intent=new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);*/
                //finish();
            }
        },3000);
    }
}





MainActivity

package com.example.anshu.sharedpreference;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText etName, etPassword;
    Button btnLogin, btnRegister;
    SharedPreferences sharedpreferences;
    String userName,password1,email;

    public static final String MyPREFERENCES = "MyPrefs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etName = (EditText) findViewById(R.id.etName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                return false;
            }
        });


        btnLogin = (Button) findViewById(R.id.btnLogin);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        btnRegister = (Button) findViewById(R.id.btnRegister);

        SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);

//                String userDetails = preferences.getString(user + password + "data","No information on that user.");
//                SharedPreferences.Editor editor = preferences.edit();
//                editor.putString("display",userDetails);
//                editor.commit();


         userName = preferences.getString("Username", "");
         password1 = preferences.getString("Password", "");
         email = preferences.getString("email", "");



        /*if (!userName.equalsIgnoreCase("") && !password1.equalsIgnoreCase("")){
            startActivity(new Intent(MainActivity.this,DisplayScreen.class));
            finish();
        }*/


        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = etName.getText().toString();
                String password = etPassword.getText().toString();


                if (etName.getText().toString().trim().length() == 0) {
                    etName.setError("User Name is not entered");
                    etName.requestFocus();
                } else if (etPassword.getText().toString().trim().length() == 0) {
                    etPassword.setError("Password is not entered");
                    etPassword.requestFocus();
                } else if (!userName.equals(user)) {
                    Toast.makeText(MainActivity.this, "UserName does not match", Toast.LENGTH_SHORT).show();
                } else if (!password1.equals(password)) {
                    Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(MainActivity.this, DisplayScreen.class));
                }
                /*else {
                    Intent intent = new Intent(getApplicationContext(),DisplayScreen.class);
                    startActivity(intent);
                }*/




                /*Intent displayScreen = new Intent(MainActivity.this, DisplayScreen.class);
                startActivity(displayScreen);*/
                /*loginCheck();*/

            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent registerScreen = new Intent(MainActivity.this, Register.class);
                startActivity(registerScreen);
            }
        });


    }

    //Important

     /*void loginCheck() {
        if (etName.getText().toString().equals("Anshu") && etPassword.getText().toString().equals("Anshu1"))
        {
            SharedPreferences.Editor e=sp.edit();
            e.putString("etName","Anshu");
            e.putString("etPassword","Anshu1");
            e.commit();

            Toast.makeText(MainActivity.this,"Login Sucessful",Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this,DisplayScreen.class));
            finish();
        }
        else {
            Toast.makeText(MainActivity.this,"Incorrect Login Details",Toast.LENGTH_SHORT).show();
       */

}



Register.java


package com.example.anshu.sharedpreference;

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.Toast;

/**
 * Created by Anshu on 12/13/2017.
 */

public class Register extends AppCompatActivity {

    EditText username, password, reenterpass, email;
    Button btnRegister;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        init();
    }


    private void init() {
        username = (EditText) findViewById(R.id.etNewName);
        reenterpass = (EditText) findViewById(R.id.etcompassword);
        password = (EditText) findViewById(R.id.etNewPassword);
        email = (EditText) findViewById(R.id.etNewEmail);
        btnRegister = (Button) findViewById(R.id.btnNewRegister);
        preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                validation();
            }
        });
    }

    private void validation() {
        if (username.getText().toString().length() == 0) {
            username.setError("User name is Required");
        } else if (password.getText().toString().length() == 0) {
            password.setError("Password Required");
        } else if (reenterpass.getText().toString().length() == 0) {
            reenterpass.setError("Password Required");
        } else if (email.getText().toString().length() == 0) {
            email.setError("Email is Requied");
        } else if (!password.getText().toString().trim().equals(reenterpass.getText().toString())) {
            Toast.makeText(Register.this, "Password Not Match", Toast.LENGTH_SHORT).show();
        } else {
            //After Register it will be redirect to login page
            String newUser = username.getText().toString();
            String newPassword = password.getText().toString();
            String newEmail = email.getText().toString();

            SharedPreferences.Editor editor = preferences.edit();

            editor.putString("Username", newUser);
            editor.commit();
            editor.putString("Password", newPassword);
            editor.commit();
            editor.putString("email", newEmail);
            editor.commit();
            Intent MainActivity = new Intent(Register.this, MainActivity.class);
            startActivity(MainActivity);
        }
    }
}



DisplayScreen.java


package com.example.anshu.sharedpreference;

import android.app.Activity;
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.TextView;
import android.widget.Toast;

import java.util.jar.Attributes;

/**
 * Created by Anshu on 12/13/2017.
 */

public class DisplayScreen extends Activity implements View.OnClickListener {


    String name1,email;
    TextView UserName,name;
    SharedPreferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_info);

        Button logout = (Button) findViewById(R.id.logout);
        logout.setOnClickListener(this);

        SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
        String display = preferences.getString("Username", "");
        String display1 = preferences.getString("email", "");
   /*     String display1 = preferences.getString("email", "");
*/
        /*StringBuilder builder = new StringBuilder();
        builder.append("UserName : "+UserName);*/

        TextView displayInfo = (TextView) findViewById(R.id.textViewName);
        displayInfo.setText(display);

        TextView Displayinfo1 = (TextView) findViewById(R.id.textViewName2);
        Displayinfo1.setText(display1);
        /*TextView displayInfo1 = (TextView) findViewById(R.id.etNewEmail);
        displayInfo1.setText(display1);*/



    }

    @Override
    public void onClick(View view) {
        SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
                    /*preferences.edit().clear().commit();*/
        SharedPreferences.Editor editor = preferences.edit();
        Toast.makeText(DisplayScreen.this, "You Have Sucessfullylogout", Toast.LENGTH_SHORT).show();
        editor.clear();
        //editor.apply();
        editor.commit();
                    /*editor.commit();*/
        Intent DisplayScreen = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(DisplayScreen);
    }
}

0 comments: