How to Create Radio Button as selecting Male Female android
In this article, today radio button onclickListner as a select Male or female. so follow this code.
follow the method as the radio button onClikcListner
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rdmale.isChecked()==true){
Toast.makeText(MainActivity.this,"You are select Male",Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this,"You are select Female",Toast.LENGTH_SHORT).show();
}
});
}
1.MainActivity.java:
package com.akash.radiobutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup rdGroup;
RadioButton rdmale,rdfemale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rdGroup=(RadioGroup)findViewById(R.id.rdGroup);
rdmale=(RadioButton)findViewById(R.id.rdmale);
rdfemale=(RadioButton)findViewById(R.id.rdfemale);
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rdmale.isChecked()==true){
Toast.makeText(MainActivity.this,"You are select Male",Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this,"You are select Female",Toast.LENGTH_SHORT).show();
}
});
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Gender"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Male"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Female"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
</RelativeLayout>
3.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.radiobutton">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.strings.xml:
<resources>
<string name="app_name">Radio Button</string>
</resources>
No comments:
Post a Comment