Radio Button Assignment
Radio Button Assignment
<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 com.example.radiobuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.findViewById(R.id.button1);
button2.findViewById(R.id.button2);
button3.findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Male is Selected",
Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Female is selected",
Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Other is selected",
Toast.LENGTH_SHORT).show();
}
});
}
}