Java tutorial
/* * Copyright (c) 2017 Francisco Jos Montiel Navarro. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.franmontiel.localechanger.sample; import android.app.DatePickerDialog; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.Spinner; import android.widget.TextView; import com.franmontiel.localechanger.LocaleChanger; import com.franmontiel.localechanger.utils.ActivityRecreationHelper; import java.util.Calendar; import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.Unbinder; import static com.franmontiel.localechanger.sample.SampleApplication.SUPPORTED_LOCALES; public class SampleFragment extends Fragment { @BindView(R.id.localeSpinner) Spinner localeSpinner; @BindView(R.id.currentLocale) TextView currentLocale; @BindView(R.id.date) TextView date; private Unbinder unbinder; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.screen_sample, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.sample_menu, menu); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); ArrayAdapter<Locale> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, SUPPORTED_LOCALES); localeSpinner.setAdapter(adapter); } @Override public void onResume() { super.onResume(); currentLocale.setText(Locale.getDefault().toString()); date.setText(DateProvider.provideSystemLocaleFormattedDate()); } @OnClick(R.id.localeUpdate) void onUpdateLocaleClick() { LocaleChanger.setLocale((Locale) localeSpinner.getSelectedItem()); ActivityRecreationHelper.recreate(getActivity(), false); } @OnClick(R.id.showDatePicker) void onShowDatePickerClick() { Calendar now = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)); dialog.show(); } @Override public void onDestroyView() { unbinder.unbind(); super.onDestroyView(); } }