Using Basic Views
Using Basic Views
Android View
The View is a base class for all UI components in android. For example,
the EditText class is used to accept the input from users in android apps,
which is a subclass of View.
Following are the some of common View subclasses that will be used in
android applications.
TextView
Button
ImageButton
EditText
CheckBox
Toggle button
RadioButton
Radio groupviews
Progress Bar View
Spinner
Android ViewGroup
The ViewGroup is a subclass of View and it will act as a base class
for layouts and layouts parameters. The ViewGroup will provide an
invisible container to hold other Views or ViewGroups and to define the
layout properties.
Both View and ViewGroup subclasses together will play a key role to
create a layouts in android applications.
i) Text View:
In android, TextView is a user interface control that is used to set
and display the text to the user based on our requirements. The
TextView control will act as like label control and it won’t allow users
to edit the text.
Attributes Description
android: id It is used to uniquely identify the control
android: ems It is used to make the textview be exactly this many ems
wide.
android: hint It is used to display the hint text when text is empty
android: width It makes the TextView be exactly this many pixels wide.
android: height It makes the TextView be exactly this many pixels tall.
android:text It is used to display the text.
android:text Color It is used to change the color of the text.
android: gravity It is used to specify how to align the text by the view's x
and y-axis.
android: maxWidth It is used to make the TextView be at most this many
pixels wide.
android: minWidth It is used to make the TextView be at least this many
pixels wide.
android: textSize It is used to specify the size of the text.
android: textStyle It is used to change the style (bold, italic, bold italic) of
text.
android: textColor It is used to change the color of the text.
android: inputType It is used to specify the type of text being placed in text
fields.
android: fontFamily It is used to specify the fontFamily for the text.
ii) Button
Attribute Description
android:id It is used to uniquely identify the control
android: gravity It is used to specify how to align the text like
left, right, center, top, etc.
android:text It is used to set the text.
android: textColor It is used to change the color of text.
android: textSize It is used to specify the size of the text.
android: textStyle It is used to change the style (bold, italic,
bolditalic) of text.
android: background It is used to set the background color for button
control.
android: padding It is used to set the padding from left, right, top
and bottom.
android: drawableBottom It’s drawable to be drawn to the below of text.
android: drawableRight It’s drawable to be drawn to the right of text.
android: drawableLeft It’s drawable to be drawn to the left of the text.
iii) ImageButton
In android, Image Button is a user interface control that is used to
display a button with an image and to perform an action when a
user clicks or taps on it.
Attributes Description
android:id It is used to uniquely identify the control
android:src It is used to specify the source file of an image
android:background It is used to set the background color for an image
button control.
android:padding It is used to set the padding from left, right, top and
bottom of the image button.
android: baseline It is used to set the offset of the baseline within the
view.
iv) EditText
In android, EditText is a user interface control which is used to
allow the user to enter or modify the text. While
using EditText control in our android applications, we need to
specify the type of data the text field can accept using
the inputType attribute.
For example, if it accepts plain text, then we need to specify the
inputType as “text”. In case if EditText field is for password,
then we need to specify the inputType as “textPassword”.
In android, EditText control is an extended version
of TextView control with additional features and it is used to
allow users to enter input values.
Attributes Description
android:id It is used to uniquely identify the control
android: gravity It is used to specify how to align the text like left,
right, center, top, etc.
android:text It is used to set the text.
android: hint It is used to display the hint text when text is empty
android: textColor It is used to change the color of the text.
android: textColorHint It is used to change the text color of hint text.
android: textSize It is used to specify the size of the text.
android: textStyle It is used to change the style (bold, italic, bolditalic) of
text.
android: background It is used to set the background color for edit text
control
android: ems It is used to make the textview be exactly this many
ems wide.
android: width It makes the TextView be exactly this many pixels
wide.
android: height It makes the TextView be exactly this many pixels tall.
android: inputType It is used to specify the type of text being placed in
text fields.
android: fontFamily It is used to specify the fontFamily for the text.
v) CheckBox:
Attribute Description
android:id It is used to uniquely identify the control
android:checked It is used to specify the current state of checkbox
android:gravity It is used to specify how to align the text like left, right,
center, top, etc.
android:text It is used to set the text for a checkbox.
android:textColor It is used to change the color of text.
android:textSize It is used to specify the size of text.
android:textStyle It is used to change the style (bold, italic, bolditalic) of
text.
android:background It is used to set the background color for checkbox
control.
android:padding It is used to set the padding from left, right, top and
bottom.
android:onClick It’s the name of the method to invoke when the
checkbox clicked.
android:visibility It is used to control the visibility of control.
Attribute Description
android:id It is used to uniquely identify the control
android:checked It is used to specify the current state of toggle
button
android:gravity It is used to specify how to align the text like left,
right, center, top, etc.
android:text It is used to set the text.
vii) RadioButton
In android, Radio Button is a two-states button that can be
either checked or unchecked and it’s the same
as CheckBox control, except that it will allow only one option to
select from the group of options.
The user can press or click on the radio button to make it select.
In android, CheckBox control allow users to change the state of
control either Checked or Unchecked but the radio button cannot
be unchecked once it is checked.
Attribute Description
android:id It is used to uniquely identify the control
android:checked It is used to specify the current state of radio button
android:gravity It is used to specify how to align the text like left, right,
center, top, etc.
android:text It is used to set the text for the radio button.
viii) RadioGroup
In android, Radio Group is used to group one or more radio
buttons into separate groups based on our requirements.
Initially, all the radio buttons of the radio group are in the
unchecked state, once we select a radio button then it’s not possible
for us to uncheck it like CheckBox control.
ix) Spinner
In android, Spinner is a view that allows a user to select one value
from the list of values. The spinner in android will behave same as a
dropdown list in other programming languages.
Generally, the android spinners will provide a quick way to select
one item from the list of values and it will show a dropdown menu
with a list of all values when we click or tap on it.
By default, the android spinner will show its currently selected value
and by using Adapter we can bind the items to spinner objects.
Android Adapter
In android, Adapter will act as an intermediate between the data
sources and adapter views such as ListView, Gridview to fill the
data into adapter views. The adapter will hold the data and iterates
through items in data set and generate the views for each item in
the list.
Adapter Description
ArrayAdapter It will expect an Array or List as input.
CurosrAdapte It will accept an instance of a cursor as an input.
r
SimpleAdapte It will accept a static data defined in the resources.
r
BaseAdapter It is a generic implementation for all three adapter
types and it can be used for ListView, Gridview or
Spinners based on our requirements Attibutes