Ad

How To Show Timezone With Country And City Name In List In Android?

I am developing an application where i have to show the timezone with country and city name in spinner. I have use TimeZone class for it. But in this case i am getting a timezoneId and timezoneName only. How i can show timezone with country and city name in spinner ?

Ad

Answer

Solution:-

private String[] timezoneArray;
private Spinner spinner_timezone;

spinner_timezone = (Spinner) findViewById(R.id.spinner_timezone);

Then call this

 timezoneArray = TimeZone.getAvailableIDs();
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), timezoneArray);
        spinner_timezone.setAdapter(customAdapter);
        for (int i = 0; i < timezoneArray.length; i++) {
            if (timezoneArray[i].equals(TimeZone.getDefault().getID())) {
                spinner_timezone.setSelection(i);
                break;
            }
        }

then CustomAdapter class

 public class CustomAdapter extends BaseAdapter {
        Context context;
        String[] countryNames;
        LayoutInflater inflter;

        public CustomAdapter(Context applicationContext, String[] countryNames) {
            this.context = applicationContext;
            this.countryNames = countryNames;
            inflter = (LayoutInflater.from(applicationContext));
        }

        @Override
        public int getCount() {
            return countryNames.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = inflter.inflate(R.layout.custom_spinner_items, null);
            TextView names = (TextView) view.findViewById(R.id.textView);
            names.setText(displayTimeZone(TimeZone.getTimeZone(countryNames[i])));
            return view;
        }

        private String displayTimeZone(TimeZone tz) {

            long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
            long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
                    - TimeUnit.HOURS.toMinutes(hours);
            // avoid -4:-30 issue
            minutes = Math.abs(minutes);

            String result = "";
            if (hours > 0) {
                result = String.format("(GMT +%d:%02d) %s", hours, minutes, tz.getID());
            } else {
                result = String.format("(GMT %d:%02d) %s", hours, minutes, tz.getID());
            }

            return result;

        }
    }

And custom_spinner_items here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:textSize="15sp"
        android:text="Wifi Airtel"
        android:textColor="#000" />
</RelativeLayout>
Ad
source: stackoverflow.com
Ad