Skip to content

Commit 6d95177

Browse files
committed
Note Taking App
Stores Notes And Displays Previously stored Notes in App
1 parent e4da5d3 commit 6d95177

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

notetakingapp/MainActivity.java

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.lenovo.notetakingapp;
2+
3+
import android.content.Context;
4+
import android.content.DialogInterface;
5+
import android.content.Intent;
6+
import android.content.SharedPreferences;
7+
import android.support.v7.app.AlertDialog;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.view.Menu;
12+
import android.view.MenuInflater;
13+
import android.view.MenuItem;
14+
import android.view.View;
15+
import android.widget.AdapterView;
16+
import android.widget.ArrayAdapter;
17+
import android.widget.ListView;
18+
import android.widget.Toast;
19+
20+
import java.util.ArrayList;
21+
import java.util.HashSet;
22+
23+
public class MainActivity extends AppCompatActivity {
24+
25+
static ArrayList<String> notes = new ArrayList<>();
26+
static ArrayAdapter arrayAdapter;
27+
28+
@Override
29+
public boolean onCreateOptionsMenu(Menu menu) {
30+
MenuInflater menuInflater = getMenuInflater();
31+
menuInflater.inflate(R.menu.add_note_menu,menu);
32+
return super.onCreateOptionsMenu(menu);
33+
}
34+
35+
@Override
36+
public boolean onOptionsItemSelected(MenuItem item) {
37+
super.onOptionsItemSelected(item);
38+
if(item.getItemId()==R.id.add_note)
39+
{
40+
Intent intent = new Intent(getApplicationContext(),NoteEditorActivity.class);
41+
startActivity(intent);
42+
return true;
43+
}
44+
return false;
45+
46+
}
47+
@Override
48+
protected void onCreate(Bundle savedInstanceState) {
49+
super.onCreate(savedInstanceState);
50+
setContentView(R.layout.activity_main);
51+
ListView listView = (ListView)findViewById(R.id.listView);
52+
final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.lenovo.notetakingapp", Context.MODE_PRIVATE);
53+
final HashSet<String> set = (HashSet<String>) sharedPreferences.getStringSet("notes",null);
54+
if (set ==null)
55+
{
56+
notes.add("Example notes");
57+
58+
}
59+
else
60+
{
61+
notes = new ArrayList(set);
62+
}
63+
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,notes);
64+
listView.setAdapter(arrayAdapter);
65+
66+
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
67+
@Override
68+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
69+
Intent intent = new Intent(getApplicationContext(),NoteEditorActivity.class);
70+
intent.putExtra("noteId",position);
71+
startActivity(intent);
72+
}
73+
});
74+
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
75+
76+
@Override
77+
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
78+
new AlertDialog.Builder(MainActivity.this)
79+
.setIcon(android.R.drawable.ic_dialog_alert)
80+
.setTitle("Are you Sure")
81+
.setMessage("Do you want to delete this note!!")
82+
.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
83+
84+
@Override
85+
public void onClick(DialogInterface dialog, int which) {
86+
notes.remove(position);
87+
arrayAdapter.notifyDataSetChanged();
88+
HashSet<String> set = new HashSet<String>(MainActivity.notes);
89+
sharedPreferences.edit().putStringSet("notes",set).apply();
90+
91+
}
92+
})
93+
.setNegativeButton("No",null)
94+
.show();
95+
96+
97+
return true;
98+
}
99+
}
100+
);
101+
}
102+
}

notetakingapp/NoteEditorActivity.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.lenovo.notetakingapp;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.content.SharedPreferences;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.text.Editable;
9+
import android.text.TextWatcher;
10+
import android.widget.EditText;
11+
12+
import java.util.HashSet;
13+
14+
public class NoteEditorActivity extends AppCompatActivity {
15+
16+
int noteid;
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_note_editor);
21+
22+
EditText editText =(EditText)findViewById(R.id.editText);
23+
Intent intent = getIntent();
24+
noteid= intent.getIntExtra("noteId",-1);
25+
if(noteid !=-1)
26+
{
27+
editText.setText(MainActivity.notes.get(noteid));
28+
}else
29+
{
30+
MainActivity.notes.add("");
31+
noteid=MainActivity.notes.size()-1;
32+
MainActivity.arrayAdapter.notifyDataSetChanged();
33+
}
34+
35+
editText.addTextChangedListener(new TextWatcher() {
36+
@Override
37+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
38+
39+
}
40+
41+
@Override
42+
public void onTextChanged(CharSequence s, int start, int before, int count) {
43+
MainActivity.notes.set(noteid,String.valueOf(s));
44+
MainActivity.arrayAdapter.notifyDataSetChanged();
45+
46+
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.lenovo.notetakingapp", Context.MODE_PRIVATE);
47+
HashSet<String> set = new HashSet<String>(MainActivity.notes);
48+
sharedPreferences.edit().putStringSet("notes",set).apply();
49+
50+
}
51+
52+
@Override
53+
public void afterTextChanged(Editable s) {
54+
55+
}
56+
});
57+
}
58+
}

0 commit comments

Comments
 (0)