Get Started with Radio Button – UI Widget

Hello folks ! working with another UI widget in android application is very easy task. Radio Button is used to quick select and select the choice between two or three. So, just start with Radio Button coding parts . I will show you complete registration form using sqlite later on with my tutorial series. How to make a proper animated registration form I will show you later. But first you should learn UI widgets so that you can fit according to your need.

Tip – While using Radio Button you should use RadioGroup in xml ,that’s a good practice for being a developer .

Now , working with MainActivity I am going to give the file name is MyAndroidAppActivity.java.In the Java file I have just defined the below line i.e.

MainJavafile

1

intselectedId=radioSex_Group.getCheckedRadioButtonId();

MainJavafile

1

radioSex_Button=(RadioButton)findViewById(selectedId);

MainJavafile

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

50

51

52

53

packageandroidarena.radiobutton;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

publicclassMyAndroidAppActivityextendsActivity{

privateRadioGroup radioSex_Group;

privateRadioButton radioSex_Button;

privateButton btn_Display;

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

checkbutton();

}

publicvoidcheckbutton(){

radioSex_Group=(RadioGroup)findViewById(R.id.group_radioSex);

btn_Display=(Button)findViewById(R.id.btnDisplay);

btn_Display.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

// get selected radio button from radioGroup

intselectedId=radioSex_Group.getCheckedRadioButtonId();

// find the radiobutton by returned id

// you can do any operation here like send values on server or save values on sqlite

radioSex_Button=(RadioButton)findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this,

radioSex_Button.getText(),2000).show();

}

});

}

}

Ouput – This is the quick and simple Radio Button in android application. You guys can include these two lines as I above mentioned. and can have the access in your app. For more query and suggestions please comment here . 😛