JSON is Javascript Object Notation. Basically it is a part of Javascript programming language. Now a days it is generally use for data exchange between server and client. It is light weight data exchange format and takes lesser memory than xml. Data is stored in JSON as key-value pair. Basic Format of JSON :
{
"key1":"value1",
"key2":"value2",
"key3":"value3",
....
....
....
....
}
JSON in Android
Android provide two classes which is used to implement JSON functionality in Android .
JSONObject
JSONArray
1) JSONObject : JSONObject is used to convert JSON String to JSON Object and vice a versa. We can also create own JSON object from our raw data. For Example -
JSONObject jsonObj = new JSONObject();
jsonObj.put("name","Sam");
jsonObj.put("age","23");
jsonObj.put("address","USA");
console.log("JSON String",jsonObj.toString());
Result on console :
{"name":"Sam","age":"23","address":"USA"}
You can also convert JSON string to JSON object by passing json string into the constructor of JSONObject class.
String jsonString = "{'name':'Sam','age':'23','address':'USA'}";
JSONObject jsonObj = new JSONObject(jsonString);
To print key's value you need to call getString('key') method of JSONObject class.
Android uses Java Date api to implement the Date functionality in app. Today every major app have date functionality in it. Like Facebook, Instagram, Youtube etc have date functionality in it. Facebook posts and messages are order according to date. Latest post and messages are on the top position. In this post we learn about Date.
Current Date
public String CurrentDate()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.DAY_OF_MONTH)).trim();
}
Current Month
public String CurrentMonth()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.MONTH)+1).trim();
}
Current Year
public String CurrentYear()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.YEAR)).trim();
}
Current Hour
public String Hour()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.HOUR_OF_DAY)).trim();
}
Current Minute
public String Minute()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.MINUTE)).trim();
}
Current Second
public String Second()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.SECOND)).trim();
}
Current MilliSecond
public String MilliSecond()
{
Calendar cal = Calendar.getInstance();
return String.valueOf(cal.get(Calendar.MILLISECOND)).trim();
}
Last Month
public String LastMonth()
{
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH ,-1);
return( new SimpleDateFormat("MM").format(cal.getTime())).trim();
}
public String getLastDate(int month, int year)
{
Calendar cal = Calendar.getInstance();
// passing month-1 because 0-->jan, 1-->feb... 11-->dec
cal.set(year, month-1, 1);
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date date = cal.getTime();
//DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/YYYY");
DateFormat DATE_FORMAT = new SimpleDateFormat("dd");
return DATE_FORMAT.format(date).trim();
}
Day Name
public String getDayName(int day,int month,int year){
Date date =null;
String dateString = String.format("%d-%d-%d", year, month, day);
try{
date = new SimpleDateFormat("yyyy-M-d").parse(dateString);
}
catch (Exception e) {}
return new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
}
Previous Date
public static String getPreviousDate()
{
Calendar cal = Calendar.getInstance();
//subtracting a day
cal.add(Calendar.DATE, -1);
SimpleDateFormat s = new SimpleDateFormat("dd");
return s.format(new Date(cal.getTimeInMillis()));
}
Months Names List
public String[] GetMonthsNames()
{
DateFormatSymbols dfs = new DateFormatSymbols();
return dfs.getMonths();
}
Tomorrow Date
public int TomorrowDate()
{
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
return cal.get(Calendar.DATE);
}
Next Month
public int NextMonth(){
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 30);
return cal.get(Calendar.MONTH)+1;
}
Next Year
public int NextYear()
{
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
return cal.get(Calendar.YEAR);
}
Get Unix TimeStamp
public String unixTimestamp(int y, int m, int d)
{
month--;
Date tg = new Date(year, month, day);
return String.valueOf(tg.getTime() / 1000L);
}
Unix timestamp to Date
public String UNIX(long a)
{
long timestamp = a * 1000;
Date date = new Date(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR);
}
In Android insert() method is used to insert record into table. Insert method compiles query every time before inserting the data into table . If there are 1000 records it compiles query 1000 times. So to eliminate this problem android provides compileStatement(). It compile query only one and then it insert records only.
HTML provides <select> tag to select value using its default dropdown. But some times there is a need for Custom dropdown. So in this tutorial we are going to build custom dropdown.
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/openfile"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text=" Open Video File "/><ImageViewandroid:id="@+id/img"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"></ListView></LinearLayout>
Android provides Listview to display items in list. But there is drawback on Android basic Listview, you can not a customize it. So to make own Custom Listview you need to implement own code.
In this tutorial we are going to implement Custom Listview using BaseAdapter class.BaseAdapter class provides a set of methods which is used to create the custom list.
First create activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/tv1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:textStyle="bold"android:text=" Countries "/><ListViewandroid:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="fill_parent"></ListView></LinearLayout>
Now create a layout for list item which is displayed in Listview as a row. Create new file in layout folder named a list_row.xml
Now create Model named as CountryModel.java. This is used to send data between Activity and Adapter class.
public classCountryModel {private intimage;private StringcountryName;public voidsetImage(int a)
{image= a;} public voidsetCountry(String a)
{countryName= a;} public intgetImage()
{
return image;} public StringgetCountry()
{
return countryName;} }
Now create Adapter class named as CountryAdapter.java.
HTML5 provide different sets on API like Camera, Geo Location, Battery and many more APIs. Vibrate api is one if them. Today every mobile vendor provide vibrating functionality in its device.
We can implements vibrate functionality with two type. First is to use native api, like Android, IPhone, Blackberry its own sets of native api for vibrating functionality. But there is drawback of this. You need to learn its native language like Java, Objective-c or Swift to implements this.
Here are the implementation of Native implementation of vibrate mobile device.
Second method is to use HTML5 API. Which is platform independent. So in this tutorial we use HTML5 API to implement this functionality. HTML5 API runs only in browser either it is Mobile browser or Desktop Browser. You need that Browser which support HTML5 APIs.
Vibrate does not work in Desktop/Laptop Browser because there is not hardware for vibration in desktop/laptop. Browser Support of Vibrate API
Before we use the api , we need check whether vibrating functionality is supported by browser or not. Then we use vibrating functionality.
if ("vibrate" in navigator) {
// vibration API supported
}
To check and use prefixed versions, you can use code such as:
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
if (navigator.vibrate) {
// vibration API supported
}
Implement Vibrate functionality
Vibration is set by passing milliseconds in vibrate method. Then device vibrate upto that value which is passed in vibrate method. For ex. 1000 milli seconds, device will vibrate for 1 second.
navigator.vibrate(1000);
Vibrate method also accept array of vibration and delay time(milliseconds)
navigator.vibrate([1000, 500, 1000]);
The above code vibrate for 1000ms , the wait for 500ms and then vibrate again for 1000ms. You can any number of arguments in an array.