title: Android How to save text from Edit Text to Shared Preferences on button click
body: <p>Either I can enter a text on Edit Text or choose a pre-defined text from the spinner.If I choose from spinner, I am displaying it on Edit Text only. I want if I click on save button, the text from Edit Text will get saved to Shared Preferences. Right now, I am saving the text on <code>onAfterTextChanged()</code> but I want it to get saved on press of button. Please Suggest.I even followed the tutorials but didn't get much.</p>
  
  <pre><code>MainActivity.java
  
  
      private EditText message;
      Spinner spinner;
      Button save;
        @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final SharedPreferences pref = PreferenceManager
              .getDefaultSharedPreferences(this);
      save = (Button) findViewById(R.id.save);
      save.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
              pref.edit().putString("autoSave", message.getText().toString()).commit();     
          }
      }) ;
  
  
  
      message = (EditText) findViewById(R.id.et_message);
      // message.setFocusable(false);
  
      spinner = (Spinner) findViewById(R.id.Spin_alert_message);
      dbHelper = new MySQLiteHelper(MainActivity.this);
      selectedContactList = dbHelper.getAllContacts();
      ArrayAdapter&lt;String&gt; adp = new ArrayAdapter&lt;String&gt;(
              getApplicationContext(), android.R.layout.simple_spinner_item,
              alert);
      spinner.setAdapter(adp);
  
      spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1,
                  int position, long id) {
              // message.setSelection(pref.getString("AutoSave",""));
              String selectedItem = alert[position];
              message.setText(selectedItem);
  
              // loads the text that has been stored to SP and set it to
              // EditText
  
              // to bring cursor to front
              message.setSelection(message.getText().length());
  
  
  
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          }
      });
  
      /************************** saving custom message *****************************/
  
  
  
  
  
      ListView contactlist = (ListView) findViewById(R.id.contactListitems);
      Resources res = getResources();
      adapter = new CustomAdapter(MainActivity.this, selectedContactList,
              res, MainActivity.this);
      contactlist.setAdapter(adapter);
  </code></pre>