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);
// ...
}
Related
I am trying to change the app theme from the fragment using setTheme() method of an activity. But somehow its looping forever while trying to change. Here is my setTheme() method calls.
//XYZ Fragment class
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
if (mThemeName.equalsIgnoreCase("Mode 1")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getActivity().setTheme(R.style.Theme_One);
} else {
getActivity().setTheme(R.style.Theme_Two);
}
super.onViewCreated(view, savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//binding object of this fragment
binding = FragmentXYZBinding.inflate(inflater);
binding.switch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked){
setTheme("Mode 2");
}else {
setTheme("Mode 1");
}
});
return inflater.inflate(R.layout.fragment_setting, container, false);
}
public void setTheme(String name) {
SharedPreferences preferences = getActivity().getSharedPreferences("app", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("ThemeName", name);
editor.apply();
// If i call this recreate() then it goes into infinite loop
// but without calling this I can't change theme on runtime
//getActivity().recreate();
}
Any idea about what could be wrong in here ?
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();
}
}
}
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();
}
After I c/p code to fragment, my function won't set the string inside the app. But it worked at first without fragment, and I can't find the problem. App runs normally, but it won't output setText string on screen, while string is visible with it default string
public class MainFragment extends FragmentActivity{
EditText editNumber;
Button calculate;
TextView result;
int two = 2;
int seven = 7;
int inputNumber;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find elements
editNumber = (EditText) findViewById(R.id.editNumber);
calculate = (Button) findViewById(R.id.calculateButton);
result = (TextView) findViewById(R.id.resultText);
//listener
editNumber.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputNumber = Integer.parseInt(editNumber.getText().toString());
}
});//onClick
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("Output: " + (inputNumber / two) + seven);
}
});//onclick
}//onCreate
}//main
[code]
In FragmentActivity you should define a Fragment object, insert it into the Activity's frame and display things in it. Please refer to:
http://developer.android.com/guide/components/fragments.html
Are you sure your MainFragment is a fragment? It extends activity: FragmentActivity. If it is just a typo issue then you have to provide your layout in the onCreateView() method of the fragment. Something like this:
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.activity_main, container, false);
result = (TextView) v.findViewById(R.id.resultText);
return v;
}
Finding a view in a fragment in other method than onCreateView() you can call:
getView().findViewById(R.id.resultText);
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