- Android Basics
- Android - User Interface
- Android Advanced Concepts
- Android Useful Examples
Why don't you download the jar file or aar package of library. I am sure the library must have provided a downloadable jar/aar file of it too. You can then just copy the jar in the libs folder or can import the aar into a separate module to inclu. How to apply 'Offline Components' in my project which i know it can make project gradle offline without internet connection, i found it from official android studio website. I already follow the instruction from the page but still not know how to apply it in my project, here is the instruction. And this picture is what i did so far,. The Android Software Development Kit (SDK) is a crucial part of Android development for beginners to come to grips with. It’s a selection of files bundled together that you will need to begin. So, here it is a list of download link for some Android SDK components (for other version of API, check the xml file above). Android SDK Platform 2.2, revision 2 Linux: android-2.2r02-linux.zip.
- Android Useful Resources
- Selected Reading
Android Studio 4.0 Gradle Offline
SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c
Database - Package
The main package is android.database.sqlite that contains the classes to manage your own databases
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
Apart from this , there are other functions available in the database package , that does this job. They are listed below
Sr.No | Method & Description |
---|---|
1 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler) This method only opens the existing database with the appropriate flag mode. The common flags mode could be OPEN_READWRITE OPEN_READONLY |
2 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) It is similar to the above method as it also opens the existing database but it does not define any handler to handle the errors of databases |
3 | openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) It not only opens but create the database if it not exists. This method is equivalent to openDatabase method. |
4 | openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory) This method is similar to above method but it takes the File object as a path rather then a string. It is equivalent to file.getPath() |
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
Sr.No | Method & Description |
---|---|
1 | execSQL(String sql, Object[] bindArgs) This method not only insert data , but also used to update or modify already existing data in database using bind arguments |
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.
There are other functions available in the Cursor class that allows us to effectively retrieve the data. That includes
Sr.No | Method & Description |
---|---|
1 | getColumnCount() This method return the total number of columns of the table. |
2 | getColumnIndex(String columnName) This method returns the index number of a column by specifying the name of the column |
3 | getColumnName(int columnIndex) This method returns the name of the column by specifying the index of the column |
4 | getColumnNames() This method returns the array of all the column names of the table. |
5 | getCount() This method returns the total number of rows in the cursor |
6 | getPosition() This method returns the current position of the cursor in the table |
7 | isClosed() This method returns true if the cursor is closed and return false otherwise |
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
Example
Here is an example demonstrating the use of SQLite Database. It creates a basic contacts applications that allows insertion, deletion and modification of contacts.
To experiment with this example, you need to run this on an actual device on which camera is supported.
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to get references of all the XML components and populate the contacts on listView. |
3 | Create new src/DBHelper.java that will manage the database work |
4 | Create a new Activity as DisplayContact.java that will display the contact on the screen |
5 | Modify the res/layout/activity_main to add respective XML components |
6 | Modify the res/layout/activity_display_contact.xml to add respective XML components |
7 | Modify the res/values/string.xml to add necessary string components |
8 | Modify the res/menu/display_contact.xml to add necessary menu components |
9 | Create a new menu as res/menu/mainmenu.xml to add the insert contact option |
10 | Run the application and choose a running android device and install the application on it and verify the results. |
Following is the content of the modified MainActivity.java.
Following is the modified content of display contact activity DisplayContact.java
Following is the content of Database class DBHelper.java
Following is the content of the res/layout/activity_main.xml
Android Studio Components Offline Download
Following is the content of the res/layout/activity_display_contact.xml
Following is the content of the res/value/string.xml
Following is the content of the res/menu/main_menu.xml
Following is the content of the res/menu/display_contact.xml
This is the defualt AndroidManifest.xml of this project
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio , open one of your project's activity files and click Run icon from the tool bar. Before starting your application,Android studio will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen −
Now open your optional menu, it will show as below image: Optional menu appears different places on different versions
Click on the add button of the menu screen to add a new contact. It will display the following screen −
It will display the following fields. Please enter the required information and click on save contact. It will bring you back to main screen.
Now our contact sai has been added.In order to see that where is your database is created. Open your android studio, connect your mobile. Go tools/android/android device monitor. Now browse the file explorer tab. Now browse this folder /data/data/<your.package.name>/databases<database-name>.
ImageView Tutorial With Example In Android
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.
Important Note:ImageView comes with different configuration options to support different scale types. Scale type options are used for scaling the bounds of an image to the bounds of the imageview. Some of them scaleTypes configuration properties are center, center_crop, fit_xy, fitStart etc. You can read our ScaleType tutorial to learn all details on it.
Below is an ImageView code in XML:
Make sure to save lion image in drawable folder
Table Of Contents
Android Studio 4.0 Offline Work
Attributes of ImageView:
Now let’s we discuss some important attributes that helps us to configure a ImageView in your xml file.
1. id: id is an attribute used to uniquely identify a image view in android. Below is the example code in which we set the id of a image view.
2. src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.
Below is the example code in which we set the source of a imageview lion which is saved in drawable folder.
In Java:
We can also set the source image at run time programmatically in java class. For that we use setImageResource() method as shown in below example code.
3. 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.Below is the example code in which we set the black color in the background and an image in the src attribute of image view.
In Java:We can also set the background at run time programmatically in java class. In below example code we set the black color in the background of a image view.
4. padding: padding attribute 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.
Below is the example code of padding attribute in which we set the 30dp padding from all the side’s of a image view.
5. scaleType:scaleType is 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.Below is the example code of scale type in which we set the scale type of image view to fit_xy.
Let’s we take an another example of scale type to understand the actual working of scale type in a image view.In below example code we set the value for scale type “fitStart” which is used to fit the image in the start of the image view as shown below:
Android Studio Offline Documentation
Example of ImageView:
Below is the example of imageview in which we display two animal images of Lion and Monkey. And whenever user click on an image Animal name is displayed as toast on screen. Below is the final output and code:
How To Install Android Studio Components Offline
Step 1:Create a new project and name it ImageViewExample.In this step we create a new project in android studio by filling all the necessary details of the app like app name, package name, api versions etc.
Step 2: Download two images lion and monkey from the web. Now save those images in the drawable folder of your project.
Step 3: Now open res -> layout -> activity_main.xml (or) main.xml and add following code:In this step we add the code for displaying an image view on the screen in a relative layout. Here make sure you have already saved two images name lion and monkey in your drawable folder.
Step 4: Now open app -> java -> package -> MainActivity.java and add the following code:
In this step we add the code to initiate the image view’s and then perform click event on them.
Output:
Now start AVD in Emulator and run the App. You will see the images of Lion and Monkey displayed on screen. Click on any Animal image and his name will appear on Screen. We clicked on Lion.