Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==============================================================
- ===============MAIN ACTIVITY CLASS============================
- ==============================================================
- package com.example.app0811.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- 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 txtUser;
- EditText txtPass;
- String userNameLogged;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUser = (EditText)findViewById(R.id.username);
- txtPass = (EditText)findViewById(R.id.pass);
- if (userLogged())
- {
- Intent iLogged = new Intent(this,Hello.class);
- iLogged.putExtra("userName",userNameLogged);
- this.startActivity(iLogged);
- }
- }
- private boolean userLogged()
- {
- SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- userNameLogged = myPref.getString("logged", "error");
- if (userNameLogged.equals("error"))
- {
- //we not logged in
- return false;
- }
- else
- {
- //we are logged in
- return true;
- }
- }
- public void onClick (View v)
- {
- if (chkUser())
- {
- Intent helloIntent = new Intent(this , Hello.class);
- this.startActivity(helloIntent);
- }
- else
- {
- Toast.makeText(this,"Wrong user name or password!!",Toast.LENGTH_LONG).show();
- }
- }
- private boolean chkUser()
- {
- SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- String userName = myPref.getString("userName", "error");
- String userPass = myPref.getString("password","error");
- if (userName.equals(txtUser.getText().toString()) && userPass.equals(txtPass.getText().toString()))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public void btnReg (View v)
- {
- Intent intentReg = new Intent(this, Register.class);
- this.startActivity(intentReg);
- }
- }
- ==============================================================
- ===============HELLO CLASS============================
- ==============================================================
- package com.example.app0811.login;
- 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.TextView;
- /**
- * Created by app0811 on 06/03/2016.
- */
- public class Hello extends Activity
- {
- SharedPreferences myPrefs; //calling SharedPrefrences Class
- SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
- TextView myTxt;
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.hello);
- setPoints();
- myTxt.setText("Hello "+ getUser());
- }
- private void setPoints()
- {
- myTxt = (TextView)findViewById(R.id.hellotxt);
- myPrefs = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- private String getUser()
- {
- String user = myPrefs.getString("userName","error");
- return user;
- }
- public void btnExit(View v)
- {
- editor.remove("logged");
- editor.commit();
- Intent exit = new Intent(this,MainActivity.class);
- this.startActivity(exit);
- }
- }
- ==============================================================
- ===============REGISTER CLASS=================================
- ==============================================================
- package com.example.app0811.login;
- 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;
- /**
- * Created by app0811 on 06/03/2016.
- */
- public class Register extends Activity
- {
- private SharedPreferences myPrefs;//calling SharedPrefrences Class
- private SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
- private EditText crUser;//create user
- private EditText crPass;//password
- private EditText confPass;//confirm password
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.reg);
- setPoint();
- }
- private void setPoint()
- {
- crUser = (EditText)findViewById(R.id.crUser);
- crPass = (EditText)findViewById(R.id.crPass);
- confPass = (EditText)findViewById(R.id.confPass);
- //creating the sharedprefrences xml file
- myPrefs = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- public void creatUser(View v)
- {
- String userName = crUser.getText().toString();
- String userPass = crPass.getText().toString();
- String confirmPass = confPass.getText().toString();
- String chkUser = "";
- if (userName.trim().length()<=0 || userPass.trim().length()<=0 || confirmPass.trim().length()<=0)
- {
- Toast.makeText(this,"Please fill all the fields above",Toast.LENGTH_LONG).show();
- }
- else if (!userPass.equals(confirmPass))
- {
- Toast.makeText(this, "The password not confirm" ,Toast.LENGTH_LONG).show();
- }
- else if(userName.equals(myPrefs.getString("username",chkUser)))
- {
- Toast.makeText(this, "This username is taken" , Toast.LENGTH_LONG).show();
- }
- else
- {
- editor.putString("userName" ,userName );
- editor.putString("password", userPass);
- editor.commit();
- Toast.makeText(this , "The username creating" ,Toast.LENGTH_LONG).show();
- Intent intentMain = new Intent(this,MainActivity.class);
- this.startActivity(intentMain);
- }
- }
- public void btncancel (View v)
- {
- Intent mainIntent = new Intent(this , MainActivity.class);
- this.startActivity(mainIntent);
- }
- }
- ==================activity main xml=================
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="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:hint="Username"
- android:id="@+id/username"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:id="@+id/pass"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Login"
- android:id="@+id/btnLogin"
- android:onClick="onClick"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register"
- android:id="@+id/btnRegister"
- android:onClick="btnReg"
- />
- </LinearLayout>
- =======================hello 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">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Hello"
- android:textSize="54sp"
- android:id="@+id/hellotxt"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Logout"
- android:id="@+id/btnlogout"
- android:onClick="btnExit"/>
- </LinearLayout>
- =================================reg 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">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Username"
- android:id="@+id/crUser"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:id="@+id/crPass"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password2"
- android:id="@+id/confPass"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register"
- android:onClick="creatUser"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Cancel"
- android:onClick="btncancel"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement