0% found this document useful (0 votes)
2 views

Log in Share Preference

The document contains XML layout files for an Android application, including a login screen (activity_main.xml) and a second activity (activity_main_2.xml) with logout functionality. It also includes Java code for the MainActivity and MainActivity2 classes, which handle user login, remember me functionality, and logout actions. The application features a user interface with input fields for username and password, social login options, and buttons for login and logout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Log in Share Preference

The document contains XML layout files for an Android application, including a login screen (activity_main.xml) and a second activity (activity_main_2.xml) with logout functionality. It also includes Java code for the MainActivity and MainActivity2 classes, which handle user login, remember me functionality, and logout actions. The application features a user interface with input fields for username and password, social login options, and buttons for login and logout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

<?xml version="1.0" encoding="utf-8"?

>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:type="linear" android:angle="100"


android:startColor="#336De1" android:endColor="#EF2f2f" />
</shape>

<?xml version="1.0" encoding="utf-8"?>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="24dp" />
<stroke android:width="1dp" android:color="@color/white" />
</shape>

Acitivity_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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp"
android:background="@drawable/login_background"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="160dp"
android:layout_height="160dp"
android:src="@drawable/icon_account_circle"/>

<EditText
android:id="@+id/username_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20dp"
android:padding="18dp"
android:inputType="text"
android:background="@drawable/round_conner"/>
<EditText
android:id="@+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Password"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20dp"
android:inputType="textPassword"
android:padding="18dp"
android:background="@drawable/round_conner"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:backgroundTint="@color/white"
android:textColor="#3B84F1"
android:padding="18dp"
android:layout_marginTop="32dp"
android:textSize="20dp"
android:id="@+id/login_btn"/>

<CheckBox
android:id="@+id/rememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:buttonTint="@color/white"
android:text="Remember me"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Social Login"
android:textColor="@color/white"
android:layout_marginTop="32dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="18dp"
android:gravity="center"
>
<ImageView
android:id="@+id/facebook_btn"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="16dp"
android:src="@drawable/face"/>
<ImageView
android:id="@+id/google_btn"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="16dp"
android:src="@drawable/google"/>
</LinearLayout>

</LinearLayout>

</RelativeLayout>

Activity_main_2.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:id="@+id/main2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background"
android:gravity="center"
android:padding="16dp"
tools:context=".MainActivity2">

<Button
android:id="@+id/logout_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:backgroundTint="@color/white"
android:padding="18dp"
android:text="Logout"
android:textColor="#3B84F1"
android:textSize="20dp" />

<Button
android:id="@+id/logout_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:backgroundTint="@color/white"
android:padding="18dp"
android:text="Thoát"
android:textColor="#3B84F1"
android:textSize="20dp" />

</LinearLayout>

Mainactivity.java
public class MainActivity extends AppCompatActivity {
EditText username, password;
CheckBox rememberMe;
Button btnLogin;

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

init();
checkLogin();

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//Toast.makeText(MainActivity.this,username.getText().toString().equals("admi
n") + "",Toast.LENGTH_SHORT).show();
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("123456"))
{
if (rememberMe.isChecked())
{
SharedPreferences sharedPreferences =
getSharedPreferences("login_check",MODE_PRIVATE);
SharedPreferences.Editor editor =
sharedPreferences.edit();
editor.putString("login","true");
editor.apply();
}
Intent intent = new
Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}

else {
Toast.makeText(MainActivity.this,"Vui lòng nhập lại mật
khẩu",Toast.LENGTH_SHORT).show();
}

}
});
}

private void checkLogin()


{
SharedPreferences sharedPreferences =
getSharedPreferences("login_check",MODE_PRIVATE);
String login = sharedPreferences.getString("login","");
if(login.equals("true"))
{
Intent intent = new
Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
}

private void init(){


username = findViewById(R.id.username_input);
password = findViewById(R.id.password_input);
btnLogin = findViewById(R.id.login_btn);
rememberMe = findViewById(R.id.rememberMe);
}
}

MainActivity2.java
public class MainActivity2 extends AppCompatActivity {
Button logout,btnThoat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);
logout = findViewById(R.id.logout_btn);

btnThoat = findViewById(R.id.logout_exit);

btnThoat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);

Intent startMain = new Intent(Intent.ACTION_MAIN);


startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
}
});

logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences =
getSharedPreferences("login_check",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//editor.putString("login","false");
editor.clear();
editor.commit();
finish();
}
});
}
}

You might also like