Java tutorial
package com.android.projectz.teamrocket.thebusapp.activities; /* 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.pm.PackageManager; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.android.projectz.teamrocket.thebusapp.R; import com.android.projectz.teamrocket.thebusapp.adapters.CustomListSettingMain; import com.android.projectz.teamrocket.thebusapp.settings.SettingGeneralActivity; import com.android.projectz.teamrocket.thebusapp.settings.SettingLanguageActivity; import com.android.projectz.teamrocket.thebusapp.settings.ShowLicenceActivity; import com.android.projectz.teamrocket.thebusapp.util.SharedPreferencesUtils; /** * SettingsActivity: * classe importante perch il fulcro di tutti settaggi dell'app * suddivisione per tematica: * 1. Generale * 2. Lingua * 3. Notifiche * 4. Licenza * -- * Generale: vedi classe SettingGeneralActivity.class * Lingua: vedi classe SettingLanguageActivity.class * Notifiche: vedi classe SettingNotificationActivity.class * Licenza: vedi classe ShowLicensesActivity.class * -- * con l'ausilio di un custom list adapter vengono visualizzati i testi e le relative icone * * @author simone98dm * @date 03/12/2016 */ public class SettingsActivity extends AppCompatActivity { /* Questo metodo crea la pagina di impostazioni alla visualizzazione di essa e fa visualizzare anche l'impostazione desiderata */ @Override protected void onCreate(Bundle savedInstanceState) { this.setTheme(getResources().getIdentifier(SharedPreferencesUtils.getSelectedTheme(this), "style", getPackageName())); super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarSettings); setSupportActionBar(toolbar); getSupportActionBar().setTitle(R.string.action_settings); getSupportActionBar().setDisplayHomeAsUpEnabled(true); TextView t1, t2, t3; try { t1 = (TextView) findViewById(R.id.versionText); t1.setText("Versione: " + getPackageManager().getPackageInfo(getPackageName(), 0).versionName); t2 = (TextView) findViewById(R.id.copyrightText); t2.setText("App over GNU General Public License"); t3 = (TextView) findViewById(R.id.teamText); t3.setText("Copyright (C) 2016-2017 RockeTeam"); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } String[] settingText = { getResources().getString(R.string.setting_general), getResources().getString(R.string.setting_language), getResources().getString(R.string.setting_licenses), getResources().getString(R.string.action_changelog), getResources().getString(R.string.action_about) }; Drawable[] settingImg = { ContextCompat.getDrawable(this, R.drawable.ic_generals), ContextCompat.getDrawable(this, R.drawable.ic_language), ContextCompat.getDrawable(this, R.drawable.ic_licenses), ContextCompat.getDrawable(this, R.drawable.ic_changelog), ContextCompat.getDrawable(this, R.drawable.ic_about) }; CustomListSettingMain adapter = new CustomListSettingMain(SettingsActivity.this, settingText, settingImg); ListView list = (ListView) findViewById(R.id.settings_list); list.setAdapter(adapter); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: startActivity(new Intent(SettingsActivity.this, SettingGeneralActivity.class)); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; case 1: startActivity(new Intent(SettingsActivity.this, SettingLanguageActivity.class)); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; case 2: startActivity(new Intent(SettingsActivity.this, ShowLicenceActivity.class)); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; case 3: startActivity(new Intent(SettingsActivity.this, ChangelogActivity.class)); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; case 4: Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse( SharedPreferencesUtils.getWebsiteUrl(getApplicationContext()) + "/web/about.php")); startActivity(browserIntent); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left); break; default: Toast.makeText(SettingsActivity.this, "La tua richiesta ha generato un conflitto. Riprova", Toast.LENGTH_SHORT).show(); break; } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.finish(); SettingsActivity.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(); SettingsActivity.this.overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right); return true; } return super.onOptionsItemSelected(item); } }