VOOZH about

URL: https://www.geeksforgeeks.org/android/implementing-search-for-a-specific-blog-on-home-page-and-logout-functionality-in-social-media-android-app/

⇱ Implementing Search for a Specific Blog On Home Page and Logout Functionality in Social Media Android App - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing Search for a Specific Blog On Home Page and Logout Functionality in Social Media Android App

Last Updated : 23 May, 2024

This is the Part 7 of "Build a Social Media App in Android Studio" tutorial, and we are going to cover the following functionalities in this article:

  • We are going to Search For a Blog on Home Page.
  • If there are few blogs in our app then it is easy to search for a blog manually. But what happens when we have 1000 blogs then search for a particular blog became a very complicated and time taking task.
  • That's why we are implementing this feature to search for a blog using the title or description provided.
  • Also, we have implemented the Logout functionality in this article.

Step By Step Implementation

Step 1: Create a Logout and Search Button in the menu folder

Go to the app > res > menu > right-click > New > Menu Resource File and name the file as main_menu. Below is the code for the main_menu.xml file.

Step 2: Working with the HomeFragment.java file

We are searching for a blog in Node "Posts" using title and description value. If any key-value matches then it will all those blogs whose value contains our search content

if(modelPost.getTitle().toLowerCase().contains(search.toLowerCase())||
 modelPost.getDescription().toLowerCase().contains(search.toLowerCase())) {
 posts.add(modelPost);
 }

Below is the code for the Logout functionality

@Override
 public boolean onOptionsItemSelected(@NonNull MenuItem item) {

 if (item.getItemId() == R.id.logout) {
 firebaseAuth.signOut();
 startActivity(new Intent(getContext(), SplashScreen.class));
 getActivity().finish();
 }

 return super.onOptionsItemSelected(item);
 }

Go to the HomeFragment.java file and refer to the following code. Below is the complete and updated code for the HomeFragment.java file.

Output:

Note: Please Add drawable items before running the Application

Below is the file structure after performing these operations:

👁 Image
Comment
Article Tags:
Article Tags:

Explore