List View
• Listof scrollable items can be displayed in Android using
ListView.
• It helps you to displaying the data in the form of a
scrollable list.
• Users can then select any list item by clicking on it.
• ListView is default scrollable so we do not need to use
scroll View or anything else with ListView.
• ListView is widely used in android applications.
• A very common example of ListView is your phone
contact book, where you have a list of your contacts
displayed in a ListView and if you click on it then user
information is displayed.
4.
Attributes
• id: idis used to uniquely identify a ListView.
• divider: This is a drawable or color to draw between
different list items.
• dividerHeight: This specify the height of the divider
between list items. This could be in dp(density
pixel),sp(scale independent pixel) or px(pixel).
• listSelector: listSelector property is used to set the
selector of the listView. It is generally orange or Sky
blue color mostly but you can also define your
custom color or an image as a list selector as per
your design.
Second solution tocreate Listview
• Open strings.xml file from values folder and do changes as
shown
• <resources>
<string name="app_name">listExample</string>
<string-array name="fruits">
<item>orange</item>
<item>Mango</item>
<item>Banana</item>
<item>Apple</item>
<item>Strawberry</item>
<item>Grapes</item>
</string-array>
</resources>
ArrayAdapter in Androidwith Example
• The Adapter acts as a bridge between the UI Component and
the Data Source. It converts data from the data sources into
view items that can be displayed into the UI Component.
Data Source can be Arrays, HashMap, Database, etc. and UI
Components can be ListView, GridView, Spinner,
etc. ArrayAdapter is the most commonly used adapter in
android. When you have a list of single type items which are
stored in an array you can use ArrayAdapter. Likewise, if you
have a list of phone numbers, names, or cities. ArrayAdapter
has a layout with a single TextView. If you want to have a
more complex layout instead of ArrayAdapter use
• Parameters usedin ArrayAdapter:
• Lets discuss parameter in ArrayAdapter class:
• context:
• The first parameter is used to pass the context means the
reference of current class. Here this is a keyword used to
show the current class reference. We can also use
getApplicationContext(), getActivity() in the place of this
keyword. getApplicationContext() is used in a Activity and
getActivity() is used in a Fragment.
• Below is the example code in which we set the current class
reference in a adapter.
15.
• resource:
• Thesecond parameter is resource id used to
set the layout(xml file) for list items in which
you have a text view.
16.
• textViewResourceId:
• Thethird parameter is textViewResourceId
which is used to set the id of TextView where
you want to display the actual text.
• Below is the example code in which we set the
id(identity) of a text view.
17.
• objects:
• Thefourth parameter is an array of objects,
used to set the array of elements in the
textView. We can set the object of array or
array list here.
• Below is the example code in which we set the
Animal array in adapter to display the Animal
name’s list.
18.
Spinner
• Android Spinneris like the combox box of AWT or Swing.
• It can be used to display the multiple options to the user
in which only one item can be selected by the user.
• Android spinner is like the drop down menu with
multiple values from which the end user can select only
one value.
• Android spinner is associated with AdapterView.
• So you need to use one of the adapter classes with
spinner.
• Android Spinner class is the subclass of AsbSpinner class.
19.
• Open strings.xmlfile from values folder and do changes as shown
• <resources>
<string name="app_name">listExample</string>
<string-array name="fruits">
<item>orange</item>
<item>Mango</item>
<item>Banana</item>
<item>Apple</item>
<item>Strawberry</item>
<item>Grapes</item>
</string-array>
</resources>
ImageView
• In Android,ImageView class is used to display
an image file in application.
• Image file is easy to use but hard to master in
Android, because of the various screen sizes in
Android devices.
• An android is enriched with some of the best UI
design widgets that allows us to build good
looking and attractive UI based application.
Attributes
• id: idis an attribute used to uniquely identify a
image view in android.
• src: src is an attribute used to set a source file
or you can say image in your imageview to
make your layout attractive.
• 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.
25.
• padding: paddingattribute 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.
• scaleType: scaleTypeis 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.
• <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/android"
android:background="#000"
android:padding="10dp"
android:scaleType="fitXY" />
AutoCompleteTextView in Android
•The AutoCompleteTextView is a type of edit
text in android which gives suggestions to the
user if the user types something in the
AutoCompleteTextView.
• Android AutoCompleteTextView is a editable
text field, it displays a list of suggestions in a
drop down menu from which user can select
only one suggestion or value.
30.
GridView
• In androidGridView is a view group that display items
in two dimensional scrolling grid (rows and columns),
the grid items are not necessarily predetermined but
they are automatically inserted to the layout using a
ListAdapter.
• Users can then select any grid item by clicking on it.
• GridView is default scrollable so we don’t need to
use ScrollView or anything else with GridView.
• GridView is widely used in android applications.
• An example of GridView is your default Gallery, where
you have number of images displayed using grid.
32.
Attributes
• id: idis used to uniquely identify a GridView.
• numColumns: numColumn define how many
columns to show. It may be a integer value,
such as “5” or auto_fit.
• horizontalSpacing: horizontalSpacing property
is used to define the default horizontal spacing
between columns.
• This could be in pixel(px),density pixel(dp) or
scale independent pixel(sp).
33.
• verticalSpacing: verticalSpacingproperty used to define the
default vertical spacing between rows. This should be in px,
dp or sp.
• columnWidth: columnWidth property specifies the fixed
width of each column. This could be in px, dp or sp
• stretchMode
• Defines how columns should stretch to fill the available
empty space, if any. This must be either of the values −
• none − Stretching is disabled.
• spacingWidth − The spacing between each column is
stretched.
• columnWidth − Each column is stretched equally.
• spacingWidthUniform − The spacing between each column is
uniformly stretched..
AutoCompleteTextView
• AutoCompleteTextView isa view i.e similar to
EditText, except that it displays a list of completion
suggestions automatically while the user is typing.
• A list of suggestions is displayed in drop down
menu from which user can choose an item which
actually replace the content of Editbox with that.