Reputation: 8978
I'm trying to recieve checkbox choices inside my Fragment class, however I'm not able to use the way I've used in Activities (calling android:onClick="onCheckboxClicked"
method from a layout.xml
), because I get the "method onCheckboxClicked not found" everytime this method is called.
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case (R.id.checkBox_wifi):
if (checked) {
editor.putBoolean("wifi_on_off", true);
}
else {
editor.putBoolean("wifi_on_off", false);
}
break;
}
editor.apply();
}
So, I've implemented the View.OnClickListener, but this doesn't get any checkbox checks/unchecks:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.checkBox_wifi:
boolean checked = ((CheckBox) view).isChecked();
if (checked) {
editor.putBoolean("wifi_on_off", true);
} else {
editor.putBoolean("wifi_on_off", false);
}
editor.apply();
break;
}
}
Upvotes: 3
Views: 11198
Reputation: 2046
public class MyFragment extends Fragment {
CheckBox myCheckBox;
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle saveInstanceState) {
rootView = layoutInflater.inflate(R.layout.your_fragment_layout, container, false);
...
...
initViews();
return rootView;
}
private void initViews() {
myCheckBox = (CheckBox) rootView.findViewById(R.id.my_check_box);
myCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked)
Log.d("TAG=>isChecked", "True");//replace your own stuffs here
else
Log.d("TAG=>isChecked", "false");//replace your own stuffs here
}
});
}
}
You can set this listener for n number of checkbox!!!
Upvotes: 2
Reputation: 3970
Just use your custom Listener
CompoundButton.OnCheckedChangeListener
inside any other view then Activity. Below code sample
public class MyFragment extends Fragment{
public View onCreateView(...){
...
CheckBox cbFilter = (CheckBox) rootView.findViewById(R.id.chb_rt_is_filter);
cbFilter.setOnCheckedChangeListener(myCheckboxListener);
return rootView;
}
private CompoundButton.OnCheckedChangeListener myCheckboxListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.chb_rt_is_filter:
break;
default:
break;
}
}
};
}
Upvotes: 0
Reputation: 1018
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentlayout, container,false);
CheckBox checkboxvariable=(CheckBox)rootView.findViewById(R.id.checkboxid);
checkboxvariable.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(v.isChecked()){ //do this}
else { //do this }
}
});
return rootView;
}
checkboxvariable.setOnClickListener(this.getActivity());
inside onClick you can get the resource id, R.id.checkboxid.
Upvotes: 5
Reputation: 14710
Any onClick
XML attribute will mean that the method must be found in the activity, not in the fragment, not in the View. So the way I see it, you have two options:
onClick
and rely on OnClickListeners
that you define in Java code. This subject was already discussed in at least this thread.Upvotes: 2