Question

In my XML, I have a relative layout with a toggle button, and 2 groups of radio buttons.

Right now it's set so that when the radio button "spells" in the first radio button group is selected, it should bring the radio button group "spellButtons" up to the front.

However, that only works if I have touched the toggle button at least once. Ideally, I shouldn't have to touch the toggle button for my onClick code to work, especially since I intend to make the toggle button a pause button eventually.

Here's my XML:

    RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    tools:context=".MainActivity"
     >

    <ToggleButton
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Toggle Yo" />

    <RadioGroup
        android:id="@+id/combatSwitchButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/spells"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />

        <RadioButton
            android:id="@+id/summons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />

        <RadioButton
            android:id="@+id/power"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/spellButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/spell1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="hi" 
            android:textColor="@android:color/black"/>

        <RadioButton
            android:id="@+id/spell2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/spell3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />
        <RadioButton
            android:id="@+id/spell4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/drone1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/drone2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/combatSwitchButtons"
        android:layout_centerVertical="true" >

    </RadioGroup>

</RelativeLayout>

And here's my code

        findViewById(R.id.combatSwitchButtons).bringToFront();
    findViewById(R.id.pause).bringToFront();

    //this sets up the toggle button which will become a pause button
    findViewById(R.id.pause).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {               
            //TODO: Pause the game
        }
    });

    //This is for if we click spells on the switch interface. It'll make the spell buttons appear
    findViewById(R.id.spells).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   
            //change the mode
            game.spells=false;
            game.power=false;
            game.summons=true;

            //add the spells buttons and the drones
            if(findViewById(R.id.spellButtons).isSelected())
                            {
                findViewById(R.id.spellButtons).bringToFront();
                                    System.out.println("brought it to the front");
                            }
            else{
                findViewById(R.id.spellButtons).setVisibility(View.INVISIBLE);
            }   

            //TODO: remove other buttons
        }
    });
Was it helpful?

Solution 2

Apparently I needed to bring the spellButton radio button group up to the front and set it to invisible before I ever entered the onclick listener. In the onclick listener, I could use the setVisibility(View.Visible).

My code ended up looking like

    findViewById(R.id.combatSwitchButtons).bringToFront();
    findViewById(R.id.pause).bringToFront();
    findViewById(R.id.spellButtons).bringToFront(); 
    findViewById(R.id.spellButtons).setVisibility(View.GONE);

    //this sets up what each button does.
    findViewById(R.id.pause).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {               
            //TODO: Pause the game
        }
    });

    /*ALLL the on click listeners*/
    //This is for if we click spells on the switch interface. It'll make the spell buttons appear
    findViewById(R.id.spells).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   
            //change the mode
            game.spells=false;
            game.power=false;
            game.summons=true;

            //add the spells buttons and the drones
                findViewById(R.id.spellButtons).setVisibility(View.VISIBLE);    
    //TODO: remove other buttons
        }
    });

OTHER TIPS

In your onClickListener for spells your checking if your RadioGroup spellButtons isSelected(). If you want it to show up when you click on the spells RadioButton you dont need that if loop. Instead of using bringToFront why dont you use setVisibility()?

Something like this:

findViewById(R.id.spells).setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {   
        //change the mode
        game.spells=false;
        game.power=false;
        game.summons=true;

        //add the spells buttons and the drones
        ((RadioGroup) findViewById(R.id.spellButtons)).setVisibility(View.VISIBLE);
        System.out.println("brought it to the front");
    }
});

And if you want to hide it when another radiobutton is clicked just setOnClickListener for that and make spellButtons visibility Invisible or Gone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top