4

I see that it's possible to launch the date & time settings via an intent in Android, but what I'd like to do is launch just the list that shows the time zones (clicking "Select time zone") and get back the selected value without having the selection modify the user's date & time settings. Any idea how to do this?

Community
  • 1
  • 1
tronman
  • 8,540
  • 9
  • 41
  • 47

4 Answers4

10

If you are talking about a spinner you could do something like this:

ArrayAdapter <CharSequence> adapter =
          new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    String[]TZ = TimeZone.getAvailableIDs();
    ArrayList<String> TZ1 = new ArrayList<String>();
    for(int i = 0; i < TZ.length; i++) {
        if(!(TZ1.contains(TimeZone.getTimeZone(TZ[i]).getDisplayName()))) {
            TZ1.add(TimeZone.getTimeZone(TZ[i]).getDisplayName());
        }
    }
    for(int i = 0; i < TZ1.size(); i++) {
        adapter.add(TZ1.get(i));
    }
    final Spinner TZone = (Spinner)findViewById(R.id.TimeZoneEntry);
    TZone.setAdapter(adapter);
    for(int i = 0; i < TZ1.size(); i++) {
        if(TZ1.get(i).equals(TimeZone.getDefault().getDisplayName())) {
            TZone.setSelection(i);
        }
    }

Take a look here for more literature on TimeZone

CornCat
  • 294
  • 4
  • 10
6

Inspired by CornCats example above but a slightly different and shorter implementation with adapters that worked for me:

private void populateAndUpdateTimeZone() {

   //populate spinner with all timezones
    mSpinner = (Spinner) findViewById(R.id.mytimezonespinner);
    String[] idArray = TimeZone.getAvailableIDs();
    idAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
            idArray);
    idAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(idAdapter);

     // now set the spinner to default timezone from the time zone settings
    for(int i = 0; i < idAdapter.getCount(); i++) {
        if(idAdapter.getItem(i).equals(TimeZone.getDefault().getID())) {
            mSpinner.setSelection(i);
        }
    }
bhaskarc
  • 7,796
  • 10
  • 58
  • 80
0

I know this is old, but I am also interested...

I have been looking at this and I do not think it is possible. I would really like to be able to do the same thing. It has come down to either porting some of the GPL Android code to my app (ack), finding a perconfigured DB and code (JAR or java), or lastly writing this code myself (double ack). The timezones are a minefeild and I hope not to have to write the code myself. There are a lot of exceptions based on users locations.

Unconn
  • 578
  • 4
  • 5
  • I would just use this if you want to know the timezone where a user is based on lat long. http://stackoverflow.com/questions/41504/timezone-lookup-from-latitude-longitude – blindstuff Feb 17 '11 at 18:12
0

Android system classes ZoneList.java and ZonePicker.java are used in building TimeZones list in "Date & Time" settings. Link to source ZoneList.java

iBog
  • 2,148
  • 1
  • 19
  • 15
  • 1
    i know this is old, but the link is dead – Shmuel Mar 11 '14 at 17:32
  • Google knows where is it ;-) https://android.googlesource.com/platform/packages/apps/Settings/+/froyo/src/com/android/settings/ZoneList.java https://android.googlesource.com/platform/packages/apps/Settings/+/froyo/src/com/android/settings/ZonePicker.java – iBog Mar 12 '14 at 10:03