From 9f419c922177c0f85b56963ab4477fc808cd5681 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 13 Feb 2016 22:07:42 +0100 Subject: [PATCH] add the Save functionality --- app/src/main/AndroidManifest.xml | 1 - .../simplemobiletools/com/Constants.java | 6 +++ .../simplemobiletools/com/MainActivity.java | 45 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 1 + app/src/main/res/menu/menu.xml | 9 ++++ app/src/main/res/mipmap-hdpi/check.png | Bin 0 -> 782 bytes 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/notes/simplemobiletools/com/Constants.java create mode 100644 app/src/main/res/menu/menu.xml create mode 100644 app/src/main/res/mipmap-hdpi/check.png diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dc36a360..88decb5d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,5 +19,4 @@ - diff --git a/app/src/main/java/notes/simplemobiletools/com/Constants.java b/app/src/main/java/notes/simplemobiletools/com/Constants.java new file mode 100644 index 00000000..f95fdfd9 --- /dev/null +++ b/app/src/main/java/notes/simplemobiletools/com/Constants.java @@ -0,0 +1,6 @@ +package notes.simplemobiletools.com; + +public class Constants { + public static final String PREFS = "prefs"; + public static final String TEXT = "text"; +} diff --git a/app/src/main/java/notes/simplemobiletools/com/MainActivity.java b/app/src/main/java/notes/simplemobiletools/com/MainActivity.java index 5fcc5c80..fba68e9f 100644 --- a/app/src/main/java/notes/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/notes/simplemobiletools/com/MainActivity.java @@ -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); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index abcf8fcb..d8b2928d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,6 +6,7 @@ android:padding="@dimen/activity_margin"> + + + diff --git a/app/src/main/res/mipmap-hdpi/check.png b/app/src/main/res/mipmap-hdpi/check.png new file mode 100644 index 0000000000000000000000000000000000000000..1f9c734187ac3deb01f785d02ddbd6101a2805cd GIT binary patch literal 782 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD0wg^q?%xcg7>k44ofy`glX(f`FeQ1ryD zTiH?K_{aHNPrRa&zRL@!sLyap;uS1-FCQHaoI; zW*jqN^C@mJSiqxzBB5CsNC4hIG%fd&Q^ zWM%`;@gFP1LmybITDAMSyI~#kmbU0$l5hPxu5I8?(C!TgVpyB>|7#-G!k|lk4ww6H z&#=~FD7AaSe8A6z;nf2RLzBN{dR+_$p51r(EWcn|Zrw_TE5|x>>pB^>ta*C<(r0hx z1*w0oep$5gQSzP_pe$i+7-A~QTD_%~MmvHc%v-2rCtHICD+kP@K zUP<`GesN9b%Er$}E;vp5u=Wv7^Upno6>Ho0fZug4J8rkK=rN95_ ziSB+S=-s)Hd)DVk`yV~NZ2p6DAWu_xYV(S)VlR!iH1-=S%#0q<{Z~$*Nru zK0I@03*Go*Z>QhmCT?`~wA%tr-T$la*F0Y7BF5zVFzWRGx7ArmHsYI94SwGL^E}%3 zSqMM3)WXb(^;3>NxsYYH>X+2R9_4`0)BorB@B3PEtHmMoPS#KMO5gKquQTf`G&TC~ zUiWjRmH6n$-)~S$<2WAm zFYjM=PP^+~sf4m4r=?w=SikdH@o;$y@7CZmdhL(5ol=*qYzqXr z&VJuwyu58i>&friwyszgz&&yPwzWbB70v&0Vx|g#mmhf^iC!pZWNC5&CRhefS3j3^ HP6