UNIT IV Android Programming
UNIT IV Android Programming
Android Programming
Using Basic Views
● all the views we have seen until this point are used to display text information
● Gallery and ImageView Views
● The Gallery is a view that shows items (such as images) in a center-locked, horizontal
scrolling list.
●
Using image views to displAy pictures
the Gallery view is used to display the series of images in a horizontal scrolling list. The
ImageView is used to display the image selected by the user.
●
Using image views to displAy pictures
●
● ImageAdapter class (which extends the BaseAdapter class) so that it can bind to the
●
Using image views to displAy pictures
●
Using image views to displAy pictures
● When an image in the Gallery view is selected (i.e., clicked), the position (0 for the first
image, 1 for the second image, and so on) of the selected image is displayed and the image
is displayed in the ImageView.
●
ImageSwitcher
● The previous section demonstrated how to use the Gallery view together with an ImageView
to display a series of thumbnail images so that when one is selected, the selected image is
displayed in the ImageView.
● However, sometimes you don’t want an image to appear abruptly when the user selects
● it in the Gallery view — you might, for example, want to apply some animation to the image
when it transits from one image to another.
● In this case, you need to use the ImageSwitcher together with the Gallery view
●
ImageSwitcher
● the MainActivity not only extends Activity, but also implements ViewFactory.
● To use the ImageSwitcher view, you need to implement the ViewFactory interface, which
creates the views for use with the ImageSwitcher view.
● For this, you need to implement the makeView() method:
●
ImageSwitcher
●
ImageSwitcher
● In this example, when an image is selected in the Gallery view, it will appear by “fading” in.
When the next image is selected, the current image will fade out.
● If you want the image to slide in from the left and slide out to the right when another image
is selected, try the following animation:
●
GridView
● The GridView shows items in a two-dimensional scrolling grid. You can use the GridView
together with an ImageView to display a series of images.
● Like the Gallery and ImageSwitcher example, you implement the ImageAdapter class and
then bind it to the GridView
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”,
Toast.LENGTH_SHORT).show(); }
});
GridView
Within the GridView, you can specify the size of the images and how images are spaced out in the
Menus are useful for displaying additional options that are not directly visible on the main UI of an
➤➤ Options menu — Displays information related to the current activity. In Android, you activate
shows an example of an options menu in the browser application. The option menu is displayed whenever the
user presses the MENU button. The menu items displayed vary according to the current activity that is running
.
Using Menus with Views
shows a context menu that is displayed when the user presses and holds on a hyperlink displayed on the page.
The menu items displayed vary according to the component or view currently selected.
To activate the context menu, the user selects an item on the screen and either taps and holds it or simply
presses the center button on the directional keypad.
Using Menus with Views
The CreateMenu() method takes a Menu argument and adds a series of menu items to it.
To add a menu item to the menu, you create an instance of the MenuItem class and use the add() method
mnu1.setAlphabeticShortcut(‘a’);
mnu1.setIcon(R.drawable.icon);
}
Using Menus with Views
➤➤ groupId — The group identifier that the menu item should be part of. Use 0 if an item is not in
a group.
Options Menu
You are now ready to modify the application to display the options menu when the user presses the
● To display the options menu for your activity, you need to override two methods in your activity:
● onCreateOptionsMenu() and onOptionsItemSelected().
● The onCreateOptionsMenu() method is called when the MENU button is pressed.
● In this event, you call the CreateMenu() helper method to display the options menu.
● When a menu item is selected, the onOptionsItemSelected() method is called.
● In this case, you call the MenuChoice() method to display the menu item selected
Using Menus with Views
Using Menus with Views
Context Menu
● how the options menu is displayed when the user presses the MENU button.
● Besides the options menu, you can also display a context menu.
● A context menu is usually associated with a view on an activity, and it is displayed when the user long
clicks an item.
● For example, if the user taps on a Button view and hold it for a few seconds, a context menu can be
displayed.
If you want to associate a context menu with a view on an activity, you need to call the
● call the setOnCreateContextMenuListener() method of the Button view to associate it with a context
menu.
● When the user long-clicks the Button view, the onCreateContextMenu() method is called.
● In this method, you call the CreateMenu() method to display the context menu. Similarly, when an item
inside the context menu is selected, the onContextItemSelected() method is called, where you call the
MenuChoice() method to display a message to the user.
SQLite
● SQLite is an open-source relational database i.e. used to perform database operations on android
devices such as storing, manipulating or retrieving persistent data from the database.
● It is embedded in android bydefault. So, there is no need to perform any database setup or administration
task.
● Here, we are going to see the example of sqlite to store and fetch the data.
● Data is displayed in the logcat.
SQLiteOpenHelper class
● For managing all the operations related to the database , an helper class has been given and is called
SQLiteOpenHelper.
● It automatically manages the creation and update of the database. Its syntax is given below
super(context,DATABASE_NAME ,null,1);
}
SQLite
public abstract void onCreate(SQLiteDatabase db): This method is called only when you create a database for
the first time.
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion): This method is called
when the database needs to be upgraded.
public synchronized void close(): This method closes the database object.
SQLite
SQLite
Database - Creation
● In order to create a database you just need to call this method openOrCreateDatabase with your
database name and mode as a parameter.
● It returns an instance of SQLite database which you have to receive in your own object.Its syntax is given
below
Database - Insertion
we can create table or insert data into table using execSQL method defined in SQLiteDatabase class. Its syntax
is given below
This will insert some values into our table in our database. Another method that also does the same job but take some additional
parameter is given below
SQLite
Database - Fetching
● We can retrieve anything from database using an object of the Cursor class.
● We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to
the table.
● We can move the cursor forward and retrieve the data.
resultSet.moveToFirst();
There are other functions available in the Cursor class that allows us to effectively retrieve the data. That
includes
SQLite
SQLite
Update a database