add the Save functionality

This commit is contained in:
tibbi 2016-02-13 22:07:42 +01:00
parent 92cd1d8f31
commit 9f419c9221
6 changed files with 61 additions and 1 deletions

View File

@ -19,5 +19,4 @@
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -0,0 +1,6 @@
package notes.simplemobiletools.com;
public class Constants {
public static final String PREFS = "prefs";
public static final String TEXT = "text";
}

View File

@ -1,13 +1,58 @@
package notes.simplemobiletools.com;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SharedPreferences prefs;
private EditText notesView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
final String text = prefs.getString(Constants.TEXT, "");
notesView = (EditText) findViewById(R.id.notesView);
notesView.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
saveText();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void saveText() {
final String text = notesView.getText().toString().trim();
prefs.edit().putString(Constants.TEXT, text).apply();
Toast.makeText(this, "Text saved", Toast.LENGTH_SHORT).show();
hideKeyboard();
}
private void hideKeyboard() {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(notesView.getWindowToken(), 0);
}
}

View File

@ -6,6 +6,7 @@
android:padding="@dimen/activity_margin">
<EditText
android:id="@+id/notesView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/save"
android:icon="@mipmap/check"
android:title="Save"
app:showAsAction="always"/>
</menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B