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

UNIT IV Android Programming

Uploaded by

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

UNIT IV Android Programming

Uploaded by

prachinpatil19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

UNIT IV

Android Programming
Using Basic Views

● Displaying Pictures and Menus :


○ Using Image Views to Display Pictures,
○ Gallery and ImageView views,
○ Image Switcher,
○ Grid View
● Using Menus with Views:
○ Creating the helper methods,
○ Options Menu,
○ Context Menu
● Databases –
○ SQLite: Introduction to SQLite,
○ SQLiteOpenHelper and
○ SQLiteDatabase,Creating , opening and closing database,Working with
cursors, Insert, Update, Delete,Building and executing queries
Using image views to displAy pictures

● 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

● You first add the Gallery and ImageView views to main.xml:

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

● The list of images to be displayed is stored in the imageIDs array:


● ImageAdapter class (which extends the BaseAdapter class) so that it can bind to the

Gallery view with a series of ImageView views


Using image views to displAy pictures


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

● This method creates a new View to be added in the ImageSwitcher view.


● Like the Gallery example in the previous section, you also implemented an ImageAdapter
class so that it can bind to the Gallery view with a series of ImageView views.
● In the onCreate() method, you get a reference to the ImageSwitcher view and set the
animation, specifying how images should “fly” in and out of the view.
● Finally, when an image is selected from the Gallery view, the image is displayed in the
ImageSwitcher view:


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

GridView by setting the padding for each image:


Using Menus with Views

Menus are useful for displaying additional options that are not directly visible on the main UI of an

application. There are two main types of menus in Android:

➤➤ Options menu — Displays information related to the current activity. In Android, you activate

the options menu by pressing the MENU key.

➤➤ Context menu — Displays information related to a particular view on an activity. In Android,

to activate a context menu you tap and hold on to it


Using Menus with Views

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

: CreateMenu() and MenuChoice().

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

of the Menu object.

MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”);

mnu1.setAlphabeticShortcut(‘a’);

mnu1.setIcon(R.drawable.icon);

}
Using Menus with Views

The four arguments of the add() method are as follows:

➤➤ groupId — The group identifier that the menu item should be part of. Use 0 if an item is not in

a group.

➤➤ itemId — Unique item ID

➤➤ order — The order in which the item should be displayed

➤➤ title — The text to display for the menu item


Using Menus with Views
Using Menus with Views
Using Menus with Views

Options Menu

You are now ready to modify the application to display the options menu when the user presses the

MENU button on the Android device.


Using Menus with Views
Using Menus with Views

● 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

setOnCreateContextMenuListener() method of that particular view


Using Menus with Views
Using Menus with Views

● 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 provides the functionality to use the SQLite database.


SQLite
SQLite

SQLiteOpenHelper class

● The android.database.sqlite.SQLiteOpenHelper class is used for database creation and version


management.
● For performing any database operation, you have to provide the implementation of onCreate() and
onUpgrade() methods of SQLiteOpenHelper class.
SQLite
SQLite

Database - Helper 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

public class DBHelper extends SQLiteOpenHelper {

public DBHelper (){

super(context,DATABASE_NAME ,null,1);

public void onCreate (SQLiteDatabase db) {}

public void onUpgrade (SQLiteDatabase database , int oldVersion , int newVersion ) {}

}
SQLite

Methods of SQLiteOpenHelper class:

SQLiteOpenHelper class has many methods. Some of them are as follows:

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

SQLiteDatabase mydatabase = openOrCreateDatabase("your database name",MODE_PRIVATE,null);


SQLite
SQLite

Database - Insertion

we can create table or insert data into table using execSQL method defined in SQLiteDatabase class. Its syntax
is given below

mydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username VARCHAR,Password VARCHAR);");

mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('admin','admin');");

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.

Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null);

resultSet.moveToFirst();

String username = resultSet.getString(0);

String password = resultSet.getString(1);


SQLite

There are other functions available in the Cursor class that allows us to effectively retrieve the data. That
includes
SQLite
SQLite

FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getContext());


SQLite

Read information from a database


SQLite
SQLite

Delete information from a database


SQLite

Update a database

You might also like