Radio Buttons Assignment
Muhammad Shehzaib Khalid
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Butons Assignment"
android:textColor="#C70039"
android:gravity="center"
android:textSize="30dp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Gender"
android:layout_weight="1"
android:gravity="center"
android:textSize="15dp"
android:textColor="#000000"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_weight="1"
android:id="@+id/button1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_weight="1"
android:id="@+id/button2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"
android:layout_weight="1"
android:id="@+id/button3"/>
</RadioGroup>
</LinearLayout>
Java Code
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
RadioButton button1;
RadioButton button2;
RadioButton button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link]([Link].button1);
[Link]([Link].button2);
[Link]([Link].button3);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Gender Male is Selected",
Toast.LENGTH_SHORT).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Gender Female is selected",
Toast.LENGTH_SHORT).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Gender Other is selected",
Toast.LENGTH_SHORT).show();
}
});
}
}