Search code examples
androidxamarintogglebuttonchecked

Updating togglebutton cheched state gives error


i have this ToggleButton

  <ToggleButton
   android:id="@+id/PlaymusicToggleButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:textOn=""
            android:textOff=""
            android:layout_weight="0"
            android:background="@drawable/mediabutton"
            android:layout_marginRight="10dp"
            android:layout_centerVertical="true" />

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/b_st"  /> //currently pressed turning the toggle on
<item android:state_pressed="true" android:drawable="@drawable/b_st" /> //currently pressed turning the toggle off
<item android:state_checked="true" android:drawable="@drawable/b_st" /> //not pressed default checked state
<item android:drawable="@drawable/b_pl" /> //default non-pressed non-checked

and im trying to update the checked value from a function

    private void UpdatePlayer(int _music){


        if (_music== 0) 
        {
            music.Playing = true;
            //playmusicToggleButton.RequestLayout ();
            playmusicToggleButton.Checked = true;

but im getting this strange error

  [ResourceType] No package identifier when getting value for resource number 0x000000

and the rest of the UpdatePlayer doesn't load, what is this error about?

I have defined togglebutton on the beggining of the fragment

  public class ControlPanel : ListFragment, SeekBar.IOnSeekBarChangeListener
  {
    ToggleButton playmusicToggleButton;

and im assigned it with a value on OnCreateView

playmusicToggleButton = view.FindViewById<ToggleButton> (Resource.Id.PlaymusicToggleButton);

Solution

  • Try doing like this:

    playmusicToggleButton = (ToggleButton) findViewById(R.id.playmusicToggleButton);              
    playmusicToggleButton.setChecked(true);