tI have 2 radio buttons, 1 is defaulted to start as true, when app loads.
If 1 is selected it hides table 2. and if Radiobutton 2 is selected it hides table 1.
This works as intended except…
The problem I am having is, when the app first loads Radiobutton 1 is checked, but both tables are open, until i select radiobutton 2 then it starts working. So I am missing something in my initial load.
A nudge in the right direction would be greatly appreciated.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Slab extends Activity {
private RadioGroup RadioMeat;
/** Called when the activity is first created. */
//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slab);
RadioMeat = (RadioGroup) findViewById(R.id.rgMeat);
RadioMeat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rbBeef :
if (checkedId == R.id.rbBeef) {
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
tl.setVisibility(View.VISIBLE);
TableLayout tl1 = (TableLayout)findViewById(R.id.tableLayout2);
tl1.setVisibility(View.GONE);
}
break;
case R.id.rbPork :
if (checkedId == R.id.rbPork) {
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout2);
tl.setVisibility(View.VISIBLE);
TableLayout tl1 = (TableLayout)findViewById(R.id.tableLayout1);
tl1.setVisibility(View.GONE);
}
break;
}
}
});
}
}
XML
<RadioGroup
android:id="@+id/rgMeat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/rbBeef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Beef"
android:checked="true"/>
<RadioButton
android:id="@+id/rbPork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Pork" />
</RadioGroup>
The problem you are facing is that at the time of start, when your onCreate() is called first, onCheckedChanged() is never called. It is called only when you check it. It would have also been called had you checked it programatically rather than do it through xml. So you could do it 2 ways,
in your on create()
Call
or Call