Fransa’da Pokemon GO Tartışmaları
Önceki Makale
- Anasayfa
- Android
- Android – Time Zone Uygulaması Yapımı
Android – Time Zone Uygulaması Yapımı
Merhaba arkadaşlar bu konumuzda android de time zone uygulaması yapımını öğreneceğiz. Bu uygulamamızda listeden seçeceğimiz bir ülkenin saatini, bulunduğumuz yere göre saat farkını ve ekrana yazdıracağız.
MainActivity.java Kodlarımız
Aşağıda yer alan kodları mainActivity’de onCreate metotun’dan önce ekliyoruz.
private Spinner spinnerAvailableID; private Calendar current; private TextView textTimeZone, txtCurrentTime, txtTimeZoneTime; private long miliSeconds; private ArrayAdapter<String> idAdapter; private SimpleDateFormat sdf; private Date resultdate;
getGMTTime() Metotumuz
Aşağıda getGMTtime metodumuz yer almakta bu metodu onCreate metodun dan hemen sonra ekliyoruz.
private void getGMTTime() { current = Calendar.getInstance(); txtCurrentTime.setText("" + current.getTime()); miliSeconds = current.getTimeInMillis(); TimeZone tzCurrent = current.getTimeZone(); int offset = tzCurrent.getRawOffset(); if (tzCurrent.inDaylightTime(new Date())) { offset = offset + tzCurrent.getDSTSavings(); } miliSeconds = miliSeconds - offset; resultdate = new Date(miliSeconds); System.out.println(sdf.format(resultdate)); }
onCreate Metodu Kodları
Aşağıda yer alan kodları onCreate Metodu içerisine ekliyoruz.
spinnerAvailableID = (Spinner) findViewById(R.id.availableID); textTimeZone = (TextView) findViewById(R.id.timezone); txtCurrentTime = (TextView) findViewById(R.id.txtCurrentTime); txtTimeZoneTime = (TextView) findViewById(R.id.txtTimeZoneTime); String[] idArray = TimeZone.getAvailableIDs(); sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss"); idAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, idArray); idAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAvailableID.setAdapter(idAdapter); getGMTTime(); spinnerAvailableID .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { getGMTTime(); String selectedId = (String) (parent .getItemAtPosition(position)); TimeZone timezone = TimeZone.getTimeZone(selectedId); String TimeZoneName = timezone.getDisplayName(); int TimeZoneOffset = timezone.getRawOffset() / (60 * 1000); int hrs = TimeZoneOffset / 60; int mins = TimeZoneOffset % 60; miliSeconds = miliSeconds + timezone.getRawOffset(); resultdate = new Date(miliSeconds); System.out.println(sdf.format(resultdate)); textTimeZone.setText(TimeZoneName + " : GMT " + hrs + "." + mins); txtTimeZoneTime.setText("" + sdf.format(resultdate)); miliSeconds = 0; } @Override public void onNothingSelected(AdapterView<?> arg0) { } });
main.xml Dosyamıza ait kodlar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center|top" android:orientation="vertical" android:padding="5dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txtGMTTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/current" /> <TextView android:id="@+id/txtCurrentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:context=".MainActivity" /> </LinearLayout> <Spinner android:id="@+id/availableID" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/timezone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtTimeZoneTime" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
Ekran Görüntüleri
-
-
Android'de NotificationManager Kullanarak Bildirim Oluşturma Sonraki Makale
Konu İle İlgili Soru, Görüş ve Öneriler