Skip to content
Permalink
Browse files

Customize the Time Zones List

  • Loading branch information...
dragosholban committed Mar 10, 2018
1 parent 75abba1 commit dd09b5f56e8e33272ae5dfd6a6c9ed8433bcedf5
@@ -16,7 +16,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TimeZoneActivity"></activity>
<activity android:name=".TimeZoneActivity" />
<activity android:name=".SelectTimezonesActivity"></activity>
</application>

</manifest>
@@ -13,6 +13,10 @@
import android.widget.TextView;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.TimeZone;

@@ -25,8 +29,11 @@
TimeZone selectedTimeZone;
TextView convertedTimeTv;
TextView convertedDateTv;
ListView listView;
ArrayAdapter<String> adapter;

private static int CHOOSE_TIME_ZONE_REQUEST_CODE = 1;
private static int SELECT_TIME_ZONES_REQUEST_CODE = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -66,9 +73,8 @@ public void onStopTrackingTouch(SeekBar seekBar) {

selectTimeZoneBtn = findViewById(R.id.timeZoneButton);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_activated_1, android.R.id.text1, selectedTimezones);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView = findViewById(R.id.listView);
setupAdapter();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
@@ -107,6 +113,19 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
selectTimeZoneBtn.setText(timezone);
userTimeZone = TimeZone.getTimeZone(timezone);
}
if (requestCode == SELECT_TIME_ZONES_REQUEST_CODE && resultCode == RESULT_OK) {
Bundle bundle = data.getBundleExtra("selectedTimezonesBundle");
ArrayList<String> selectedTimezonesArrayList = bundle.getStringArrayList("selectedTimezones");
Collections.sort(selectedTimezonesArrayList, new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return s.compareToIgnoreCase(t1);
}
});
selectedTimezones = new String[selectedTimezonesArrayList.size()];
selectedTimezonesArrayList.toArray(selectedTimezones);
setupAdapter();
}
convertDate(userTimeZone, selectedTimeZone);
}

@@ -124,4 +143,17 @@ private void convertDate(TimeZone fromTimeZone, TimeZone toTimeZone) {
convertedDateTv.setText(DateFormat.getDateInstance().format(convertedDate));
}
}

public void selectTimezones(View view) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("selectedTimezones", new ArrayList<String>(Arrays.asList(selectedTimezones)));
Intent intent = new Intent(this, SelectTimezonesActivity.class);
intent.putExtra("selectedTimezonesBundle", bundle);
startActivityForResult(intent, 2);
}

private void setupAdapter() {
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_activated_1, android.R.id.text1, selectedTimezones);
listView.setAdapter(adapter);
}
}
@@ -0,0 +1,100 @@
package dragosholban.com.timezoneconverter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.TimeZone;

public class SelectTimezonesActivity extends AppCompatActivity {
ArrayList<String> selectedTimezones = new ArrayList<>();
ArrayAdapter<String> adapter;
ArrayList<String> timezones;
ListView listView;
boolean showAll = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_timezones);

setTitle("Choose Time Zones");

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("selectedTimezonesBundle");
selectedTimezones = bundle.getStringArrayList("selectedTimezones");
timezones = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));

listView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, timezones);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (listView.isItemChecked(i)) {
selectedTimezones.add(adapter.getItem(i));
} else {
selectedTimezones.remove(adapter.getItem(i));
}
}
});

checkSelectedTimezones();
}

public void done(View view) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("selectedTimezones", selectedTimezones);
Intent result = new Intent(this, MainActivity.class);
result.putExtra("selectedTimezonesBundle", bundle);
setResult(RESULT_OK, result);
finish();
}

public void showChecked(View view) {
Button button = (Button) view;
adapter.clear();
if (showAll) {
for (String timezone : selectedTimezones) {
adapter.add(timezone);
}
adapter.notifyDataSetChanged();

button.setText("Show All");
showAll = false;
} else {
for (String timezone : TimeZone.getAvailableIDs()) {
adapter.add(timezone);
}
adapter.notifyDataSetChanged();

button.setText("Show Checked");
showAll = true;
}

checkSelectedTimezones();
}

public void uncheckAll(View view) {
selectedTimezones.clear();
checkSelectedTimezones();
}

private void checkSelectedTimezones() {
for(int j = 0; j < adapter.getCount(); j++) {
if (selectedTimezones.contains(adapter.getItem(j))) {
listView.setItemChecked(j, true);
} else {
listView.setItemChecked(j, false);
}
}
}
}
@@ -102,6 +102,7 @@
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:onClick="selectTimezones"
android:tint="@android:color/white"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dragosholban.com.timezoneconverter.SelectTimezonesActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showChecked"
android:text="Show Selected" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="uncheckAll"
android:text="Uncheck All" />

</LinearLayout>

<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:choiceMode="multipleChoice"
app:layout_constraintBottom_toTopOf="@+id/doneButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

<Button
android:id="@+id/doneButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:onClick="done"
android:text="Done"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

0 comments on commit dd09b5f

Please sign in to comment.
You can’t perform that action at this time.