I have created two fragments. The second fragment contains multiple edittexts.
The SharedPreference works well, but only on the last edittext. For the remaining, it doesn't save anything. At the last, when we write in the edittext, then saving and running again, the app still shows the previous saved date.
EditText et;
public TwoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
et =(EditText) view.findViewById(R.id.strength_score);
et =(EditText) view.findViewById(R.id.strength_modif);
et =(EditText) view.findViewById(R.id.strength_tem_scor);
et =(EditText) view.findViewById(R.id.strength_tem_modi);
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE);
et.setText(setting.getString("value", ""));
// Inflate the layout for this fragment
return view;
}
public void onStop( ){
super.onStop();
if(et.getText() != null) {
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.putString("value", et.getText().toString());
editor.commit();
}
}
}
Thanks for the help.
Do like this -
EditText et,et1,et2,et3;
public TwoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
et =(EditText) view.findViewById(R.id.strength_score);
et1 =(EditText) view.findViewById(R.id.strength_modif);
et2 =(EditText) view.findViewById(R.id.strength_tem_scor);
et3 =(EditText) view.findViewById(R.id.strength_tem_modi);
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE);
et.setText(setting.getString("value", ""));
et1.setText(setting.getString("value1", ""));
et2.setText(setting.getString("value2", ""));
et3.setText(setting.getString("value3", ""));
// Inflate the layout for this fragment
return view;
}
public void onStop( ){
super.onStop();
if(et.getText() != null) {
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.putString("value", et.getText().toString());
editor.putString("value1", et1.getText().toString());
editor.putString("value2", et2.getText().toString());
editor.putString("value3", et3.getText().toString());
editor.commit();
}
}
}
Related
I am very new to android and I am trying to make CardView by programmatically, I am using Fragment. By setting an OnClick on the button I am able to create cards in my layout. The issue is after closing the activity cards are not showing.
How can I save the cards, so after closing the activity cards should be there?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("Okhlee");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
return view;
}
#Override
public void onClick(View v) {
createCardView();
}
/*Adding cardview programmatically function */
private void createCardView() {
CardView addedCardView = new CardView(context);
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
addedCardView.setLayoutParams(layoutParams);
addedCardView.setPadding(10 ,10, 10, 10);
addedCardView.setRadius(15);
addedCardView.setCardBackgroundColor(Color.BLUE);
addedCardView.setMaxCardElevation(30);
addedCardView.setMaxCardElevation(6);
relativeLayout.addView(addedCardView);
}
Sorry for my bad English.
You can use SharedPreferences in order to know whether you should display the card.
Here's a sample code using your original one -
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean shouldDisplayCard = sharedPref.getBoolean("should_display_card", false);
if(shouldDisplayCard)
createCardView();
return view;
}
#Override
public void onClick(View v) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("should_display_card", true);
editor.apply();
createCardView();
}
Showing error while using MODE_PRIVATE for sharedPreferences within onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}
You should use it like this Context.MODE_PRIVATE.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}
Add context in your code like :
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);
I want to update the NumberPicker using the value stored in Shared Preferences. This is all happening inside a fragment, there is also a button which calls the onValueChange() method whenever the user clicks on it.
HomeFragment.java
import android.content.SharedPreferences;
import android.widget.NumberPicker;
public class HomeFragment extends Fragment {
NumberPicker numberPicker;
SharedPreferences sharedPref;
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
numberPicker = (NumberPicker) rootView.findViewById(R.id.np);
//Populate NumberPicker values from minimum and maximum value range
//Set the minimum value of NumberPicker
numberPicker.setMinValue(0);
//Specify the maximum value/number of NumberPicker
numberPicker.setMaxValue(239);
//Gets whether the selector wheel wraps when reaching the min/max value.
numberPicker.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
final NumberPicker.OnValueChangeListener np_listener = new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal){
//Statement goes here...
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("someInteger", newVal);
editor.commit();
}
};
numberPicker.setOnValueChangedListener(np_listener);
b1 = (Button) rootView.findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int oldValue = numberPicker.getValue();
numberPicker.setValue(oldValue + 1);
np_listener.onValueChange(numberPicker, oldValue, oldValue + 1);
}
});
// Inflate the layout for this fragment
return rootView;
}
}
Whenever the NumberPicker value changes, I'm saving it in Preferences but where to retrieve the value? I know the code to retrieve:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int someInteger = sharedPref.getInt("someInteger", 0);
But where to put this code, inside onCreate() or onCreateView()? After the value is retrieved if it exists then how to call onValueChange() so that all the statements inside that method gets executed?
You don't have to call it from SharedPreferences.
Inside onCreateView, set the NumberPicker value using the setValue method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ... some code ...
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
// Use default value 0 if key does not exist
int someInteger = sharedPref.getInt("someInteger", 0);
numberPicker = (NumberPicker) rootView.findViewById(R.id.np);
numberPicker.setValue(someInteger);
// ...
}
Is is possible to LOCKED fragments on button click?
For example.. Im making a quiz app but it is in Fragment form..
after clicking the SUBMIT BUTTON. you must swipe to the next page or the QUESTION 2.java.
now the thing is..
I want to disable the PREVIOUS fragment so you cannot go back to QUESTION 1.
Thank you guys!
my code..
QUESTION 1.JAVA
public class Question1 extends Fragment{
RadioButton q1a2;
Button btn1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question1, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
btn1 = (Button)getView().findViewById(R.id.btnq1);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
Toast.makeText(getActivity(), "Submitted", Toast.LENGTH_SHORT).show();
if (q1a2.isChecked()){
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.commit();
}
});
}
}
QUESTION 2.JAVA
public class Question2 extends Fragment{
RadioButton q2a1;
Button btn2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question2, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q2a1 = (RadioButton)getView().findViewById(R.id.q2a1);
btn2 = (Button)getView().findViewById(R.id.q2_button);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
if (q2a1.isChecked()){
editor.putInt("answer_value2", 1);
} else {
editor.putInt("answer_value2", 0);
}
editor.commit();
}
});
}
}
You can remove the old fragments from the viewpager dynamically, as the user moves forward:
dynamically add and remove view to viewpager
I'd like to create editable textfield in fragment, which after close or stop of app would be saved. But there's something wrong in line with return notatki; I already have this:
public class DetailFragment2 extends Fragment {
private EditText notatki;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
#Override
public void onStop( ){
super.onStop();
if(notatki.getText() != null) {
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("value", notatki.getText().toString());
editor.commit();
}
}
}
When I change return notatki; to return view; it works until the stop of app, when I wanted to save content of editText but it isn't saving anything.
Change to
private EditText notatki;
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view; // return view here instead of notaki
}
You have already declared EditText as a class member
private EditText notatki;
So just initialize it in onCreateView.
Change
EditText notatki = (EditText) view.findViewById(R.id.editText1);
to
notatki = (EditText) view.findViewById(R.id.editText1);
because if you declare it again in your onCreateView it will shadow the global one and you will get it null in onStop
Try This....
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
EditText notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
You are doing it right but the problem is that you should return the parent view from onCreateView and not the EditText instance notatki
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
....
return view;
}
private EditText etext;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment,container,false);
etext = (EditText) getView().findViewById(R.id.editText4);
return v;