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

Using Basic Views

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Using Basic Views

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

USING BASIC VIEWS

In android, Layout is used to define the user interface for an app


or activity and it will hold the UI elements that will appear to the user.

The user interface in an android app is made with a collection


of View and ViewGroup objects. Generally, the android apps will contain
one or more activities and each activity is a one screen of app. The
activities will contain multiple UI components and those UI components
are the instances of View and ViewGroup subclasses.

The user interface in an android app is made with a collection


of View and ViewGroup objects. Generally, the android apps will contain
one or more activities and each activity is a one screen of the app. The
activities will contain multiple UI components and those UI components
are the instances of View and ViewGroup subclasses.

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.

For example, Linear Layout is the ViewGroup that contains a UI controls


like button, textview, etc. and other layouts also.

Following are the commonly used ViewGroup subclasses in android


applications.
 Linear Layout
 Relative Layout
 Table Layout
 Frame Layout
 Web View
 List View
 Grid View

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.

Android TextView Attributes


The following are some of the commonly used attributes related to
TextView control in android applications.

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

In android, Button is a user interface control that is used to perform


an action whenever the user clicks or tap on it.
Generally, Buttons in android will contain a text or an icon or both
and perform an action when the user touches it.

In android, we have a different type of buttons available to use


based on our requirements, those are ImageButton, Toggle
Button, RadioButton.

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.

By default, the ImageButton looks same as normal button and it


performs an action when a user clicks or touches it, but the only
difference is we will add a custom image to the button instead of
text.

In android, we can add an image to the button by


using <ImageButton> attribute android:src in XML layout file or
by using the setImageResource() method.

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:

 In android, CheckBox is a two-states button that can be either


checked (ON) or unchecked (OFF) and it will allow users to toggle
between the two states (ON / OFF) based on the requirements.
 Generally, use multiple CheckBox controls in android application
to allow users to select one or more options from the set of
values.

 By default, the android CheckBox will be in


the OFF (Unchecked) state. We can change the default state of
CheckBox by using android:checked attribute.

 In case, if we want to change the state


of CheckBox to ON (Checked), then we need to
set android:checked = “true” in XML layout file.

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.

vi) Toggle Button

In android, Toggle Button is a user interface control that is used to


display ON (Checked) or OFF (Unchecked) states as a button with
a light indicator.

The ToggleButton is useful for the users to change the settings


between two states either ON or OFF. We can add
a ToggleButton to our application layout by using
the ToggleButton object.
In case, if we want to change the state of ToggleButton
to ON (Checked), then we need to set android: checked =
“true” in XML layout file.

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.

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 toggle
button control.
android:padding It is used to set the padding from left, right, top
and bottom.

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.

 Generally, use RadioButton controls in an android application to


allow users to select only one option from the set of values.

 In android, use radio buttons with in a RadioGroup to combine


multiple radio buttons into one group and it will make sure that
users can select only one option from the group of multiple
options.
 By default, the android RadioButton will be
in OFF (Unchecked) state. the default state of RadioButton by
using android: checked attribute.

 In case, if you want to change the state


of RadioButton to ON (Checked), then we need to
set android: checked = “true” in our XML layout file.

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.

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 radio button
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 radio
button clicked.
android:visibility It is used to control the visibility of control.

viii) RadioGroup
In android, Radio Group is used to group one or more radio
buttons into separate groups based on our requirements.

If we group Radio Buttons using RadioGroup, at a time only one


item can be selected from the group of radio buttons. In case, if we
select one radio button that belongs to a radio group will unselect all
other previously selected radio buttons within the same group.

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.

We can populate our Spinner control with list of choices by defining


an ArrayAdapter in our Activity file.

Generally, the Adapter pulls data from sources such as an array or


database and converts each item into a result view and that’s
placed into the list.

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.

Generally, in android we have a different types of adapters available


to fetch the data from different data sources to fill the data into
adapter views, those are

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

You might also like