Here is a sample implementation of the app design described:
-
Create a new Android Studio project with a blank activity.
-
Create a layout file for the main screen activity: "activity_main.xml"
<!-- activity_main.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search contacts" />
<ListView
android:id="@+id/contactListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/addContactButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_contact" />
</LinearLayout>
- Create a layout file for the edit contact screen activity: "activity_edit_contact.xml"
<!-- activity_edit_contact.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/phoneEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_save" />
<ImageView
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete" />
</LinearLayout>
</LinearLayout>
- Create an adapter to display the list of contacts in the main screen:
// ContactAdapter.java
public class ContactAdapter extends ArrayAdapter<String> {
private List<String> contacts;
public ContactAdapter(Context context, List<String> contacts) {
super(context, 0, contacts);
this.contacts = contacts;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
String contact = contacts.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(contact);
return convertView;
}
}
- Modify the MainActivity to handle the main screen functionality:
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView contactListView;
private SearchView searchView;
private List<String> contacts;
private ContactAdapter contactAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactListView = findViewById(R.id.contactListView);
searchView = findViewById(R.id.searchView);
contacts = new ArrayList<>();
contacts.add("John Doe");
contacts.add("Jane Smith");
// Add more contacts...
contactAdapter = new ContactAdapter(this, contacts);
contactListView.setAdapter(contactAdapter);
// Set up onClick listeners
contactListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showDetailScreen(contacts.get(position));
}
});
ImageView addContactButton = findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showEditContactScreen();
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Filter the contacts list based on the search query and update the listview
filterContacts(newText);
return false;
}
});
}
private void showDetailScreen(String contact) {
// TODO: Implement detail screen activity
}
private void showEditContactScreen() {
// TODO: Implement edit contact screen activity
}
private void filterContacts(String query) {
List<String> filteredContacts = new ArrayList<>();
for (String contact : contacts) {
if (contact.toLowerCase().contains(query.toLowerCase())) {
filteredContacts.add(contact);
}
}
contactAdapter.clear();
contactAdapter.addAll(filteredContacts);
contactAdapter.notifyDataSetChanged();
}
}
This implementation provides the basic functionality for storing and displaying contacts. You will need to handle saving, updating, and deleting contacts in the EditContact activity and implement the Detail activity to display the full details of a contact.