Permalink
Browse files

Back to Main Activity with the Selected Time Zone

  • Loading branch information...
dragosholban committed Mar 10, 2018
1 parent 338756e commit 85da4af7a27a903d0d2a8c4cdc8fab38ecbdf13c
@@ -11,10 +11,15 @@
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
Date localDate = new Date();
Button dateBtn;
Button selectTimeZoneBtn;
TimeZone userTimeZone;
private static int CHOOSE_TIME_ZONE_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -48,6 +53,8 @@ public void onStopTrackingTouch(SeekBar seekBar) {
dateBtn = findViewById(R.id.dateButton);
dateBtn.setText(DateFormat.getDateInstance().format(localDate));
selectTimeZoneBtn = findViewById(R.id.timeZoneButton);
}
public void showDatePicker(View view) {
@@ -68,6 +75,15 @@ public void setLocalDate(Date date) {
public void chooseTimezone(View view) {
Intent intent = new Intent(this, TimeZoneActivity.class);
startActivity(intent);
startActivityForResult(intent, CHOOSE_TIME_ZONE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CHOOSE_TIME_ZONE_REQUEST_CODE && resultCode == RESULT_OK) {
String timezone = data.getStringExtra("timezone");
selectTimeZoneBtn.setText(timezone);
userTimeZone = TimeZone.getTimeZone(timezone);
}
}
}
@@ -1,7 +1,10 @@
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.ListView;
@@ -17,8 +20,18 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_time_zone);
ListView listView = findViewById(R.id.listView);
ArrayList<String> timezones = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
final ArrayList<String> timezones = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, 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) {
Intent result = new Intent(getApplicationContext(), MainActivity.class);
result.putExtra("timezone", timezones.get(i));
setResult(RESULT_OK, result);
finish();
}
});
}
}

0 comments on commit 85da4af

Please sign in to comment.