Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Activity Java///////////////////////////////
- package com.mordekicloud.yuli.user123;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- EditText txtUserName;
- EditText txtPassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUserName = (EditText) findViewById(R.id.txtUser1);
- txtPassword = (EditText) findViewById(R.id.txtPass1);
- if (CheckUser()) {
- SharedPreferences
- pref = getApplicationContext().getSharedPreferences("Pref", MODE_PRIVATE);
- Intent loggedIntent = new Intent(this, Daf3.class);
- loggedIntent.putExtra("userPut", pref.getString("Logged In", "Error"));
- this.startActivity(loggedIntent);
- }
- }
- private boolean CheckUser ()
- {
- SharedPreferences
- pref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
- String userName = pref.getString("Logged In", "Error");
- return (!userName.equals("Error"));
- }
- public void btnLogIn(View v) {
- SharedPreferences pref =
- getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
- String logUserName = txtUserName.getText().toString().toLowerCase();
- String logPassword = txtPassword.getText().toString();
- String logPasswordConf = pref.getString(logUserName, "Error");
- if (logUserName.isEmpty() || logPassword.isEmpty()) {
- Toast.makeText(this, "Ther is one or more fields empty", Toast.LENGTH_LONG).show();
- return;
- }
- else if (!pref.contains(logUserName) || !logPassword.equals(logPasswordConf)) {
- Toast.makeText(this, "Invald user name or password", Toast.LENGTH_LONG).show();
- return;
- }
- else {
- SharedPreferences.Editor editor = pref.edit();
- editor.putString("Logged", logUserName).commit();
- Toast.makeText(this, "You logged successfuly", Toast.LENGTH_SHORT).show();
- Intent loggedIntent = new Intent(this, Daf3.class);
- loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
- this.startActivity(loggedIntent);
- }
- }
- public void btnSingUp (View v)
- {
- Intent singIntent = new Intent(this, Daf2.class);
- this.startActivity(singIntent);
- }
- }
- Daf2 java//////////////////////////////////////
- package com.mordekicloud.yuli.user123;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.util.IllegalFormatCodePointException;
- /**
- * Created by yuli.mordek on 3/11/2016.
- */
- public class Daf2 extends Activity
- {
- EditText txtUserName;
- EditText txtPassword;
- EditText txtComfPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.daf2);
- txtUserName=(EditText)findViewById(R.id.UserName);
- txtPassword=(EditText)findViewById(R.id.Password);
- txtComfPass=(EditText)findViewById(R.id.ConfPass);
- }
- public void btnCancel (View v)
- {
- Intent cancelIntent=new Intent(this, MainActivity.class);
- this.startActivity(cancelIntent);
- }
- public void btnEnter(View v)
- {
- SharedPreferences
- pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
- SharedPreferences.Editor editor=pref.edit();
- String UserName=txtUserName.getText().toString().toLowerCase();
- String Password=txtPassword.getText().toString().toLowerCase();
- String ConfPass=txtComfPass.getText().toString().toLowerCase();
- if (UserName.isEmpty() || Password.isEmpty() || ConfPass.isEmpty())
- {
- Toast.makeText(this,"You must fill all fields",Toast.LENGTH_LONG).show();
- return;
- }
- else if(pref.contains(UserName))
- {
- Toast.makeText(this,"User you exists",Toast.LENGTH_SHORT).show();
- return;
- }
- else if (!Password.equals(ConfPass))
- {
- Toast.makeText(this, "Password is not correct", Toast.LENGTH_LONG).show();
- return;
- }
- Toast.makeText(this, "You registreted successful", Toast.LENGTH_SHORT).show();
- editor.putString(UserName, Password).commit();
- Intent registerIntent = new Intent(this,MainActivity.class);
- this.startActivity(registerIntent);
- }
- }
- Daf3 Java////////////////////////////////////
- package com.mordekicloud.yuli.user123;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- /**
- * Created by yuli.mordek on 3/11/2016.
- */
- public class Daf3 extends Activity
- {
- TextView txtUser1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.daf3);
- txtUser1=(TextView)findViewById(R.id.Msg);
- txtUser1.setText("Hiiiiiiiii!!"+getIntent().getStringExtra("userPut"));
- }
- public void btnOut(View v)
- {
- SharedPreferences
- pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
- SharedPreferences.Editor editor=pref.edit();
- editor.remove("Logged").commit();
- Intent outIntent=new Intent(this,MainActivity.class);
- this.startActivity(outIntent);
- }
- }
- main activity XML///////////////////////////
- <LinearLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:layout_marginBottom="20dp"
- android:layout_margin="15dp"
- android:hint="User Name"
- android:textSize="25sp"
- android:gravity="center"
- android:id="@+id/txtUser1"
- android:inputType="text"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="20dp"
- android:layout_margin="15dp"
- android:textSize="25sp"
- android:hint="Password"
- android:gravity="center"
- android:id="@+id/txtPass1"
- android:inputType="textPassword"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:layout_marginBottom="20dp"
- android:text="Log In"
- android:textSize="25sp"
- android:onClick="btnLogIn"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:text="Sing Up"
- android:textSize="25sp"
- android:onClick="btnSingUp"
- />
- </LinearLayout>
- daf2 XML//////////////////////////////
- <LinearLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:layout_marginTop="20dp"
- android:textSize="25sp"
- android:hint="Enter User Name"
- android:gravity="center"
- android:id="@+id/UserName"
- android:inputType="text"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:textSize="25sp"
- android:hint="Password"
- android:gravity="center"
- android:id="@+id/Password"
- android:inputType="text"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:hint="Confirm Password"
- android:textSize="25sp"
- android:gravity="center"
- android:id="@+id/ConfPass"
- android:inputType="text"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:text="Enter"
- android:textSize="25sp"
- android:onClick="btnEnter"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dp"
- android:text="Cancel"
- android:textSize="25sp"
- android:onClick="btnCancel" />
- </LinearLayout>
- daf3 XML////////////////////////////////////
- <LinearLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/Msg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="Logout"
- android:textSize="25sp"
- android:layout_margin="15dp"
- android:onClick="btnOut"
- />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement