Professional Documents
Culture Documents
Output:
Saving first note:
Note Saved:
Saving another note:
Notes list:
Saving more notes:
Now closing the application:
Re-running the application:
We get all the saved notes:
Editing the third note:
Note list updated:
Again closing the app:
Re-running the app:
We get the saved list (updated)
For saving data in shared preferences: (On Save button click)
int length=DummyContent.ITEMS.size();
SharedPreferences preferences =
getSharedPreferences("Sharedpreferences",MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
// editor.putString("EDITED TEXT",text);
editor.putInt("NOTES LENGTH",length);
editor.apply();
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import com.inderjit.androidassessment2.dummy.DummyContent;
import java.text.SimpleDateFormat;
import java.util.Date;
TextView id_TV;
int position;
EditText descNote,et_desc;
//for default constructor
SharedPreferrencesNoteDetails sharedNoteDetails = new
SharedPreferrencesNoteDetails();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_details);
id_TV = findViewById(R.id.ET_ID_notes);
Intent intent = getIntent();
if(intent.hasExtra("note_position"))
{
position = intent.getIntExtra("note_position",0);
id_TV.setText(DummyContent.ITEMS.get(position).id);
((TextView)findViewById(R.id.ET_Description_Notes)).setText(DummyContent.ITEMS.get(po
sition).content);
}
else if(intent.hasExtra("note_time"))
{
String currentTime = intent.getStringExtra("note_time");
String noteNumber = "" + (DummyContent.ITEMS.size()+1);
id_TV.setText(noteNumber);
((TextView)findViewById(R.id.ET_Description_Notes)).setText("New
Note.....");
}
if(Integer.parseInt(id_TV.getText().toString())<=(DummyContent.ITEMS.size())) {
EditText currentDescription = (EditText)
findViewById(R.id.ET_Description_Notes);
String description = currentDescription.getText().toString();
DummyContent.ITEMS.get(position).content = description;
finish();
}
else
{
Intent intent = getIntent();
String currentTime = intent.getStringExtra("note_time");
EditText currentDescription = (EditText)
findViewById(R.id.ET_Description_Notes);
String description = currentDescription.getText().toString();
description+="\nCreated on :" +currentTime;
DummyContent.addNewItem(description,Integer.parseInt(id_TV.getText().toString()+1));
finish();
}
*/
///New code begins
if(Integer.parseInt(id_TV.getText().toString())<=(DummyContent.ITEMS.size()))
{
EditText currentDescription = (EditText)
findViewById(R.id.ET_Description_Notes);
String description = currentDescription.getText().toString();
DummyContent.ITEMS.get(position).content = description;
finish();
}
else
{
Intent intent = getIntent();
String currentTime = intent.getStringExtra("note_time");
EditText currentDescription = (EditText)
findViewById(R.id.ET_Description_Notes);
String description = currentDescription.getText().toString();
//description+="\nCreated on :" +currentTime;
DummyContent.addNewItem(description,Integer.parseInt(id_TV.getText().toString()+1));
finish();
}
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.inderjit.androidassessment2.dummy.DummyContent;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.Gson;
import android.content.Intent;
import android.content.SharedPreferences;
public class MainActivity extends AppCompatActivity implements
NoteFragment.MyNotesOnClickListener {
TextView id_TV;
int position;
SharedPreferrencesNoteDetails sharedNoteDetails ;
EditText descNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NoteFragment notes = new NoteFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_notes,notes,"notesFragment")
.commit();
//added code
onloadData(this);
/*
//added code for notes description
descNote = findViewById(R.id.ET_Description_Notes);
//load the shared data and populate in Et variable
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String jsonText= preferences.getString("NOTE DATA","");
if(jsonText.length()>0)
{
Gson gson = new Gson();
sharedNoteDetails=
gson.fromJson(jsonText,SharedPreferrencesNoteDetails.class);
descNote.setText(sharedNoteDetails.description);
}
*/
}
//added method
private void onloadData(Context con)
{
DummyContent.loadData(con);
}
@Override
public void onClick(int position) {
Toast.makeText(this, "Selected Note : "+ DummyContent.ITEMS.get(position),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,NoteDetails.class);
intent.putExtra("note_position" ,position);
// startActivity(intent);
startActivityForResult(intent, 1000);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast.makeText(this, "Changes saved", Toast.LENGTH_SHORT).show();
NoteFragment notesFragment= (NoteFragment)
getSupportFragmentManager().findFragmentByTag("notesFragment");
if(notesFragment!=null)
notesFragment.refresh();
Toast.makeText(this, "Changes saved", Toast.LENGTH_SHORT).show();
}
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.inderjit.androidassessment2.R;
import com.inderjit.androidassessment2.SharedPreferrencesNoteDetails;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.SharedPreferences;
import android.widget.EditText;
import com.google.gson.Gson;
/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String,
DummyItem>();
}
*/
////added method
public static void loadData(Context con)
{
SharedPreferences preferences =
con.getSharedPreferences("Sharedpreferences",Context.MODE_PRIVATE);
notesLength= preferences.getInt("NOTES LENGTH",0);
for (int i = 1; i <= notesLength; i++) {
addItem(createDummyItem(i,con));
}
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String id;
public String content;
public final String details;
}
@Override
public String toString() {
return content;
}
}
////
private static DummyItem createNewItem(String content,int position) {
return new DummyItem(String.valueOf(position), content + "",
makeDetails(position));
}
/////
public static void addNewItem(String desc,int i)
{
addItem(createNewItem(desc,COUNT+1));
COUNT=COUNT+1;
}
}
SharedPreferencesNoteDetails.java
package com.inderjit.androidassessment2;
String id;
public String description;
int notesLength;
@Override
public String toString() {
return "Note{" +
"id='" + id + '\'' +
", description='" + description + '\'' +
'}';
}
MynoteRecyclerViewAdapter.java
package com.inderjit.androidassessment2;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.inderjit.androidassessment2.dummy.DummyContent.DummyItem;
import java.util.List;
/**
* {@link RecyclerView.Adapter} that can display a {@link DummyItem}.
* TODO: Replace the implementation with code for your data type.
*/
public class MynoteRecyclerViewAdapter extends
RecyclerView.Adapter<MynoteRecyclerViewAdapter.ViewHolder> {
//listener
NoteFragment.MyNotesOnClickListener myNotesListener;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_note, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
//setting on click listener to edit button
holder.edit_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//referencing the method of interface
myNotesListener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
NoteFragment.java
package com.inderjit.androidassessment2;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.inderjit.androidassessment2.dummy.DummyContent;
/**
* A fragment representing a list of Items.
*/
public class NoteFragment extends Fragment {
MyNotesOnClickListener myNotesListener;
MynoteRecyclerViewAdapter adapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public NoteFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes_list, container, false);
// recyclerView.setAdapter(new
MynoteRecyclerViewAdapter(DummyContent.ITEMS,myNotesListener));
adapter= new
MynoteRecyclerViewAdapter(DummyContent.ITEMS,myNotesListener);
recyclerView.setAdapter(adapter);
}
return view;
}
void refresh()
{
adapter.notifyDataSetChanged();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
myNotesListener = (MyNotesOnClickListener) context;
}
interface MyNotesOnClickListener{
void onClick(int position);
}
}
activity_note_details.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteDetails">
<TextView
android:layout_marginTop="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/TV_Title"
android:layout_marginBottom="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="My Note Details"
android:textColor="#000000"
android:textSize="40sp"
/>
<TextView
android:id="@+id/TV_Title"
android:layout_marginTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/ET_ID_notes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="Note Number"
android:textColor="#000000"
android:textSize="25sp"
/>
<TextView
android:id="@+id/ET_ID_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/TV_Title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:hint="Note ID"
/>
<TextView
android:layout_height="wrap_content"
android:height="1dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@id/ET_ID_notes"
android:background="#a4c639"
/>
<TextView
android:id="@+id/TV_Content"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/ET_ID_notes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textColor="#000000"
android:text="Description :"
android:textSize="25sp"
/>
<EditText
android:id="@+id/ET_Description_Notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/TV_Content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:hint="Note-Content"
/>
<TextView
android:layout_height="wrap_content"
android:height="1dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@id/ET_Description_Notes"
android:background="#a4c639"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/ET_Description_Notes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="24dp"
android:background="@drawable/add_btn_layout"
android:onClick="saveOnClick"
android:text="Save"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="24dp"
android:background="@drawable/add_btn_layout"
android:onClick="cancelOnClick"
android:text="Cancel"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_note.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<TextView
android:id="@+id/item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_margin="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:layout_height="wrap_content"
android:height="1dp"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@id/content"
android:background="#a4c639"
/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200dp"
android:layout_marginLeft="30dp"
android:layout_margin="20dp"
android:hint="Content Tv"
app:layout_constraintLeft_toRightOf="@id/item_number"
app:layout_constraintTop_toTopOf="parent"
android:textAppearance="?attr/textAppearanceListItem" />
<Button
android:id="@+id/btn_edit"
android:text="Edit"
android:minHeight="30dp"
android:minWidth="50dp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="@drawable/add_btn_layout"
android:textColor="#FF0000"
app:layout_constraintLeft_toRightOf="@id/content"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_note_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="com.inderjit.androidassessment2.noteFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".NoteFragment"
tools:listitem="@layout/fragment_note" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/Add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="+"
android:background="@drawable/add_btn_layout"
android:textSize="30sp"
android:gravity="center"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="20dp"
android:onClick="AddOnClick"
/>
<FrameLayout
android:layout_marginTop="30dp"
android:id="@+id/fragment_notes"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>