SharePreference in android

 package com.example.mylibrary.Fragments;


import static android.graphics.BlendMode.COLOR;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mylibrary.R;
import com.google.type.Color;

public class HomeFragment extends Fragment {
Button BottomSheet,save,movetext;
LinearLayout containers;
TextView text;
EditText inputext;
Switch switchs;
String inputtext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_home, container, false);

BottomSheet = root.findViewById(R.id.bottomsheet);
containers = root.findViewById(R.id.containers);
save = root.findViewById(R.id.save);
movetext = root.findViewById(R.id.settext);
text = root.findViewById(R.id.savetext);
inputext = root.findViewById(R.id.inputtext);
switchs = root.findViewById(R.id.switch1);


save.setOnClickListener(v->{
String text1 = inputext.getText().toString();
SharedPreferences sharedPreferences = getContext().getSharedPreferences("name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("text",text1);
editor.apply();
editor.putBoolean("switch",switchs.isChecked());
});

// SharedPreferences preferences = getActivity().getSharedPreferences("name",Context.MODE_PRIVATE);
// String texts = preferences.getString("text","");
// Boolean switchonoff = preferences.getBoolean("switch",false);
// text.setText(texts);
// switchs.setChecked(switchonoff);

movetext.setOnClickListener(v->{
// String text1 = inputext.getText().toString();
// text.setText(text1);
// Toast.makeText(getContext(), "set text", Toast.LENGTH_SHORT).show();
SharedPreferences preferences1 = getActivity().getSharedPreferences("names",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences1.edit();
editor.putString("textss","Hello world all the data will be store in the app");
editor.apply();
});

SharedPreferences preferences2 = getActivity().getSharedPreferences("names",Context.MODE_PRIVATE);
String text1 = preferences2.getString("textss","");
text.setText(text1);


BottomSheet.setOnClickListener(v->{
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getFragmentManager(),bottomSheetFragment.getTag());
});

return root;
}
}

play.setOnClickListener(v->{
String names = name.getText().toString();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("name",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if(names.isEmpty()){
Toast.makeText(this, "Enter text here to save your data in our app", Toast.LENGTH_SHORT).show();
}else{
editor.putBoolean("isclick",true);
editor.putString("name",names);
}
editor.apply();
});

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("name",MODE_PRIVATE);
String name = sharedPreferences.getString("name","helo");
Boolean isclick = sharedPreferences.getBoolean("isclick",false);
if(isclick){
stop.setText(name);
}else{
stop.setText("Please save your data");
}

Comments

Popular posts from this blog

Media Player in android

Fetch data from Api in android using Volley library

GridView in android