mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-01-29 13:39:16 +01:00
add the Save functionality
This commit is contained in:
parent
92cd1d8f31
commit
9f419c9221
@ -19,5 +19,4 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
@ -0,0 +1,6 @@
|
||||
package notes.simplemobiletools.com;
|
||||
|
||||
public class Constants {
|
||||
public static final String PREFS = "prefs";
|
||||
public static final String TEXT = "text";
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
|
9
app/src/main/res/menu/menu.xml
Normal file
9
app/src/main/res/menu/menu.xml
Normal 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>
|
BIN
app/src/main/res/mipmap-hdpi/check.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/check.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 782 B |
Loading…
x
Reference in New Issue
Block a user