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
Related
So, I wanted to make an app that has the main activity in which I can switch between two fragments containing a number randomizer and the other one a stopwatch. The stopwatch, meanwhile, must not stop if I switch to another fragment (it may only stop after I click again a start/stop button). I can switch between them normally, but when I try to generate a random number via button or I try to start the stopwatch, the app crashes. This is what I tried:
Stopwatch fragment:
public class frStopwatch extends Fragment {
View view;
EditText etTime;
Button btnStartStop;
Button btnReset;
Chronometer chronometer;
private boolean running;
private long pauseOffset;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_stopwatch, container, false);
btnStartStop = (Button) view.findViewById(R.id.btnStartStop);
btnReset = (Button) view.findViewById(R.id.btnReset);
btnStartStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.start();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
return view;
}
}
Randomizer fragment:
public class frRandomizer extends Fragment {
View view;
Button btnRandom;
EditText etShowNum;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_randomizer, container, false);
btnRandom = view.findViewById(R.id.btnRandom);
etShowNum = view.findViewById(R.id.etShowNum);
btnRandom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random r = new Random();
etShowNum.setText(r.nextInt(9));
}
});
return view;
}
}
The app crashes on both emulator and mobile phone.
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();
}
I am trying to add a button listener to a fragment view in Android. When I tap the "Like" or "Dislike" buttons, nothing appears to happen. I don't get the "dislikeClicked" or "likeClicked" in my Logcat view.
Here is my code:
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
queryForData();
View rootView = inflater.inflate(R.layout.fragment_view1, container, false );
likeButton = (Button) rootView.findViewById(R.id.view1_likeButton);
likeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("likeClicked", "");
like();
}
});
dislikeButton = (Button) rootView.findViewById(R.id.view1_dislikeButton);
dislikeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("dislikeClicked", "");
dislike();
}
});
return rootView;
}
Any help is appreciated.
Thanks!
I am trying to implement Radio groups in my SherlockFragment activity....but not able to do this.I am getting "java.lang.NullPointer" Exception when i run the application and no activity is displayed on the screen.
here is my code
public class Fragment_1 extends SherlockFragment {
RadioGroup cls;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View V= inflater.inflate(R.layout.fragment_1, container, false);
cls= (RadioGroup) V.findViewById(R.id.radioclass);
cls.clearCheck();
OnClickListener listener = new OnClickListener(){
/#Override
public void onClick(View V) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) V;
clss=rb.getText().toString();
}
};
RadioButton rb1 = (RadioButton)V. findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
RadioButton rb2 = (RadioButton)V. findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener);
rb2.setChecked(true);
return V;
}
}
I don't think I would be doing you any favours by posting a cut and paste solution, but this might be
helpful (don't be too surprised if you find a few errors in it):
public class Fragment_1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// inflate ...
View view = inflater.inflate(R.layout.fragment_1, container, false);
return view;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button rb1 = (Button) getActivity().findViewById(R.id.radioButton1);
//then do other buttons ...
rb1.setOnClickListener(next_Listener);
//then do other buttons ...
}
private OnClickListener next_Listener = new OnClickListener() {
public void onClick(View v) {
//xml find out which radio button has been checked ...
RadioGroup radio_grp=(RadioGroup)getActivity().findViewById(R.id.radio_grp); //change or leave out this line (I've put it in because you might find it useful later )
RadioButton rb1=(RadioButton)getActivity().findViewById(R.id.radioButton1); //you dont need to do this again if global ...
if(rb1.isChecked() == true) {
//toast ... button has been selected ...
}
}
}
}