In this example opening url in webview and showing progress Dialog for page. Show file chooser option on clicking file option for web form . Open new link from webview to external browser. For any url open new activity or do other task.
You can save and retrieve key, value pair data from Shared preferences.
SharedPreferences values will persist across user sessions. Data in shared preferences will be persistent even though user closes the application.
In this example capturing video by inbuild camera in android and save captured video on sdcard. Using camera intent to record video and after record the video again come back on activity and show captured video path on activity.
ContentProvider used to get data from central repository. Android application contains content provider to provide data to other applications. you can also create your custom content provider to get data from database / sdcard / media etc.
Content providers create an abstraction layer between its repository of data and external application that are using data.
External Application can call Content Provider methods with the use of ContentResolver.
ContentResolver work as ContentProvider client object, with the use of Content Resolver object we can get data from Content Provider.
ContentProvider and ContentResolver (provider clients) used together to create a interface for data to handles inter-process communication and access data in secure way.
In this example defining variable in application context and you can use this variable as a Global variable. You can set value in this variable and get value on any activity / broadcast reci\\eiver / service in application context(environment).
In Further examples you will see good use of this way to follow SINGALTON pattern and MVC pattern in android.
In this example creating a custom Expandable ListView with parent and child rows. parent rows contains texts,images and a checkbox. child rows contains texts,images. Creating custom adapter to create Expandable ListView rows .
1. Create Model classes for parent rows(Parent.java) and for child rows(Child.java). These classes will use to store parent rows and child rows data. 2. Create xml files for parent rows(grouprow.xml) and for child rows(childrow.xml). These classes will use to create GUI for parent rows and child rows. 3. Create dummy data in parent and child model objects and Store objects in an ArrayList. 4. Create custom Adapter MyExpandableListAdapter class and inflate grouprow.xml file for parent rows() and childrow.xml for child rows. Use Models Parent.java and Child.java to create data for rows.
In this example creating a tab layout to show different screen for each tab.
Creating three tabs and setting different images and screens for each tab.
- To Create Tab and Tab change listner Extend TabActivity and implements OnTabChangeListener.
- Create tabhost object to create tabs.
- Create three tabs name FOOD , GAME , SPORT and set background images.
- Create three activities add these tabs.
- Add Tabs to tabhost.
In This example calling dot net webservice to get/send data to server. Using ksoap2-android-assembly-2.4-jar-with-dependencies.jar to make soap call.
Accessing a live web service ConvertWeight from http://www.webserviceX.NET/ which convert weight from one unit to another.
1. Add jar ksoap2-android-assembly-2.4-jar-with-dependencies.jar in your project. 2. Creating class WebserviceCall.java and define webservice namespace and url and make soap call. Accessing a live web service ConvertWeight from http://www.webserviceX.NET/ which convert weight from one unit to another. 3. Call WebserviceCall.java object to call getConvertedWeight method. 4. Showing response in a toast(alert) and on screen.
In this example we will see how to check internet connectivity.
When we have requirement to call file on server or call webservice then first check internet connctivity.
If internet connection is showing connected then we will call file on server or call webservice.
Required ACCESS_NETWORK_STATE permission in mainfest.xml file.
First Time User Please check previous example for normal file upload by server script( php | java | .net | coldfusion ) Upload File To Server - Android Example (http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106 )
In this example uploading file on server by ftp client ( with the use of ftp4j-1.6.jar ).
1. Using ftp4j-1.6.jar from sauronsoftware repository. Download and Add ftp4j-1.6.jar as library.
2. Must be required a Server Dedicated IP for FTP File upload.
In This example creating a simple repeating alarm system with the use of AlarmManager. Alarm will start after each 2 mins.
1. Using AlarmManager class to repeating and called a PendingIntent after each 2 min. 2. Call a media file on PendingIntent and start media file. 3. So after each 2 min AlarmManager will call PendingIntent a media file start.
In this example broadcasting screen wake / sleep event with a service.
Some times we have requirement to make less battery consumption then you can stop your services when phone is going to sleep mode and again start your services when phone is going to wake.
1. Create a service inside main activity to get screen wake / sleep broadcast events. 2. Create a broadcast receiver inside service to get screen wake / sleep events. 3. Send screen wake/sleep value from broadcast receiver to Service.
In this example creating time picker. time picker is used for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners.
In this Example creating custom spinner with one image and two texts.
After spinner item selection showing selected item text on screen.
1. Create Model (SpinnerModel.java) to store data for each spinner row. 2. Create a ArrayList to store Model (SpinnerModel.java) objects. 3. Store data in Models and Store Model objects in Arraylist. 4. Pass Model object Arraylist to custom adapter. 5. Custom Adapter use Arraylist data (Model Objects) and create rows for Spinner. 6. Create listener for Spinner and show spinner item selected values on activity.
In this example uploading an image from sdcard to web server.
1. place an image on sdcard. 2. place sdcard image path and image name in UploadToServer.java. ( see below ) 3. create a php script (UploadToServer.php) at server. ( see below ) 4. place php script path in UploadToServer.java. ( see below ) 5. create folder name uploads on server where you have placed php script. 6. give permission 777 (read/write/execute) to uploads folder.
In this example creating a basic layout then a login screen.
In Android, TableLayout let you arranges components in rows and columns, just like the standard table layout in HTML, <tr> and <td>.
In this tutorial, we show you how to use TableLayout to arrange button, textview and edittext in rows and columns format, and also demonstrates the use of “android:layout_span” to span view in 2 cells, and “android:layout_column” to display the view in specified column.
In this example we learn how to create a RELATIVE LAYOUT and how it will adjust components.
1. Creating a Login screen.
The RelativeLayout is very flexible. RelativeLayout give flexbility to position your component base on the relative or sibling component’s position. It’s the most flexible layout, that allow you to position your component to display in anywhere you want.
In this example we will learn about LINEAR LAYOUT.
1. How to create Linear Layout. 2. Created example to place comonents vertical and horizontal order. 3. Created one example ( Login form ) to use both vertical and horizontal orientation property of LINEAR LAYOUT.
1. A broadcast receiver is an Android component which allows to register and listen for device orientation changes like sms messsage recieved , phone call recieved/pick/cut ,battery status changed, the Wi-Fi came on.
2. Android operating system and even other applications time to time broadcast messages about things that are happening like sms messsage recieved , phone call recieved/pick/cut ,battery status changed, the Wi-Fi came on.
3. With the help of broadcast receiver you will catch Android operating system specific events and then you can code your application for that event.
When we install an application in android then it create a thread for that application called MAIN UI Thread, All activities run inside that thread , By the android single thread model rule we can not access UI elements (bitmap , textview etc..) directly for another thread defined inside that activity.
So if want to access Main UI Thread elements by another thread then we will use handlers.
** A Service runs with the main application thread , so some time user interaction will lock the current application . Use Service inside a separate thread, you will reduce the risk of Application Not Responding (ANR) errors . So application's main thread can remain dedicated to user interaction with your activities.
** If you are starting service inside any activity then it will block activity so when created service done then only your activity will unlock. in this mean time if user will interacting to activity then Application Not Responding (ANR) errors will come.
** If you want user interaction on when activity is visible and also some server or time taking tasks(performs intensive or blocking operations) then you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface.