In layout,We can add android:checked="true" to CheckBox you want to be selected
Programmatically, We can use the setChecked method defined in the checkable interface
In Radiobutton, for checking the radiobutton with setChecked() is not changing the state inside the RadioGroup. For example this method from the radioGroup will return a wrong result: getCheckedRadioButtonId()
MainActivity[MainActivity.class]
package com.androidfeeders.radiobuttonsample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RadioButton genderMale, genderFemale;
String selectedGender;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
genderMale = (RadioButton) findViewById(R.id.genderMale);
genderFemale = (RadioButton) findViewById(R.id.genderFemale);
submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (genderMale.isChecked()) {
selectedGender = genderMale.getText().toString();
} else if (genderFemale.isChecked()) {
selectedGender = genderFemale.getText().toString();
}
// Toast message for selected Gender
Toast.makeText(getApplicationContext(), "Selected Gender :"+selectedGender, Toast.LENGTH_SHORT).show();
}
}
Layout Resource[activity_main.xml]
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/layout"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register page"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:layout_width="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/genderMale"
android:layout_width="200dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true"
android:text="Male"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/genderFemale"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Female"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/colorAccent"
android:padding="10dp"
android:text="Submit"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>->
</LinearLayout>
Gradle Scripts~build.gradle[Module:app]
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.androidfeeders.radiobuttonsample"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Gradle Scripts~build.gradle[Project:RadioButtonSample]
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Resource[strings.xml]
<resources>
<string name="app_name">RadioButtonSample</string>
</resources>
manifests
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidfeeders.radiobuttonsample">
<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>
I started to learn android and Kotlin and iOS and Hybrid application and i cant code IOS very well. But i love more interesting to code and finding new developer minds who are join to me You're welcome to keep on using my website.Instead, you can join our FB page for developer comments and post there: https://www.facebook.com/ramasamy.m.779 Best wishes :) Ramasamy(Software Developer)
See what's new in Android & IOS & Hybrid development … explore and learn in site
Ramasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
Reply