Android Radio Button And Radio Group 1

Android Development

Android Radio Button And Radio Group 1 :


⇛ layout > .xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".First"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
android:id="@+id/linearlayout">

<!--RadioGrup for RadioButtons-->

<RadioGroup
android:id="@+id/Idradiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton

android:id="@+id/Idradiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="red"/>

<RadioButton

android:id="@+id/Idradiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"/>

<RadioButton

android:id="@+id/Idradiobutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"/>

<RadioButton

android:id="@+id/Idradiobutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow"/>

<RadioButton

android:id="@+id/Idradiobutton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="White"/>

<RadioButton

android:id="@+id/Idradiobutton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>

<RadioButton

android:id="@+id/Idradiobutton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black"/>

</RadioGroup>
</LinearLayout>

⇛ java file :

package com.example.dell.radiobuttonandradiogroup;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class First extends AppCompatActivity {

private RadioGroup rg1;
LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

rg1 = (RadioGroup) findViewById(R.id.Idradiogroup1);

linearLayout = (LinearLayout) findViewById(R.id.linearlayout);

/*When click on any RadioButton, the listener will execute*/
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId){

/*If red is seected*/
case R.id.Idradiobutton1:

linearLayout.setBackgroundColor(Color.RED);

break;

/*If blue is selected*/

case R.id.Idradiobutton2:

linearLayout.setBackgroundColor(Color.BLUE);

break;

/*If green is selected*/

case R.id.Idradiobutton3:

linearLayout.setBackgroundColor(Color.GREEN);

break;

case R.id.Idradiobutton4:

linearLayout.setBackgroundColor(Color.YELLOW);

break;

case R.id.Idradiobutton6:

linearLayout.setBackgroundColor(Color.WHITE);

break;

case R.id.Idradiobutton7:

linearLayout.setBackgroundColor(Color.BLACK);

break;
}
}
});
}
}

No comments

Powered by Blogger.