Question: ( give code to put into android studio using java and xml ) Application Title: Personal Diary App Purpose: To demonstrate the capabilities of Android's

(give code to put into android studio using java and xml) Application Title: Personal Diary App
Purpose:
To demonstrate the capabilities of Android's SharedPreferences by creating an application where
users can write and save personal diary entries.
Features and Algorithms:
1. App Structure:
The application should feature a simple and user-friendly interface for writing and viewing diary entries.
Implement `RecyclerView` for displaying a list of saved diary entries.
2. Diary Entry Creation and Display:
Users should be able to create new diary entries with a title and content.
Display a list of entry titles on the main screen. Selecting a title should display the full diary entry.
3. Use of SharedPreferences:
Utilize SharedPreferences to save and retrieve diary entries.
Demonstrate the use of `SharedPreferences.Editor` for writing data and `SharedPreferences` for reading data.
Each diary entry (title and content) should be stored as a separate preference.
4. Editing and Deleting Entries:
Provide options for users to edit or delete existing entries.
Reflect these changes immediately in the SharedPreferences and update the UI accordingly.
5. User Interface:
Design the app to be intuitive and easy to navigate.
Consider implementing a floating action button for adding new entries.
Use a clean and minimalist design to keep the focus on the content. To save:
// Assume 'saveButton' is your button and 'nameEditText' is your EditText
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String name = nameEditText.getText().toString();
// Getting SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
// Opening the SharedPreferences editor to write data
SharedPreferences.Editor editor = sharedPreferences.edit();
// Storing the name with a key "userName"
editor.putString("userName", name);
// Applying the changes
editor.apply();
Toast.makeText(MainActivity.this, "Name saved!", Toast.LENGTH_SHORT).show();
}
});
To retrieve:
// Getting SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
// Retrieving the value using the key. The second parameter is a default value if the key doesn't exist
String name = sharedPreferences.getString("userName", "Default Name");
// Now 'name' contains the stored value or "Default Name" if nothing was stored
Note: SharedPreferences is accessible throughout the application

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!