Sunday, June 30, 2013

First Android Application

In this short tutorial I will show you how to convert the screen of your Nexus 7 into checker board.

I assume you have installed java 1.6 or 1.7, Eclipse classic (Juno) with plugin for Android and these Android packages: Android 4.2.2, 4.0.3 and 2.3.3 with SDK, Documentation, Google APIs, ARM EABI, Samples for SDK and Tools all 3 packages.

First create Android application in Eclipse and call it CheckerBoardApp. Leave everything default except when creating the project set Theme to none. You can also rename the java package from example to... lets say CheckerBoardPkg or you can leave it default.For publishing your app on Google Play you must change the package name to something different than example because it will not be accepted

Once you have the new application generated add new java class in the main src folder and call it CheckerBoard.

Add this code in it and save:

package com.checkerboardpkg.checkerboardapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CheckerBoard extends View {

    private final int TILE_WIDTH_HEIGHT = 160;
    private Paint currentPaint = null;
   
    public CheckerBoard(Context context) {
        super(context);
        currentPaint = new Paint();
        setFocusable(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.BLACK);

        int currentColor = 0;
       
        for (int i = 0; i < 8; i += 1) {
            for (int j = 0; j < 5; j += 1) {
               
                // set up color and Paint object
                if ((i + j) % 2 == 0) {
                    currentColor = Color.WHITE;
                } else {
                    currentColor = Color.BLACK;
                }
                currentPaint.setAntiAlias(true);
                currentPaint.setColor(currentColor);
               
                // draw rectangles to form checker board
                canvas.drawRect(j * this.TILE_WIDTH_HEIGHT, i
                        * this.TILE_WIDTH_HEIGHT, (j + 1)
                        * this.TILE_WIDTH_HEIGHT, (i + 1)
                        * this.TILE_WIDTH_HEIGHT, currentPaint);
            }
        }
    }
}


Basically what we do here is we create 8 rows and 5 columns of rectangles into the Canvas. We check the counters i and j if their sum is even or odd number and we change the color to white and than black appropriatelly. Each rectangle is 160x160 pixels so we get exactly 800x1280 screen which is the exact size of Nexus 7 screen resolution.


Now you must set the content of main acitivity to be the CheckerBoard view. We do that by adding this line

setContentView(new CheckerBoard(this));

as last statement in

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

function in MainActivity.java class.

Now the code is ready but to see it in action you must create Android Virtual Machine to run it on.
In Eclipse press Window -> Android Virtual Device Manager and that click New. Name it Nexus7AVD, Device: Nexus 7, target: Android 4.2.2, and set RAM to 512. It is 1024 by default but it may not start up if on Windows with low memory. Set SD card to 100 MB. Click OK.

Then press start and then launch. Wait some time while the AVD is not started. After you see the batery icon on top, your AVD is ready to run so go to Eclipse and right click on the project node in package explorer and click Run as -> Android Application.

Here is what you should see:




Mine app was called TileGame, but you should see CheckerBoardApp instead

Monday, March 5, 2012

Detecting user's timezone

There are two methods that can be used to detect user's timezone:

1. BY using JavaScript to get clients timezone and send it to php

2. By using geolocation data and IP address of the users PC.

List of Supported TimeZones

Link1

Link2

Link3

js detect

Link4

geolocation tool

php web service

Sunday, March 4, 2012

Friday, March 2, 2012

Time and Date related PHP functions

Overview

In this post I will explain how can you use time and date related php functions in order to manipulate current time of the user that is using your web app no matter where he is in the world.

Before I start all time/date related functions in php work accordingly to the timezone property in the php.ini file


date.timezone = "continent/city"


For example I am in Europe and the city of Skopje. So I edit my time zone with this value

date.timezone = "Europe/Skopje"


The default is I think date.timezone = "Europe/London". But anyway if you are developing multilingual application or it will be used from all over the world you should adjust your time zone in php.ini or use the default with adjusting the time in you code.

There is a function called

date_default_timezone_set() 

Here date-default-timezone-set, which can be used in you php code to change the timezone of the running script at runtime. So for example if in the php.ini is set to date.timezone = "Europe/Skopje" if I create a script test.php with this code 

[php tag]

date_default_timezone_set('Europe/London');
echo "date = ".date("D M j G:i:s T Y");

[/php tag]

Output: date = Fri Mar 2 10:29:33 GMT 2012

it will return that I am one hour behind. Thats because I restore the timezone to the default value i.e London

Note: As far as I was able to test this function, it doesnt work with just value "Skopje", so it must be used parameter like this "Europe/Skopje". Maybe there are also other misses like this but I dont know them at the moment.


Note: You can also use date_default_timezone_get() which will return the current timezone that is set by php.ini or overrided if date_default_timezone_set is used.


Basic Functions

1. time() gives the current time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
That means I can always know the current time and the number returned, can be used to convert to current date and time.

 I used two functions to get the current time according to php.ini timezone: date(), and localtime().

2. date() accepts two parameters $format and $timestap == time() function.
So it is neccessary to provide the format of the output. The timestamp defaults to the time() function.
I use this format for my local date "D M j G:i:s T Y".

D = day, M = month, j = day of the month, G = 24 hour format, i = minutes with leading zeroes
s = seconds with leading zeroes, T = Timezone abbreviation, Y = year.

There are more parameters you can add and also you can change the order of each.

3. localtime() is a function that also accepts two parameters, timestamp and type of the array returned, true if associative and false if numerically indexed.

So I use this code snippet to get formatted array

echo "<pre/>";
print_r( localtime(time(),true));
output:

Array
(
    [tm_sec] => 3
    [tm_min] => 3
    [tm_hour] => 12
    [tm_mday] => 2
    [tm_mon] => 2
    [tm_year] => 112
    [tm_wday] => 5
    [tm_yday] => 61
    [tm_isdst] => 0
)

it returnes array of values.

tm_sec = seconds,
tm_min = minutes,
tm_hour = hour 0-23,
tm_mday = month 1-31,
tm_mon = month 0-11,
tm_year = years since 1900,
tm_wday = day of the week 0-6,
tm_yday = day of the year 0-365,
tm_isdst = is daylight savings time in effect? Positive if yes, 0 if not, negative if unknown


I found this webpage on wikipedia which was very useful for me http://en.wikipedia.org/wiki/GMT
So we measure in UTC time format which is equvalent to GMT. Just to mention that meridian time is not always the same as the time we use on daily basis. This means that for example in Iceland meridian time would be UTC-01 but they use the UTC+00 because of more comfortable usage and other reasons (Internet, etc.)
 
     

Wednesday, February 29, 2012