0

I have 3 xml source files like this:

XmlPullParser xmlR =  params[0].getXml(R.xml.code_1);
XmlPullParser xmlR =  params[0].getXml(R.xml.code_2);
XmlPullParser xmlR =  params[0].getXml(R.xml.code_3);

And this is my radio button:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/separator2"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <RadioButton android:id="@+id/code_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="code_1"
            android:onClick="onRadioButtonClicked"
            />

        <RadioButton
            android:id="@+id/code_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="code_2"
            android:onClick="onRadioButtonClicked"
             />

        <RadioButton
            android:id="@+id/code_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="onRadioButtonClicked"
            android:text="code_3" />

    </RelativeLayout>

How to select just one of the xml files with radio button? So, when I select one of radio button, the xml code_1 or xml code_2 or xml code_3 will be used.

Thanks.

Bonnie7
  • 19
  • 1
  • 10

2 Answers2

0

First of all, you need to group your radio buttons in a RadioGroup[1] layout, then you'll have to set an OnCheckedChangeListener[2] this way you'll be able to know which radio button is selected at a time.

RadioGroup.OnCheckedChangeListener(RadioGroup group, int checkedId)

The checkedId relates to the selected RadioButton, so you can use your xml based on it.

[1] - https://developer.android.com/reference/android/widget/RadioGroup.html [2] - https://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

ansorod
  • 367
  • 1
  • 5
-1

You need to inflate your xml programatically!!! Just do this and it will work!

Rodrigo Gontijo
  • 587
  • 1
  • 9
  • 21
  • I confuse with java code. Can you give me example for select just one of the xml with java code please.. – Bonnie7 Jun 16 '17 at 18:40
  • read this answer : https://stackoverflow.com/questions/21400415/inflate-layout-programmatically-within-another-layout And please, upvote this answer! – Rodrigo Gontijo Jun 16 '17 at 18:43