Android Studio Share Preferences - JAVA And XMl CODE
MainActivity - XML CODE
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#E4EEED"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="What is your name"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edShare1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:maxLines="1"
android:shadowColor="@color/black"
android:outlineSpotShadowColor="#FF0000"
android:outlineAmbientShadowColor="@color/black"
android:focusedSearchResultHighlightColor="@color/black"
android:searchResultHighlightColor="@color/black"
android:textColorLink="#FF0000"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="Gmail"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edShare2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:maxLines="1"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="Phone Number"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edShare3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:inputType="number"
android:maxLines="1"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="Voter ID Number"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edShare4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:inputType="number"
android:maxLines="1"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="address"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edShare5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:maxLines="1"
/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/ButtonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Now"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
android:backgroundTint="#E91E63"
/>
<Button
android:id="@+id/ButtonGoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to Activity"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
android:backgroundTint="#E91E63"
/>
</LinearLayout>
MainActivity - JAVA CODE
package com.mahfuj.sharedprefere;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.textfield.TextInputEditText;
public class MainActivity extends AppCompatActivity {
Button ButtonGoto, ButtonSave;
TextInputEditText edShare1, edShare2, edShare3, edShare4, edShare5;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonGoto = findViewById(R.id.ButtonGoto);
ButtonSave = findViewById(R.id.ButtonSave);
edShare1 = findViewById(R.id.edShare1);
edShare2 = findViewById(R.id.edShare2);
edShare3 = findViewById(R.id.edShare3);
edShare4 = findViewById(R.id.edShare4);
edShare5 = findViewById(R.id.edShare5);
sharedPreferences = getSharedPreferences(""+getString(R.string.app_name), MODE_PRIVATE);
editor = sharedPreferences.edit();
ButtonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edShare1.getText().toString();
String gmail = edShare2.getText().toString();
String phone = edShare3.getText().toString();
String voter = edShare4.getText().toString();
String address = edShare5.getText().toString();
editor.putString("name", ""+name+"\n"+gmail+"\n"+phone+"\n"+voter+"\n"+address);
editor.apply();
}
});
ButtonGoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});
}
}
MainActivity2 - XML CODE
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2"
android:orientation="vertical"
android:background="#E4EEED"
>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:strokeColor="#D6CACA"
app:cardBackgroundColor="#7B9E9B"
>
<TextView
android:id="@+id/TextViev"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="30dp"
android:text=""
android:layout_margin="10dp"
/>
</com.google.android.material.card.MaterialCardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="3"
android:layout_margin="5dp"
>
<com.google.android.material.card.MaterialCardView
android:id="@+id/LoveButton"
android:layout_width="match_parent"
android:layout_height="50dp"
app:strokeColor="#D6CACA"
app:cardBackgroundColor="#0022FF"
android:layout_weight="1"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/heart"
android:padding="5dp"
/>
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/AnimisonLottiJOGP"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/search"
android:visibility="visible"
/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/CopyButton"
android:layout_width="match_parent"
android:layout_height="50dp"
app:strokeColor="#D6CACA"
app:cardBackgroundColor="#FF0000"
android:layout_weight="1"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/copy"
android:padding="5dp"
/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/ShareButton"
android:layout_width="match_parent"
android:layout_height="50dp"
app:strokeColor="#D6CACA"
app:cardBackgroundColor="#E91E63"
android:layout_weight="1"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/share"
android:outlineSpotShadowColor="#FFFFFF"
android:padding="5dp"
/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</LinearLayout>
MainActivity2 - JAVA CODE
package com.mahfuj.sharedprefere;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity {
CardView CopyButton, ShareButton;
TextView TextViev;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
CopyButton = findViewById(R.id.CopyButton);
ShareButton = findViewById(R.id.ShareButton);
TextViev = findViewById(R.id.TextViev);
sharedPreferences = getSharedPreferences(""+getString(R.string.app_name), MODE_PRIVATE);
String mystring = sharedPreferences.getString("name", "");
TextViev.setText(mystring);
CopyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager clipboar = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("TextView", TextViev.getText().toString());
clipboar.setPrimaryClip(clip);
Toast.makeText(MainActivity2.this, "Copy Text", Toast.LENGTH_SHORT).show();
}
});
ShareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myintent = new Intent(Intent.ACTION_SEND);
myintent.setType("text/plain");
myintent.putExtra(Intent.EXTRA_TEXT, TextViev.getText().toString().trim());
Intent.createChooser(myintent, "Share");
startActivity(myintent);
Toast.makeText(MainActivity2.this, "Sharing", Toast.LENGTH_SHORT).show();
}
});
}
}