0% found this document useful (0 votes)
29 views

Android Ui

The document discusses the TextView and EditText widgets in Android. It provides code examples for defining these widgets in XML layout files and programmatically in Java. Key attributes of TextView like id, text, gravity, textColor are described. For EditText, additional attributes like hint and ways to get user-entered text values are covered. Common properties between TextView and EditText are noted, with EditText being a subclass of TextView.

Uploaded by

ALMAS NAZAKAT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Android Ui

The document discusses the TextView and EditText widgets in Android. It provides code examples for defining these widgets in XML layout files and programmatically in Java. Key attributes of TextView like id, text, gravity, textColor are described. For EditText, additional attributes like hint and ways to get user-entered text values are covered. Common properties between TextView and EditText are noted, with EditText being a subclass of TextView.

Uploaded by

ALMAS NAZAKAT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

TextView

code in XML:

<TextView android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid" />

TextView code in JAVA:

TextView textView = (TextView) findViewById(R.id.textView);


textView.setText("AbhiAndroid"); //set text for text view

Attributes of TextView:

Now let’s we discuss about the attributes that helps us to configure a TextView in
your xml file.
1. id: id is an attribute used to uniquely identify a text view. Below is the example code
in which we set the id of a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

2. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text like left, right, center, top, bottom, center_vertical,
center_horizontal etc.
Below is the example code with explanation included in which we set the
center_horizontal gravity for text of a TextView.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:textSize="20sp"
android:gravity="center_horizontal"/> <!--center horizontal gravity-->

3. text: text attribute is used to set the text in a text view. We can set the text in xml as
well as in the java class.
Below is the example code with explanation included in which we set the text
“AbhiAndroid” in a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="AbhiAndroid"/><!--Display Text as AbhiAndroid-->
In Java class:
Below is the example code in which we set the text in a textview programmatically
means in java class.

TextView textView = (TextView)findViewById(R.id.textView);


textView.setText("AbhiAndroid"); //set text for text view

4. textColor: textColor attribute is used to set the text color of a text view. Color value is
in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
Below is the example code with explanation included in which we set the red color for
the displayed text.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="25sp"
android:textColor="#f00"/><!--red color for text view-->

In Java class:
Below is the example code in which we set the text color of a text view
programmatically means in java class.
TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextColor(Color.RED); //set red color for text view

5. textSize: textSize attribute is used to set the size of text of a text view. We can set the
text size in sp(scale independent pixel) or dp(density pixel).
Below is the example code in which we set the 20sp size for the text of a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="40sp" /><!--Set size-->

In Java class:
Below is the example code in which we set the text size of a text view programmatically
means in java class.

TextView textView = (TextView)findViewById(R.id.textView);


textView.setTextSize(20); //set 20sp size of text

6. textStyle: textStyle attribute is used to set the text style of a text view. The possible
text styles are bold, italic and normal. If we need to use two or more styles for a text
view then “|” operator is used for that.
Below is the example code with explanation included in which we set the bold and italic
text styles for text.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="40sp"
android:textStyle="bold|italic"/><!--bold and italic text style of text-->

7. background: background attribute is used to set the background of a text view. We


can set a color or a drawable in the background of a text view.
8. padding: padding attribute is used to set the padding from left, right, top or bottom.
In above example code of background we also set the 10dp padding from all the side’s
of text view.
Below is the example code with explanation included in which we set the black color for
the background, white color for the displayed text and set 10dp padding from all the
side’s for text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="40sp"
android:padding="10dp"
android:textColor="#fff"
android:background="#000"/> <!--red color for background of text view-->

In Java class:
Below is the example code in which we set the background color of a text view
programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);


textView.setBackgroundColor(Color.BLACK);//set background color

EditText
Important Note: An EditText is simply a thin extension of a TextView. An EditText
inherits all the properties of a TextView.
EditText Code:
We can create a EditText instance by declaring it inside a layout(XML file) or by
instantiating it programmatically (i.e. in Java Class).
EditText code in XML:

<EditText
android:id="@+id/simpleEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

Retrieving / Getting the Value From EditText In Java Class:


Below is the example code of EditText in which we retrieve the value from a EditText
in Java class. We have used this code in the example you will find at the end of this post.

EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);


String editTextValue = simpleEditText.getText().toString();

Attributes of EditText:
Now let’s we discuss few attributes that helps us to configure a EditText in your xml.
1. id: id is an attribute used to uniquely identify a text EditText. Below is the example
code in which we set the id of a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

2. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text like left, right, center, top, bottom, center_vertical,
center_horizontal etc.
Below is the example code with explanation included in which we set the right gravity
for text of a EditText.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Email"
android:gravity="right"/><!--gravity of a edit text-->
3. text: text attribute is used to set the text in a EditText. We can set the text in xml as
well as in the java class.
Below is the example code in which we set the text “Username” in a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Username"/><!--set text in edit text-->
Setting text in EditText In Java class:
Below is the example code in which we set the text in a text view programmatically
means in java class.

EditText editText = (EditText)findViewById(R.id.simpleEditText);


editText.setText("Username");//set the text in edit text

4. hint: hint is an attribute used to set the hint i.e. what you want user to enter in
this edit text. Whenever user start to type in edit text the hint will automatically
disappear.
Below is the example code with explanation in which we set the hint of a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter Your Name Here" /><!--display the hint-->
Setting hint in EditText In Java class:
Below is the example code in which we set the text in a text view programmatically
means in java class.

EditText editText = (EditText)findViewById(R.id.simpleEditText);


editText.setHint("Enter Your Name Here");//display the hint

5. textColor: textColor attribute is used to set the text color of a text edit text. Color
value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
Below is the example code with explanation included in which we set the red color for
the displayed text of a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Password"
android:textColor="#f00"/><!--set the red text color-->

Setting textColor in EditText In Java class:


Below is the example code in which we set the text color of a edit text programmatically
means in java class.

EditText simpleEditText=(EditText)findViewById(R.id.simpleEditText);
simpleEditText.setTextColor(Color.RED);//set the red text color

6. textColorHint: textColorHint is an attribute used to set the color of displayed hint.


Below is the example code with explanation included in which we set the green color for
displayed hint of a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter Your Name Here"
android:textColorHint="#0f0"/><!--set the hint color green-->

Setting textColorHint in EditText In Java class:


Below is the example code in which we set the hint color of a edit text programmatically
means in java class.

EditText simpleEditText=(EditText)findViewById(R.id.simpleEditText);
simpleEditText.setHintTextColor(Color.green(0));//set the green hint color

7. textSize: textSize attribute is used to set the size of text of a edit text. We can set the
text size in sp(scale independent pixel) or dp(density pixel).
Below is the example code in which we set the 25sp size for the text of a edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textSize="25sp" /><!--set 25sp text size-->

Setting textSize in EditText in Java class:


Below is the example code in which we set the text size of a edit text programmatically
means in java class.

EditText simpleEditText=(EditText)findViewById(R.id.simpleEditText);
simpleEditText.setTextSize(25);//set size of text

8. textStyle: textStyle attribute is used to set the text style of a edit text. The possible
text styles are bold, italic and normal. If we need to use two or more styles for a edit
text then “|” operator is used for that.
Below is the example code with explanation included, in which we set the bold and italic
text styles for text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Email"
android:textSize="25sp"
android:textStyle="bold|italic"/><!--set bold and italic text style-->

9. background: background attribute is used to set the background of a edit text. We


can set a color or a drawable in the background of a edit text.
Below is the example code with explanation included in which we set the black color for
the background, white color for the displayed hint and set 10dp padding from all the
side’s for edit text.

<EditText
android:id="@+id/simpleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter Your Name Here"
android:padding="15dp"
android:textColorHint="#fff"
android:textStyle="bold|italic"
android:background="#000"/><!--set background color black-->

Setting Background in EditText In Java class:


Below is the example code in which we set the background color of a edit text
programmatically means in java class.

EditText simpleEditText=(EditText)findViewById(R.id.simpleEditText);
simpleEditText.setBackgroundColor(Color.BLACK);//set black background color

10. padding: padding attribute is used to set the padding from left, right, top or bottom.
In above example code of background we also set the 10dp padding from all the side’s
of edit text.

Button
Button code in XML:
The below code will create Button and write “Abhi Android” text on it.

<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abhi Android"/>

Attributes of Button in Android:


Now let’s we discuss some important attributes that helps us to configure a Button in
your xml file (layout).
1. id: id is an attribute used to uniquely identify a text Button. Below is the example
code in which we set the id of a Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abhi Android"/>

2. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text like left, right, center, top, bottom, center_vertical,
center_horizontal etc.
Below is the example code with explanation included in which we set the right and
center vertical gravity for text of a Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Abhi Android"
android:layout_centerInParent="true"
android:gravity="right|center_vertical"/><!--set the gravity of button-->
3. text: text attribute is used to set the text in a Button. We can set the text in xml as
well as in the java class.
Below is the example code with explanation included in which we set the text “Learning
Android @ AbhiAndroid” in a Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Learn Android @ AbhiAndroid"/><!--display text on button-->

Setting Text Using Java class:


Below is the example code in which we set the text on Button programmatically means
in java class. The output will be same as the above.

Button button = (Button) findViewById(R.id.simpleButton);


button.setText("Learn Android @ AbhiAndroid");//set the text on button

4.textColor: textColor attribute is used to set the text color of a Button. Color value is in
the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
Below is the example code with explanation included in which we set the red color for
the displayed text of a Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textColor="#f00"/><!--red color for the text-->

Setting Text Color On Button Inside Java class:


Below is the example code in which we set the text color of a Button programmatically
means in java class.

Button simpleButton=(Button) findViewById(R.id.simpleButton);


simpleButton.setTextColor(Color.RED);//set the red color for the text
5. textSize: textSize attribute is used to set the size of the text on Button. We can set the
text size in sp(scale independent pixel) or dp(density pixel).
Below is the example code in which we set the 25sp size for the text of a Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textSize="25sp" /><!--25sp text size-->

Setting textSize In Java class:


Below is the example code in which we set the text size of a Button programmatically
means in java class.

Button simpleButton=(Button)findViewById(R.id.simpleButton);
simpleButton.setTextSize(25);//set the text size of button

6. textStyle: textStyle attribute is used to set the text style of a Button. The possible text
styles are bold, italic and normal. If we need to use two or more styles for a Button then
“|” operator is used for that.
Below is the example code with explanation included, in which we set the bold and italic
text styles for text of a button.
<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="AbhiAndroid"
android:textSize="20sp"
android:textStyle="bold|italic"/><!--bold and italic text style-->

7. background: background attribute is used to set the background of a Button. We can


set a color or a drawable in the background of a Button.
Below is the example code in which we set the gren color for the background, Black
color for the displayed text and set 15dp padding from all the side’s for Button.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Download"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:background="#147D03" /><!--Background green color-->
Setting background in Button In Java class:
Below is the example code in which we set the background color of a Button
programmatically means in java class.

Button simpleButton=(Button)findViewById(R.id.simpleButton);
simpleButton.setBackgroundColor(Color.BLACK);//set the black color of button
background

8. padding: padding attribute is used to set the padding from left, right, top or bottom.
In above example code of background we also set the 10dp padding from all the side’s
of button.
9. drawableBottom: drawableBottom is the drawable to be drawn to the below of the
text.
Below is the example code in which we set the icon to the below of the text.
Make sure you have image saved in your drawable folder name ic_launcher.

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#147D03"
android:text="Download Code"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:drawableBottom="@drawable/ic_launcher"/><!--image drawable on button-->

10. drawableTop, drawableRight And drawableLeft: Just like the above attribute we
can draw drawable to the left, right or top of text.
In the Below example we set the icon to the right of the text. In the same way you can
do for other two attribute by your own:

<Button
android:id="@+id/simpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#147D03"
android:text="Download Code"
android:textSize="20sp"
android:padding="15dp"
android:textStyle="bold|italic"
android:drawableRight="@drawable/ic_launcher"/><!--image drawable on Right side
of Text on button-->
ImageView
Below is an ImageView code in XML:
Make sure to save lion image in drawable folder

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" />

Attributes of ImageView:
Now let’s we discuss some important attributes that helps us to configure a ImageView
in your xml file.
1. id: id is an attribute used to uniquely identify a image view in android. Below is the
example code in which we set the id of a image view.

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

2. src: src is an attribute used to set a source file or you can say image in your imageview
to make your layout attractive.
Below is the example code in which we set the source of a imageview lion which is
saved in drawable folder.

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" /><!--set the source of an image view-->

In Java:
We can also set the source image at run time programmatically in java class. For that we
use setImageResource() method as shown in below example code.

/*Add in Oncreate() funtion after setContentView()*/


ImageView simpleImageView=(ImageView) findViewById(R.id.simpleImageView);
simpleImageView.setImageResource(R.drawable.lion);//set the source in java class
3. background: background attribute is used to set the background of a ImageView. We
can set a color or a drawable in the background of a ImageView.
Below is the example code in which we set the black color in the background and an
image in the src attribute of image view.

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion"
android:background="#000"/><!--black color in background of a image view-->

In Java:
We can also set the background at run time programmatically in java class. In below
example code we set the black color in the background of a image view.

/*Add in Oncreate() funtion after setContentView()*/


ImageView simpleImageView=(ImageView) findViewById(R.id.simpleImageView);
simpleImageView.setBackgroundColor(Color.BLACK);//set black color in background of a
image view in java class

4. padding: padding attribute is used to set the padding from left, right, top or bottom
of the Imageview.

 paddingRight: set the padding from the right side of the image view.
 paddingLeft: set the padding from the left side of the image view.
 paddingTop: set the padding from the top side of the image view.
 paddingBottom: set the padding from the bottom side of the image view.
 padding: set the padding from the all side’s of the image view.

Below is the example code of padding attribute in which we set the 30dp padding from
all the side’s of a image view.

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/lion"
android:padding="30dp"/><!--set 30dp padding from all the sides-->

5. scaleType: scaleType is an attribute used to control how the image should be re-sized
or moved to match the size of this image view. The value for scale type attribute can be
fit_xy, center_crop, fitStart etc.
Below is the example code of scale type in which we set the scale type of image view to
fit_xy.

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion"
android:scaleType="fitXY"/><!--set scale type fit xy-->

Let’s we take an another example of scale type to understand the actual working of
scale type in a image view.
In below example code we set the value for scale type “fitStart” which is used to fit the
image in the start of the image view as shown below:

<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion"
android:scaleType="fitStart"/><!--set scale type fit start of image view-->
CheckBox
CheckBox code in XML:

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"/>

Important Note: You can check the current state of a check box programmatically by
using isChecked() method. This method returns a Boolean value either true or false, if
a check box is checked then it returns true otherwise it returns false. Below is an
example code in which we checked the current state of a check box.
//initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);

//check current state of a check box (true or false)


Boolean checkBoxState = simpleCheckBox.isChecked();

Attributes of CheckBox:
Now let’s we discuss important attributes that helps us to configure a check box
in XML file (layout).
1.id: id is an attribute used to uniquely identify a check box. Below we set the id of a
image button.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"/>

2. checked: checked is an attribute of check box used to set the current state of a check
box. The value should be true or false where true shows the checked state and false
shows unchecked state of a check box. The default value of checked attribute is false.
We can also set the current state programmatically.
Below is an example code in which we set true value for checked attribute that sets the
current state to checked.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"
android:checked="true"/> <!--set the current state of the check box-->
Setting Current State Of CheckBox In Java Class:
Below we set the current state of CheckBox in java class.

/*Add in Oncreate() funtion after setContentView()*/


// initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);

// set the current state of a check box


simpleCheckBox.setChecked(true);

3. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text in CheckBox like left, right, center, top, bottom, center_vertical,
center_horizontal etc.
Below we set the right and center_vertical gravity for the text of a check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Simple CheckBox"
android:checked="true"
android:gravity="right|center_vertical"/> <!-- gravity of the check box-->
4. text: text attribute is used to set the text in a check box. We can set the text in xml as
well as in the java class.
Below is the example code with explanation included in which we set the text “Text
Attribute Of Check Box” for a check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Text Attribute Of Check Box"/> <!--displayed text of the check
box-->

Setting text in CheckBox In Java class:


Below is the example code in which we set the text of a check box programmatically
means in java class.

/*Add in Oncreate() funtion after setContentView()*/


// initiate check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);

// displayed text of the check box


simpleCheckBox.setText("Text Attribute Of Check Box");

5. textColor: textColor attribute is used to set the text color of a check box. Color value
is in form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
Below we set the red color for the displayed text of a check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text is Red Color"
android:textColor="#f00"
android:checked="true"/> <!-- red color for the text of check box-->

Setting textColor in CheckBox In Java class:


Below we set the text color of a check box programmatically.

/*Add in Oncreate() funtion after setContentView()*/


//initiate the checkbox
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);

//red color for displayed text


simpleCheckBox.setTextColor(Color.RED);
6. textSize: textSize attribute is used to set the size of text of a check box. We can set
the text size in sp(scale independent pixel) or dp(density pixel).
Below is the example code in which we set the 20sp size for the text of a check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text size Attribute Of Check Box"
android:textColor="#00f"
android:checked="false"
android:textSize="20sp"/><!--set Text Size of text in CheckBox-->

Setting Text Size in CheckBox In Java class:


Below we set the text size of a check box in java class.

/*Add in Oncreate() funtion after setContentView()*/


CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
//set 20sp displayed text size
simpleCheckBox.setTextSize(20);

7. textStyle: textStyle attribute is used to set the text style of the text of a check box.
The possible text styles are bold, italic and normal. If we need to use two or more styles
for a text view then “|” operator is used for that.
Below we set the bold and italic text styles for text of a check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Style Attribute Of Check Box"
android:textColor="#44f"
android:textSize="20sp"
android:checked="false"
android:textStyle="bold|italic"/>

8. background: background attribute is used to set the background of a check box. We


can set a color or a drawable in the background of a check box.
Below we set the black color for the background, white color for the displayed text of a
check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text size Attribute Of Check Box"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold|italic"
android:checked="true"
android:background="#000" /><!-- black background for the background of check
box-->
Setting Background in CheckBox In Java class:
Below is the example code in which we set the background color of a check box
programmatically means in java class.

/*Add in Oncreate() funtion after setContentView()*/


CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
// set background in CheckBox
simpleCheckBox.setBackgroundColor(Color.BLACK);

9. padding: padding attribute is used to set the padding from left, right, top or bottom.

 paddingRight :set the padding from the right side of the check box.
 paddingLeft :set the padding from the left side of the check box.
 paddingTop :set the padding from the top side of the check box.
 paddingBottom :set the padding from the bottom side of the check box.
 Padding :set the padding from the all side’s of the check box.

Below is the example code of padding where we set the 30dp padding from all the side’s
of the check box.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Padding Attribute Of Check Box"
android:textColor="#44f"
android:textSize="20sp"
android:textStyle="bold|italic"
android:checked="false"
android:padding="30dp"/> <!--30dp padding from all side’s-->

RadioButton & RadioGroup


Important Note: RadioGroup is a widget used in Android for the grouping of radio
buttons and provide the feature of selecting only one radio button from the set. When a
user try to select any other radio button within same radio group the previously
selected radio button will be automatically unchecked.
RadioGroup And RadioButton code in XML:

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/simpleRadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
Checking Current State Of Radio Button:
You can check the current state of a radio button programmatically by using isChecked()
method. This method returns a Boolean value either true or false. if it is checked then
returns true otherwise returns false. Below is an example code with explanation in
which we checked the current state of a radio button.

/*Add in Oncreate() funtion after setContentView()*/


RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);
// initiate a radio button

Boolean RadioButtonState = simpleRadioButton.isChecked(); // check current state of a


radio button (true or false).

Attributes of RadioButton In Android:


Now let’s we discuss important attributes that helps us to create a beautiful radio
button in xml file (layout).
1. id: id is an attribute used to uniquely identify a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

2. checked: checked attribute in radio button is used to set the current state of a radio
button. We can set it either true or false where true shows the checked state and false
shows unchecked state of a radio button. As usual default value of checked attribute is
false. We can also set the current state in JAVA.
Below we set true value for checked attribute which sets the current state to checked of
a Button

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/> <!-- set the current state of the radio button-->

Setting checked State Of RadioButton In Java Class:


Below code set the current state of RadioButton to checked programmatically.

/*Add in Oncreate() funtion after setContentView()*/


// initiate a radio button
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);

// set the current state of a radio button


simpleRadioButton.setChecked(true);

3. text: text attribute is used to set the text in a radio button. We can set the text both
ways either in XML or in JAVA class.
Below is the example code with explanation included in which we set the text “I am a
radiobutton” of a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerHorizontal="true"
android:text="I am a radiobutton" /> <!-- displayed text of radio button-->

Setting text of RadioButton In Java class:


Below we set the text of a radio button programmatically:

/*Add in Oncreate() funtion after setContentView()*/


RadioButton simpleRadioButton=(RadioButton) findViewById(R.id.simpleRadioButton);
simpleRadioButton.setText("I am a radiobutton"); // displayed text of radio button

4. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of text like left, right, center, top, bottom, center_vertical, center_horizontal
etc.
Below is the example code with explanation included in which we set the center gravity
for the text of a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="I am a Button"
android:gravity="center"/> <!-- center gravity of the text-->
5. textColor: textColor attribute is used to set the text color of a radio button. Color
value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
Below we set the red color for the displayed text of a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerHorizontal="true"
android:text="Male"
android:textColor="#f00" /><!--red color for displayed text-->

Setting textColor of RadioButton text In Java class:


Below we set the text color of a radio button programmatically.

/*Add in Oncreate() funtion after setContentView()*/


RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);//
initiate radio button

simpleRadioButton.setTextColor(Color.RED); //red color for displayed text of radio


button

6. textSize: textSize attribute is used to set the size of the text of a radio button. We can
set the text size in sp(scale independent pixel) or dp(density pixel).
Below we set the 25sp size for the text of a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerHorizontal="true"
android:text="AbhiAndroid"
android:textColor="#f00"
android:textSize="25sp"/> <!--setting text size-->

Setting textSize Of RadioButton Text In Java class:


Below we set the text size of a radio button programmatically:

/*Add in Oncreate() funtion after setContentView()*/


RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);
// initiate radio button
simpleRadioButton.setTextSize(25); // set 25sp displayed text size of radio button

7. textStyle: textStyle attribute is used to set the text style of the text of a radio button.
The possible text styles are bold, italic and normal. If we need to use two or more styles
for a text view then “|” operator is used for that.
Below is the example code with explanation included in which we set the bold and italic
text styles for text of a radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="25sp"
android:layout_centerHorizontal="true"
android:text="Male"
android:textColor="#f00"
android:textStyle="bold|italic"/> <!-- bold and italic text style-->

8. background: background attribute is used to set the background of a radio button.


We can set a color or a drawable in the background of a radio button.
Below we set the black color for the background and red color for the displayed text of a
radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="25sp"
android:textStyle="bold|italic"
android:padding="20dp"
android:layout_centerHorizontal="true"
android:text="Male"
android:textColor="#f00"
android:background="#000"/> <!-- black background for radio button-->

Setting Background Of RadioButton In Java class:


Below we set the background color of a radio button programmatically.

/*Add in Oncreate() funtion after setContentView()*/


RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);

simpleRadioButton.setBackgroundColor(Color.BLACK);

9. padding: padding attribute is used to set the padding from left, right, top or bottom.

 paddingRight: set the padding from the right side of the radio button.
 paddingLeft : set the padding from the left side of the radio button.
 paddingTop : set the padding from the top side of the radio button.
 paddingBottom: set the padding from the bottom side of the radio button.
 Padding: set the padding from the all side’s of the radio button.
Below we set padding attribute of 20dp padding from all the side’s of the radio button.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="25sp"
android:textStyle="bold|italic"
android:layout_centerHorizontal="true"
android:text="AbhiAndroid"
android:textColor="#f00"
android:padding="40dp"/> <!--40dp padding from all the sides of radio button-->

10. drawableBottom, drawableTop, drawableLeft And drawableRight: These attribute


draw the drawable to the below of the text of RadioButton.
Below we set the icon to the right of the text of a RadioButton.

<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="25sp"
android:padding="20dp"
android:layout_centerHorizontal="true"
android:text="AbhiAndroid"
android:textColor="#f00"
android:drawableRight="@drawable/ic_launcher" /> <!-- drawable icon at the right
of radio button-->

You might also like