Time Zone App

Tutorials with advanced 'difficulty' and more Lines of Code.

Time Zone App

Postby ashwani » Tue Apr 26, 2011 4:15 am

Hi,
i had created a time zone app, i would like to share the source code of this application. i would also appreciate suggestions and improvements in my source code. Code of main.xml file is given below in case of any query please feel free to ask me:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
   

android:orientation="vertical"
    android:layout_width="fill_parent"
   

android:layout_height="fill_parent">
<Spinner android:id="@+id/spinner1"
   

android:layout_height="wrap_content"
   android:layout_width="match_parent">
</Spinner>
<TextView  android:id="@+id/textView1"
    android:layout_width="fill_parent"
   

android:layout_height="wrap_content"
    android:text=""
   

android:gravity="center_horizontal"
    android:drawingCacheQuality="high"
   

android:textStyle="bold"
   android:typeface="normal"
    android:textSize="30sp"
   

android:textColor="#12b1f3"/>
<ImageView android:id="@+id/imageView1"
   

android:layout_height="match_parent"
   android:layout_width="match_parent">
</ImageView>
</LinearLayout>


Code for the main activity class (TimeZone.java) is given below:
Code: Select all
package com.apps.timezone;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class Time_Zone extends Activity {
   /** Called when the activity is first created. */
   private ArrayAdapter<String> adapter;
   private Spinner zone_list;
   private TextView time;
   private ImageView zonemap;
   Calendar c;
   private String[] zones = { "Etc/GMT-12", "Etc/GMT-11", "Etc/GMT-10",
         "Etc/GMT-9", "Etc/GMT-8", "Etc/GMT-7", "Etc/GMT-6", "Etc/GMT-5",
         "Etc/GMT-4", "Canada/Newfoundland", "Etc/GMT-3", "Etc/GMT-2",
         "Etc/GMT-1", "Etc/GMT", "Etc/GMT+1", "Etc/GMT+2", "Etc/GMT+3",
         "Asia/Tehran", "Etc/GMT+4", "Asia/Kabul", "Etc/GMT+5",
         "Asia/Kolkata", "Asia/Kathmandu", "Etc/GMT+6", "Etc/GMT+7",
         "Etc/GMT+8", "Etc/GMT+9", "Australia/Adelaide", "Etc/GMT+10",
         "Etc/GMT+11", "Etc/GMT+12" };
   private Integer[] mapimage = { R.drawable.mapm12, R.drawable.mapm11,
         R.drawable.mapm10, R.drawable.mapm09, R.drawable.mapm08,
         R.drawable.mapm07, R.drawable.mapm06, R.drawable.mapm05,
         R.drawable.mapm04, R.drawable.mapm033, R.drawable.mapm03,
         R.drawable.mapm02, R.drawable.mapm01, R.drawable.map,
         R.drawable.mapp01, R.drawable.mapp02, R.drawable.mapp03,
         R.drawable.mapp033, R.drawable.mapp04, R.drawable.mapp043,
         R.drawable.mapp05, R.drawable.mapp053, R.drawable.mapp054,
         R.drawable.mapp06, R.drawable.mapp07, R.drawable.mapp08,
         R.drawable.mapp09, R.drawable.mapp093, R.drawable.mapp10,
         R.drawable.mapp11, R.drawable.mapp12 };
   private String[] zone_names = { "(GMT-12:00) Eniwetok, Kwajalein",
         "(GMT-11:00) Midway Island, Samoa", "(GMT-10:00) Hawaii",
         "(GMT-9:00) Alaska", "(GMT-8:00) Pacific Time (US Canada)",
         "(GMT-7:00) Mountain Time (US Canada)",
         "(GMT-6:00) Central Time (US Canada), Mexico City",
         "(GMT-5:00) Eastern Time (US Canada), Bogota, Lima",
         "(GMT-4:00) Atlantic Time (Canada), Caracas, La Paz",
         "(GMT-3:30) Newfoundland",
         "(GMT-3:00) Brazil, Buenos Aires, Georgetown",
         "(GMT-2:00) Mid-Atlantic",
         "(GMT-1:00 hour) Azores, Cape Verde Islands",
         "(GMT) Western Europe Time, London, Lisbon, Casablanca",
         "(GMT+1:00 hour) Brussels, Copenhagen, Madrid, Paris",
         "(GMT+2:00) Kaliningrad, South Africa",
         "(GMT+3:00) Baghdad, Riyadh, Moscow, St. Petersburg",
         "(GMT+3:30) Tehran", "(GMT+4:00) Abu Dhabi, Muscat, Baku, Tbilisi",
         "(GMT+4:30) Kabul",
         "(GMT+5:00) Ekaterinburg, Islamabad, Karachi, Tashkent",
         "(GMT+5:30) Bombay, Calcutta, Madras, New Delhi",
         "(GMT+5:45) Kathmandu", "(GMT+6:00) Almaty, Dhaka, Colombo",
         "(GMT+7:00) Bangkok, Hanoi, Jakarta",
         "(GMT+8:00) Beijing, Perth, Singapore, Hong Kong",
         "(GMT+9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk",
         "(GMT+9:30) Adelaide, Darwin",
         "(GMT+10:00) Eastern Australia, Guam, Vladivostok",
         "(GMT+11:00) Magadan, Solomon Islands, New Caledonia",
         "(GMT+12:00) Auckland, Wellington, Fiji, Kamchatka" };

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      zone_list = (Spinner) findViewById(R.id.spinner1);
      time = (TextView) findViewById(R.id.textView1);
      zonemap = (ImageView) findViewById(R.id.imageView1);
      adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, zone_names);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      zone_list.setAdapter(adapter);      
      handler.removeCallbacks(updateTimeTask);
      handler.postDelayed(updateTimeTask, 1000);
   }

   private Runnable updateTimeTask = new Runnable() {
      public void run() {
         TimeZone.setDefault(TimeZone.getTimeZone(zones[zone_list
               .getSelectedItemPosition()]));
         c = Calendar.getInstance();
         Date time_millis = c.getTime();
         String date = String.valueOf(time_millis);
         date = date.substring(11, 19);
         paint(date);
         zonemap.setImageResource(mapimage[zone_list
               .getSelectedItemPosition()]);
         handler.postDelayed(this, 1000);
      }
   };

   public void paint(String date) {
      time.setText(date);
   }

   @Override
   protected void onStop() {
      super.onStop();
      handler.removeCallbacks(updateTimeTask);
   }

   @Override
   protected void onResume() {
      super.onResume();
      handler.removeCallbacks(updateTimeTask);
      handler.postDelayed(updateTimeTask, 1000);
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      if (handler != null)
         handler.removeCallbacks(updateTimeTask);
      handler = null;
   }

   private Handler handler = new Handler();
}


Please contribute to make this app more robust and look good. screen shots of the application are given below:
timezone.png
list from where you can choose time zone
timezone.png (38.03 KiB) Viewed 2366 times

timezone1.png
GMT -3:30 time zone selected
timezone1.png (37.48 KiB) Viewed 2366 times

timezone2.png
The Yellow layer indicates the GMT -12:00 time zone
timezone2.png (21.6 KiB) Viewed 2366 times
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Top

Re: Time Zone App

Postby faisalgeek » Tue Aug 09, 2011 10:27 am

Thanks very much for great sharing. but if you share the complete project like res/ folder then it will be great thanks.
faisalgeek
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 09, 2011 9:55 am

Re: Time Zone App

Postby blundell » Thu Aug 11, 2011 1:15 pm

Share the project on Github , looks good on your CV :-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Time Zone App

Postby kunal » Thu Feb 16, 2012 12:35 pm

Hi Ashwani,

Can you please share the images and updated code for the application.

Getting couple of errors while launching the application.

Thanks for the wonderful share:)
kunal
Freshman
Freshman
 
Posts: 2
Joined: Thu Feb 16, 2012 12:25 pm

Re: Time Zone App

Postby tooncs » Mon Apr 02, 2012 1:03 pm

thank u
tooncs
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Apr 02, 2012 7:43 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests