Tuesday, August 5, 2014

Display formatted date/time using String.format()

Examples of using String.format() to display formatted calendar:


package com.example.androidtime;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView cal = (TextView)findViewById(R.id.cal);

Calendar calendar = Calendar.getInstance();

cal.setText(
String.format("%td-%tm-%tY\n", calendar, calendar, calendar) +
String.format("%ty/%tm/%td\n", calendar, calendar, calendar) +
String.format("%tI:%tM:%tS %tp\n", calendar, calendar, calendar, calendar) +
"\n" +
String.format("%tD\n", calendar) +
String.format("%tc\n", calendar) +
String.format("%tF\n", calendar) +
String.format("%tr\n", calendar)
);

}

}

The layout file is same as last post.

Monday, August 4, 2014

Display date/time in various format with SimpleDateFormat

Examples of using java.text.SimpleDateFormat:


package com.example.androidtime;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView cal = (TextView)findViewById(R.id.cal);

Calendar calendar = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("yy/MM/dd");
SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format4 = new SimpleDateFormat("yyyy MMM dd (EEE)");
SimpleDateFormat format5 = new SimpleDateFormat("hh:mm a");
SimpleDateFormat format6 = new SimpleDateFormat("HH:mm");

cal.setText(
format1.format(calendar.getTime()) + "\n" +
format2.format(calendar.getTime()) + "\n" +
format3.format(calendar.getTime()) + "\n" +
format4.format(calendar.getTime()) + "\n" +
format5.format(calendar.getTime()) + "\n" +
format6.format(calendar.getTime())
);

}

}

<LinearLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidtime.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<TextView
android:id="@+id/cal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp" />

</LinearLayout>

Related: Display formatted date/time using String.format().

Get time of system

Example to get time (in millisecond and nanosecond) from System, and get instance of Calendar to get current time.


package com.example.androidtime;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tim = (TextView)findViewById(R.id.tim);
TextView cal = (TextView)findViewById(R.id.cal);

tim.setText(
"currentTimeMillis: " + System.currentTimeMillis() + "\n" +
"nanoTime" + System.nanoTime() + "\n"
);

Calendar calendar = Calendar.getInstance();
cal.setText(
"YEAR: " + calendar.get(Calendar.YEAR) + "\n" +
"MONTH: " + calendar.get(Calendar.MONTH) + "\n" +
"DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH) + "\n" +
"HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY) + "\n" +
"MINUTE: " + calendar.get(Calendar.MINUTE) + "\n" +
"SECOND: " + calendar.get(Calendar.SECOND) + "\n"
);

}

}

<LinearLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidtime.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<TextView
android:id="@+id/tim"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/cal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

Tuesday, July 29, 2014

Android L Developer Preview - Camera2 API

Ankur Kotwal presents the Camera2 API in Android L, which is designed as a very flexible and unified pipeline for all camera related tasks. This API gives you direct control of the camera subsystem including autofocus, asynchronous burst captures and special effects.

Thursday, July 24, 2014

Building Advanced UIs for Android Wear

How to write apps for Android Wear devices that need to support complex interactions with the user. This video will describe the navigational concepts that Wear apps should support in order to integrate well with the device and provide guidance on designing the UI flow of your app. Will also introduce a new UI widget library that allows app authors to create experiences with the same polish and consistency as the system UI. Using this library, app authors will be able to create "stream-like" UIs that provide the same physics and transition animations as the built-in "home" stream or search results stream.

In detail: This talk is meant to be largely around how to use GridViewPager in order to create "stream-like" experiences in apps for Android Wear.

Building Advanced UIs for Android Wear

Tuesday, July 15, 2014

Google I/O 2014 Highlights



Relive the best moments of Google I/O 2014, including the keynote, sessions, Developer Sandbox, and After Hours. The keynote and all I/O 2014 sessions are available at: google.com/io