Java tutorial
package com.android.projectz.teamrocket.thebusapp.settings; /* Copyright (C) 2016-2017 TeamRocket This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.afollestad.materialdialogs.MaterialDialog; import com.android.projectz.teamrocket.thebusapp.R; import com.android.projectz.teamrocket.thebusapp.activities.BugReportActivity; import com.android.projectz.teamrocket.thebusapp.activities.MainActivity; import com.android.projectz.teamrocket.thebusapp.adapters.CustomListSettingOther; import com.android.projectz.teamrocket.thebusapp.util.SharedPreferencesUtils; import java.util.Locale; /** * SettingLanguageActivity: * questa classe permette di far visualizzare le impostazioni riguardanti il cambio lingua * e collegamenti al bug report (nel caso di errori nella traduzione) * * @author simone98dm * @date 08/12/2016 */ public class SettingLanguageActivity extends AppCompatActivity { private String compleateLanguage; /* Questo metodo crea il menu della selezione lingua */ @Override protected void onCreate(Bundle savedInstanceState) { this.setTheme(getResources().getIdentifier(SharedPreferencesUtils.getSelectedTheme(this), "style", getPackageName())); super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting_language); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarLanguageSetting); setSupportActionBar(toolbar); getSupportActionBar().setTitle(getString(R.string.title_activity_language)); getSupportActionBar().setSubtitle(getString(R.string.subtitle_activity_language)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } /** * Metodo che serve a far visualizzare la lista con le relative personalizzazioni all'interno della schermata * <p> * ho deciso di fare ci in modo tale da richiamare il metodo quando la lingua viene cambiata, cosi la vista * aggiornata con la lingua selezionata */ @Override protected void onStart() { super.onStart(); String[] settingText = { getString(R.string.setting_language), getString(R.string.commit_translation) }; String[] settingsubtext = { getCompleateLanguage(SharedPreferencesUtils.getSelectedLanguage(SettingLanguageActivity.this)), getString(R.string.language_bug_subtitle) }; Drawable[] settingImg = { ContextCompat.getDrawable(this, R.drawable.ic_language), ContextCompat.getDrawable(this, R.drawable.ic_report_language) }; CustomListSettingOther customList = new CustomListSettingOther(SettingLanguageActivity.this, settingText, settingsubtext, settingImg); ListView list = (ListView) findViewById(R.id.language_setting_list); list.setAdapter(customList); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { switch (position) { case 0: int languageCode = 0; switch (SharedPreferencesUtils.getSelectedLanguage(SettingLanguageActivity.this)) { case "it": languageCode = 0; break; case "en": languageCode = 1; break; case "fr": languageCode = 2; break; case "de": languageCode = 3; break; case "es": languageCode = 4; break; case "ua": languageCode = 5; break; case "kg": languageCode = 6; break; } new MaterialDialog.Builder(SettingLanguageActivity.this).title(R.string.dialog_title_language) .items(R.array.language) .itemsCallbackSingleChoice(languageCode, new MaterialDialog.ListCallbackSingleChoice() { @Override public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { try { SharedPreferencesUtils.setSelectedLanguage(SettingLanguageActivity.this, getShortLanguage(which)); setLocale(getShortLanguage(which)); } catch (Exception e) { e.printStackTrace(); setLocale("it"); } return true; } }).positiveText(R.string.dialog_ok_button).show(); break; case 1: Intent intent = new Intent(SettingLanguageActivity.this, BugReportActivity.class); intent.putExtra("PRECOMPILED_FORM", getString(R.string.language_bug_textfield)); startActivity(intent); SettingLanguageActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.finish(); SettingLanguageActivity.this.overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.finish(); SettingLanguageActivity.this.overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right); return true; } return super.onOptionsItemSelected(item); } /** * Ritorna il nome completo della lingua che si ha selezionato dall'archivio * * @param selectedLanguage * @return es. it->Italiano, en->English */ public String getCompleateLanguage(String selectedLanguage) { switch (selectedLanguage) { case "it": return "Italiano"; case "en": return "English"; case "fr": return "Franaise"; case "de": return "Deutsch"; case "es": return "Espaol"; case "ua": return "?"; case "kg": return "Veneto"; } return null; } /** * ritorna il nome abbreviato della lingua che si ha selezionato dall'archivio * * @param position * @return es. 0->it, 1->en */ private String getShortLanguage(int position) { switch (position) { case 0: return "it"; case 1: return "en"; case 2: return "fr"; case 3: return "de"; case 4: return "es"; case 5: return "ua"; case 6: return "kg"; } return null; } public void setLocale(String lang) { Resources res = this.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale(lang.toLowerCase()); res.updateConfiguration(conf, dm); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); /* FLAG_ACTIVITY_CLEAR_TOP If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. */ startActivity(intent); } }