rusty009
rusty009

Reputation: 876

Only 1/2 of my radio buttons are working in android

this is my XML,

            <RadioGroup
            android:id="@+id/radioType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            >

            <RadioButton
                android:id="@+id/RadioAuction"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55dp"
                android:textColor="#3DCC00"
                android:onClick="showAuctionOptions"
                android:text="@string/RadioButton1" />

            <RadioButton
                android:id="@+id/RadioBin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:onClick="showAuctionOptions"
                android:textColor="#FF0000"
                android:text="Buy it now" />

        </RadioGroup>

and this is my showAuctionOptions function,

    public void showAuctionOptions(View v){

    switch(v.getId()){

    //execute this case if auction button is checked
        case R.id.RadioAuction:
            //Display start price
            LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
            ShowPrice.setVisibility(View.VISIBLE);

            //Display reserve price
            LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
            ShowReservePrice.setVisibility(View.VISIBLE); 

            //Hide quantity
            LinearLayout HideQuantity = (LinearLayout)findViewById(R.id.LayoutQuantity);
            HideQuantity.setVisibility(View.GONE);
        break;

        //execute this case if buy it now button is checked
        case R.id.RadioBin:
            //Hide start price
            LinearLayout HidePrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
            HidePrice.setVisibility(View.GONE);

            //Hide reserve price
            LinearLayout HideReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
            HideReservePrice.setVisibility(View.GONE);

            //Display quantity
            LinearLayout DisplayQuantity = (LinearLayout)findViewById(R.id.LayoutQuantity);
            DisplayQuantity.setVisibility(View.VISIBLE);
        break;

    }


}

My problem is, that it only displays the options when the auction radio button is pressed. Nothing happens when the Buy it now is pressed. Can anyone help? Thanks !

Upvotes: 1

Views: 128

Answers (2)

Squonk
Squonk

Reputation: 48871

Don't use an 'onClick' listener for RadioButtons. Try using RadioGroup.OnCheckedChangeListener instead. I don't think you can assign it in the XML layout file in the way you've done with android:onClick however.

Have your Activity implement the interface as follows...

public class MyActivity extends Activity
    implements RadioGroup.OnCheckedChangeListener {
    ...

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ...
    }
}

You'll then need to find the RadioGroup and set the listener...

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioType);
radioGroup.setOnCheckedChangeListener (this);

Upvotes: 1

user932887
user932887

Reputation:

Have you set the onclick listener for RadioBin?

RadioButton radioBin = (RadioButton) findViewById(R.id.RadioBin);
radioBin.setOnClickListener(...); 

Just like you did for RadioAuction? Seems to me that there's no listener for RadioBin.

Upvotes: 0

Related Questions