Question: Android Studio Java Here the bugs that needed to be fix/objective that needed to be done: - Last option selected by the user should be

Android Studio Java

Here the bugs that needed to be fix/objective that needed to be done:

- Last option selected by the user should be selected when it opens the Settings screen again even when the user restart the application.

- The background music selected by the user should retain the state (play or stop) even the application has been restarted.

SettingsActivity:

package com.example.BibleApplication; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.media.MediaPlayer; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import com.scwang.wave.MultiWaveHeader; public class SettingsActivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater= getMenuInflater(); inflater.inflate(R.menu.main_menu,menu); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if(id==R.id.nav_home){ Intent intent = new Intent(SettingsActivity.this,TOCActivity.class); startActivity(intent); return true; } else if(id==R.id.nav_verses){ Intent intent = new Intent(SettingsActivity.this,ChapterActivity.class); startActivity(intent); return true; } else if(id==R.id.nav_quiz){ Intent intent = new Intent(SettingsActivity.this, QuizActivity.class); startActivity(intent); return true; } else if(id==R.id.nav_settings){ Intent intent = new Intent(SettingsActivity.this,SettingsActivity.class); startActivity(intent); return true; } else if(id==R.id.nav_about){ Intent intent = new Intent(SettingsActivity.this, AboutActivity.class); startActivity(intent); return true; } else if(id==R.id.nav_share) { Toast.makeText (this,"Shared", Toast.LENGTH_SHORT).show(); return true; } else if(id==R.id.nav_exit) { appExit(); } return super.onOptionsItemSelected(item); } private void appExit() { this.finish(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } MediaPlayer bgcmusic; View view; Toolbar toolbar; MultiWaveHeader wave_footer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); RadioButton rdWhiteMod = findViewById(R.id.rdWhiteMod); RadioButton rdNightMod = findViewById(R.id.rdNightMod); RadioButton On = findViewById(R.id.rdOnBgm); RadioButton Off = findViewById(R.id.rdOffBgm); TextView txtSetting = findViewById(R.id.txtSettings); TextView txtBackgroundColor = findViewById(R.id.txtBgc); TextView txtBgm = findViewById(R.id.txtBgm); wave_footer = findViewById (R.id.wave_footer); wave_footer.setVelocity (1); wave_footer.setProgress (1); wave_footer.isRunning (); wave_footer.setGradientAngle (45); wave_footer.setWaveHeight (40); wave_footer.setStartColor (Color.CYAN); wave_footer.setCloseColor (Color.GREEN); toolbar = findViewById (R.id.toolbar); setSupportActionBar (toolbar); SettingsActivity.this.setTitle ("Settings"); txtBgm.setText("Background Music"); txtSetting.setText("Settings"); txtBackgroundColor.setText("Theme Color"); bgcmusic = MediaPlayer.create(SettingsActivity.this, R.raw.bgcmusic); SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences sharedPref = getSharedPreferences("bgColorFile", Context.MODE_PRIVATE); int colorValue = sharedPref.getInt("color", 0); View view = this.getWindow().getDecorView(); view.setBackgroundColor(colorValue); rdWhiteMod.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rdNightMod.setChecked(false); int rdWhiteMod = Color.parseColor("#faf3e0"); View view = SettingsActivity.this.getWindow().getDecorView(); view.setBackgroundResource(R.color.color4); SharedPreferences sharedPref = SettingsActivity.this.getSharedPreferences("bgColorFile", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("color", rdWhiteMod); editor.apply(); } }); rdNightMod.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rdWhiteMod.setChecked(false); int rdNightMod = Color.parseColor("#222831"); View view = SettingsActivity.this.getWindow().getDecorView(); view.setBackgroundResource(R.color.color1_background); SharedPreferences sharedPref = SettingsActivity.this.getSharedPreferences("bgColorFile", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("color", rdNightMod); editor.apply(); } }); On.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Off.setChecked(false); Music.startMusic(); SharedPreferences m_pref = SettingsActivity.this.getSharedPreferences("music", MODE_PRIVATE); SharedPreferences.Editor editor = m_pref.edit(); editor.putBoolean("music", true); editor.apply(); } }); Off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { On.setChecked(false); Music.stopMusic(); SharedPreferences m_pref = SettingsActivity.this.getSharedPreferences("music", MODE_PRIVATE); SharedPreferences.Editor editor = m_pref.edit(); editor.putBoolean("music", false); editor.apply(); } }); } } 

TOCActivity:

import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import com.google.android.material.navigation.NavigationView; public class TOCActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { DrawerLayout drawerLayout; NavigationView navigationView; Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_tableofcontent); SharedPreferences sharedPref = getSharedPreferences ("bgColorFile", Context.MODE_PRIVATE); int colorValue = sharedPref.getInt ("color", 0); View view = this.getWindow ().getDecorView (); view.setBackgroundColor (colorValue); if(Music.bgcmusic == null) Music.prepMusic(this, R.raw.bgcmusic); SharedPreferences m_pref = getSharedPreferences("music", Context.MODE_PRIVATE); boolean ifPlay = m_pref.getBoolean("music", false); if (ifPlay == false) Music.stopMusic(); else Music.startMusic(); Button btnVerses = (Button) findViewById (R.id.btnVerses); Button btnQuiz = (Button) findViewById (R.id.btnQuiz); Button btnSettings = (Button) findViewById (R.id.btnSettings); Button btnAbout = (Button) findViewById (R.id.btnAbout); drawerLayout = findViewById (R.id.drawer_layout); navigationView = findViewById (R.id.nav_view); toolbar = findViewById (R.id.toolbar); TextView txtTitle = (TextView) findViewById (R.id.txtTitle); txtTitle.setText ("Table Of Content"); setSupportActionBar (toolbar); TOCActivity.this.setTitle("Simple Bible App"); navigationView.bringToFront (); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle (this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener (toggle); toggle.syncState (); navigationView.setNavigationItemSelectedListener (this); btnVerses.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { Intent i = new Intent (TOCActivity.this, ChapterActivity.class); startActivity (i); } }); btnQuiz.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { Intent i = new Intent (TOCActivity.this, QuizActivity.class); startActivity (i); } }); btnSettings.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { Intent i = new Intent (TOCActivity.this, SettingsActivity.class); startActivity (i); } }); btnAbout.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { Intent i = new Intent (TOCActivity.this, AboutActivity.class); startActivity (i); } }); } @Override public void onBackPressed() { if (drawerLayout.isDrawerOpen (GravityCompat.START)) { drawerLayout.closeDrawer (GravityCompat.START); } else { super.onBackPressed (); } } @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId ()) { case R.id.nav_home: break; case R.id.nav_verses: Intent intent = new Intent (TOCActivity.this,ChapterActivity.class); startActivity (intent); break; case R.id.nav_quiz: Intent intent2 = new Intent (TOCActivity.this,QuizActivity.class); startActivity (intent2); break; case R.id.nav_settings: Intent intent3 = new Intent (TOCActivity.this,SettingsActivity.class); startActivity (intent3); break; case R.id.nav_about: Intent intent4 = new Intent (TOCActivity.this,AboutActivity.class); startActivity (intent4); break; case R.id.nav_share: Toast.makeText (this,"Shared", Toast.LENGTH_SHORT).show(); break; case R.id.nav_exit: appExit(); break; } return true; } private void appExit() { this.finish(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!