Initialize Switch widget in menu during activity OnCreate

I want to initialize the state of the switch I have on my menu. The below code works during the OnCreate BUT then the Switch set check is called at least 2 more times. These calls toggle the state so that my widget is set back from true to false. I show the code below. It seems to be a timing thing but I can’t find where in the Activity lifecycle I should initialize the widget. It’s complicated because the widget is in the menu.xml file and not in the regular activity xml file. Any ideas of how I could fix this? I’ve spent 2.5 hours on it.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list)
// the switch is in a different layout because its layout is included in the menu.xml file
val switchView = layoutInflater.inflate(R.layout.switch_item,null,false)
val showFavs_switch = switchView?.findViewById(R.id.favs_only)
showFavs_switch?.setChecked(true)
// the above lines of code all work. The switch has been set to true for checked. But then
// it gets changed.
}

Here is the switch.java code that is getting called multiple times. Seems like the inflate calls it then maybe the findViewById. I don’t know. It is acting on a different widget than my showFavs_switch because the debug inspector still shows its state for checked is true. So a different view with same ID is being invoked later after my calls in OnCreate.

        public void setChecked(boolean checked) {
    super.setChecked(checked);

    // Calling the super method may result in setChecked() getting called
    // recursively with a different value, so load the REAL value...
    checked = isChecked();

    if (isAttachedToWindow() && isLaidOut()) {
        animateThumbToCheckedState(checked);
    } else {
        // Immediately move the thumb to the new position.
        cancelPositionAnimator();
        setThumbPosition(checked ? 1 : 0);
    }
}

I’ve attached a screenshot to help you get a visual of what I want. The switch SHOULD be set to On.
Favs_switch

1 Like

Set a breakpoint in the debugger and see. findViewById() will not call setChecked(). However, inflating the layout or menu resource would call setChecked(), in addition to any calls in your own code.

Then you will need to figure out where that extra widget is coming from.

We do not know what “the regular activity xml file” contains, what menu.xml contains, and how the two relate to each other.

1 Like

There is no relationship between activity.xml and menu.xml. They only reason I did the layoutinflater.inflate call was to get the view’s reference. Otherwise, it’s not found by the find ViewById call in OnCreate (returns null). I guess I’ll end up making a fragment. There’s so much code that I didn’t want to do that rewrite for such a small thing. But I guess it’s not so small if it’s taken hours of debugging with no progress.

onCreateOptionsMenu is where menu.xml is inflated. I tried the code there and same problem. Initially works but then gets overwritten with subsequent setChecked calls.

That means you created a view and are not using it.

In onCreateOptionsMenu(), after you inflate your menu.xml resource, call findItem() on the Menu to find the MenuItem that contains the Switch. Call getActionView(), which should return your Switch, assuming that your menu resource is set up the way that I am guessing. Use that to configure the Switch. And, most likely, get rid of all LayoutInflater logic that you added that is inflating switch_item.xml.

See:

for examples of using getActionView(), in my case to configure a SearchView.

2 Likes

Thank you! I hadn’t run across ActionView in any books so didn’t know about it. Works now.