Reputation:
I have checkboxes that can be checked. However if these are checked I would like to have them all unchecked if the screen orientation has changed. How can I go about doing something like that?
Upvotes: 0
Views: 97
Reputation:
I partially solved the problem by overriding onConfigurationChanged
by checking if the orientation changed and if so checkbox.toggle()
. Only problem that still persists is that now when the orientation changes and checkboxes are clicked they don't set the visibility of the objects in my code as opposed to before the configuration change.
Upvotes: 0
Reputation: 41
Try this,
//step 1 use this methode that loops al layout looking for checkboxes and reset them void unCheckAllCheckBoxs(ViewGroup youRootView) {
for (int i = youRootView.getChildCount() - 1; i >= 0; i--) {
final View child = youRootView.getChildAt(i);
if (child instanceof ViewGroup) {
unCheckAllCheckBoxs((ViewGroup) child);
} else {
if ((child != null) && (child instanceof CheckBox)) {
CheckBox checkBox = (CheckBox) child;
checkBox.setSelected(false);
}
}
}
}
//step 2 overrige onConfigurationChanged methode to detect orientation changing and call the previous pethode inside passing that parent you root layout that contains all the checkbox as parameter.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
unCheckAllCheckBoxs((ViewGroup) findViewById(R.id.root));
}
let me now if it works .
Upvotes: 0
Reputation: 12605
The onCreate()
callback method will be called with savedInstanceState object so you can check if it's not null then uncheck all the checkboxes.
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
if(savedInstanceState != null) {
// uncheck all of the checkboxes.
}
}
Upvotes: 1