1

I've made a custom toolbar with a title and functioning popup menu, but I can't seem to get a search menu to show up in the toolbar. The search menu icon doesn't even show in the toolbar for this activity and I can't figure out why.

Main activity:

import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.appcompat.widget.Toolbar; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.PopupMenu; import android.widget.TextView; import java.util.ArrayList; public class Skills_main extends AppCompatActivity implements SkillsRecyclerViewInterface { ArrayList<SkillsListModel> skillsListModels = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_skills_main); RecyclerView recyclerView = findViewById(R.id.skillsRecyclerView); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); setUpSkillsListModels(); SkillsRecyclerViewAdapter adapter = new SkillsRecyclerViewAdapter(this, skillsListModels, this); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); ImageView menuIcon = findViewById(R.id.menu_icon); TextView title = findViewById(R.id.toolbar_title); String skillsTable = "Skills"; title.setText(skillsTable); menuIcon.setOnClickListener(this::showMenu); } private void setUpSkillsListModels() { String[] skillsHeaders = getResources().getStringArray(R.array.skillHeaders); String[] skillDescriptions = getResources().getStringArray(R.array.skillDescriptions); for (int i = 0; i < skillsHeaders.length; i++) { skillsListModels.add(new SkillsListModel(skillsHeaders[i],skillDescriptions[i])); } } private void showMenu(View v){ PopupMenu popupMenu = new PopupMenu(Skills_main.this,v); popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(item -> { if(item.getItemId()== R.id.appInfo) { Intent intent = new Intent(Skills_main.this, App_Info_Page.class); startActivity(intent); } if(item.getItemId()==R.id.appHome) { Intent intent = new Intent(Skills_main.this, MainActivity.class); startActivity(intent); } return true; }); popupMenu.show(); } @Override public void onItemClick(int position) { Intent intent = new Intent(Skills_main.this, SkillsDescriptions.class); intent.putExtra("TITLE", skillsListModels.get(position).getSkillsTitle()); intent.putExtra("DESCRIPTION", skillsListModels.get(position).getSkillsDescription()); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search_menu, menu); MenuItem item = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) item.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { return false; } }); return super.onCreateOptionsMenu(menu); } } 

Toolbar XML:

<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/list_background" android:elevation="8dp" android:id="@+id/toolbar" android:fitsSystemWindows="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/menu_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingVertical="15dp" android:contentDescription="@string/app_info" android:src="@drawable/baseline_menu_24" /> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginStart="40dp" android:fontFamily="@font/graduate" android:text="@string/app_name" android:textColor="#E3CDB3" android:textSize="22sp" /> </RelativeLayout> </androidx.appcompat.widget.Toolbar> 

Search menu XML:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:title="Search" android:icon="@drawable/baseline_search_24" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="androidx.appcompat.widget.SearchView"/> </menu> 

I added the setSupportActionBar(toolbar) lines to attempt to get the toolbar to behave like an action bar. I also tried using an ImageView similarly to how I made the Popup Menu, but still the search menu did not function.

1 Answer 1

0

It is because you have set this attribute to match parent , which is covering your toolbar,

 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 

change layout_width to wrap content, and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

This did not fix the issue, the search icon is still not showing in the toolbar for this activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.