Junius L
September 26, 2023
4 min read
Junius L
September 26, 2023
4 min read
Before we dive into the tutorial, make sure you have the following set up:
In your Android Studio project, open the activity_main.xml layout file for your MainActivity. We will add three essential elements: an EditText for user input, a Button to save the input, and a ListView to display the saved items. Here's how to do it:
<EditText
android:id="@+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your text"/>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>Now, let's move to the MainActivity.java file. We need to implement the logic for saving user input in an array and displaying it in the ListView. Additionally, we'll persist this data using SharedPreferences. Here's the code for that:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText textInput;
private Button saveButton;
private ListView listView;
private ArrayList<String> dataList;
private ArrayAdapter<String> adapter;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInput = findViewById(R.id.textInput);
saveButton = findViewById(R.id.saveButton);
listView = findViewById(R.id.listView);
// Initialize the ArrayList and ArrayAdapter
dataList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(adapter);
// Initialize SharedPreferences
sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
// Load data from SharedPreferences
loadData();
// Save user input on button click
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = textInput.getText().toString().trim();
if (!input.isEmpty()) {
dataList.add(input);
adapter.notifyDataSetChanged();
textInput.setText("");
saveData();
}
}
});
// Handle item removal on item click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
dataList.remove(position);
adapter.notifyDataSetChanged();
saveData();
}
});
}
// Save data to SharedPreferences
private void saveData() {
SharedPreferences.Editor editor = sharedPreferences.edit();
Set<String> dataSet = new HashSet<>(dataList);
editor.putStringSet("dataSet", dataSet);
editor.apply();
}
// Load data from SharedPreferences
private void loadData() {
Set<String> dataSet = sharedPreferences.getStringSet("dataSet", new HashSet<String>());
dataList.addAll(dataSet);
adapter.notifyDataSetChanged();
}
}In this final step, we'll add a button in the MainActivity to navigate to another activity (MainActivity2) and display the stored data in a ListView there. Here's how to do it:
Modify activity_main.xml
Add a button to navigate to MainActivity2:
<Button
android:id="@+id/goToMainActivity2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to MainActivity2"/>Modify MainActivity.java
Add an onClick listener to the button:
Button goToMainActivity2Button = findViewById(R.id.goToMainActivity2Button);
goToMainActivity2Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});Create a new activity called MainActivity2 in Android Studio, and in its activity_main2.xml layout file, add a ListView with the id listView2:
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>Now, in the MainActivity2.java file, load and display the data from SharedPreferences similar to what we did in MainActivity:
public class MainActivity2 extends AppCompatActivity {
private ListView listView2;
private ArrayAdapter<String> adapter;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView2 = findViewById(R.id.listView2);
// Initialize ArrayAdapter
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
// Initialize SharedPreferences
sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
// Load data from SharedPreferences
loadData();
// Set the adapter for listView2
listView2.setAdapter(adapter);
}
// Load data from SharedPreferences
private void loadData() {
Set<String> dataSet = sharedPreferences.getStringSet("dataSet", new HashSet<String>());
adapter.addAll(dataSet);
}
}With these steps completed, you have successfully created an Android app that stores user input in an array, persists it using SharedPreferences, and displays the list in another activity. This tutorial covers the basics, but you can expand upon it by adding features like editing and deleting items or enhancing the user interface to make your app more functional and user-friendly. Happy coding!