Write a Program to change background color using Radio Button in android
In this Example, There are three radio button use in this program. On click of radio button background color of view will be changed as per name. For ex. on yellow radio button select background color will be change and yellow color set. Here, design file and code file is describe here and output of the program also display in below.
There are RadioGroup is used in the example. RadioGroup is used for radio button in group format. All radio buttons are used under RadioGroup.
File Name : MainActivity.javaThere are RadioGroup is used in the example. RadioGroup is used for radio button in group format. All radio buttons are used under RadioGroup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.example.radiobtn; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.widget.RadioGroup; public class MainActivity extends Activity { RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg=(RadioGroup)findViewById(R.id.radioGroup1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { View root=v.getRootView(); switch(rg.getCheckedRadioButtonId()) { case R.id.radio0: root.setBackgroundColor(Color.YELLOW); break; case R.id.radio1: root.setBackgroundColor(Color.MAGENTA); break; case R.id.radio2: root.setBackgroundColor(Color.CYAN); break; } } } |
File Name : activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/yellow" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/magenta" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/cyan" /> </RadioGroup> </RelativeLayout> |
Output :
Android Tutorial, Change Background Color using android, Gtu Android Practical, Radio Button Example in android, Write a Program to change background color using Radio Button in android
String file
ReplyDelete