title: How to create Settings with toolbar in android
body: <p>I have a Settings Preference screen.It has a ListPreference and a CheckBoxPreference. I want to change my app's date format when I choose an item of ListPreference. Also, by the CheckBoxPreference I want to show/hide notification on the status bar. Can anyone tell what I have to do to achieve that.</p>
  
  <p>Also, how can I add a toolbar to the preference screen?  I am stuck here. Please help. Thanks in advance. 
  I am stuck here. Please help.
  Thanks in advance.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public void setCurrentDateOnView() {
      String dateFormat = "dd - MM - yyyy";
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
      tv_Current_Date.setText(simpleDateFormat.format(calendar_now.getTime()));
      String short_weekday = new DateFormatSymbols().getShortWeekdays()[day_of_current_week];
      tv_Current_weekday.setText(short_weekday);
      til_Current_Date.setError(null);
  }
  
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
  
          case R.id.action_settings:
              Intent intent_settings = new Intent(this, SettingsActivity.class);
              startActivity(intent_settings);
              Toast.makeText(this, "You have clicked on settings action menu.", Toast.LENGTH_SHORT).show();
              break;
  
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>SettingsActivity.java</p>
  
  <pre><code>public class SettingsActivity extends PreferenceActivity
      implements SharedPreferences.OnSharedPreferenceChangeListener {
  
  NotificationManager mNotifyManager;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      //Display the fragment as the main content.
      getFragmentManager().beginTransaction()
              .replace(android.R.id.content, new SettingsFragment())
              .commit();
      PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
  
      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  
      boolean notifyEnabled = sharedPreferences.getBoolean("pref_cb_notification", true);
      mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      if (notifyEnabled) {
          //Show notification
          showNotification();
      }
      else {
          //Hide notification
          hideNotification();
      }
  }
  
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  
          boolean isChecked = sharedPreferences.getBoolean("pref_cb_notification", false);
          if (isChecked) {
              //Show notification
              showNotification();
          }
          else {
              //Hide notification
              hideNotification();
          }
  
  }
  
  public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          //Load the preferences from an XML resource
          addPreferencesFromResource(R.xml.preferences);
      }
  
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  
      }
  }
  
  //Method to show notification
  public void showNotification() {
      NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
              new NotificationCompat.Builder(SettingsActivity.this)
                      .setSmallIcon(R.drawable.ic_notifications_white_24dp)
                      .setContentTitle("My Application")
                      .setSubText("Tap to start");
      Intent resultIntent = new Intent(SettingsActivity.this, MainActivity.class);
      PendingIntent resultPendingIntent = PendingIntent
              .getActivity(SettingsActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      //System.currentTimeMillis();
      mBuilder.setContentIntent(resultPendingIntent);
      Notification notification = mBuilder.build();
      notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
      //notification.flags |= Notification.FLAG_AUTO_CANCEL;
      mNotifyManager.notify(1, notification);
  }
  
  //Method to hide notification
  public void hideNotification() {
      mNotifyManager.cancel(1);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><a href="https://i.stack.imgur.com/63pQd.jpg" rel="nofollow noreferrer">Settings image</a></p>