I want to add search on action bar but it doesn't work correctly. I put my code below:
Menu:
<?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/menu_search"
android:title="#string/search_title"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
this is code for onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView)MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
res/xml/searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
layout activity_search_result.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textViewSearchResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
My Activity
public class SearchResultActivity extends Activity {
TextView mTextViewSearchResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
mTextViewSearchResult = (TextView)findViewById(R.id.textViewSearchResult);
if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {
handleSearch(getIntent().getStringExtra(SearchManager.QUERY));
}
}
private void handleSearch(String searchQuery) {
mTextViewSearchResult.setText(searchQuery);
}
}
Related
I want to make an app with a custom toolbar with a menu on the toolbar. The problem I have is that when I run the app and I click on the menu on the right side of the toolbar, the options do not appear but the menu box appears. One of the options closes the app and if I click the menu area where the exit option is the app closes.
Here is my activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/toolbar_color"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="#string/app_name"
app:logo="#drawable/applogo">
</androidx.appcompat.widget.Toolbar>
Here is my mainActivity.java:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.exit)
{
this.finishAffinity();
}
return true;
}
}
This is my menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="#string/new_entry"
android:id="#+id/add">
</item>
<item
android:title="#string/about"
android:id="#+id/about">
</item>
<item
android:title="#string/exit"
android:id="#+id/exit">
</item>
I created the toolbar xml I set it in MainActivity .. I created the menu xml and I inflate it in MainActivity from all the tutorials these are the steps that I had to go through to get here, yet I don't know why the items in the menu xml file won't show up in the emulator.
Big thanks to Gabriele Mariotti. In his words: " The issue is the text color in the popup menu." .All I had to do was to was to add the following line of code to the Toolbar xml.
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Try this one I have made a demo for you it is working I have tested it on the emulator as well as the device.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_700"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!" />
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
}
my_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="#+id/item2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never" />
<item
android:id="#+id/item4"
android:title="Item 4"
app:showAsAction="never" />
</menu>
I want to make it so that the search view inside my toolbar takes all the available space. As of now, it doesn't, instead, it leaves a slither of unused space right next to the back button. I want it to take up more of that space.
Here is a picture:
Here is my Menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ticker_searchview"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/tickersearch"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>
Here is my Toolbar XML code:
<?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/White"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the relevant java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tickersearch_menu, menu);
SearchView tickerSearchView = (SearchView) menu.findItem(R.id.ticker_searchview).getActionView();
tickerSearchView.setQueryHint("Ticker Symbol or Name");
tickerSearchView.setIconifiedByDefault(false);
tickerSearchView.setMaxWidth(Integer.MAX_VALUE);
TextView tickerSearchViewTextView = tickerSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
tickerSearchViewTextView.setTypeface(font);
tickerSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
final Intent toStockInfo = new Intent(Search.this, stockinfo.class);
#Override
public boolean onQueryTextSubmit(String query) {
toStockInfo.putExtra("STOCKTICKER", query.toUpperCase());
startActivity(toStockInfo);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
newSearchUpdateRecyclerContent(newText.toUpperCase());
return true;
}
});
return true;
}
What is your toolbar XML file?
Do this in your toolbar.xml
android:layout_width="match_parent"
in my fragment i added a toolbar now am trying to add here a searchView which expands and collapse its working fine on a action bar but am having layout issue while using this on my toolbar :
here's my toolbar and below of that there's 2 buttons
my issue #1 is that here my icon of searchView is not visible but when you touch below the clock (5:16 in the status bar ) , searchView expands see second image
you can see that now searchView is visible and the title (its a textView) of toolbar shifted to left for making room for the searchView which is also a issue and when am typing anything on search view the colour of text is not visible (but searchView is working fine ) only the searched text is not visible in the searchView
when i again tap below at the clock my searchView collapses and title also set on its right position
here's my xml file of fragment :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:id="#+id/toolbar"
android:theme="#style/ToolBarStyle"
android:elevation="5dp"
android:textAlignment="center"
android:background="#android:color/holo_red_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Channel"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
android:textSize="20sp"
android:textStyle="bold"
android:background="#00000000"
android:textColor="#ffffff" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar">
.... other ui elements
</RelativeLayout>
</RelativeLayout>
searchView menu xml file :
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/done"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
this fragment is inside a tabLayout and i can't use Actionbar instead of toolbar
any idea how can i fix this ?or why its happening ? please let me know it'll be so helpful for me
inside my fragment class :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.menu_action_next){
//Do whatever you want to do
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
final MenuItem searchItem = menu.findItem(R.id.action_search);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuInflater menuInflater = inflater;
menuInflater.inflate(R.menu.search_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView = null;
if (searchItem != null) {
searchView = (android.support.v7.widget.SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}
if (searchView != null )
{
searchView.setOnClickListener(onClickListener);
searchView.setOnQueryTextListener(onQueryTextListener);
searchView.setOnCloseListener(onCloseListener);
}
}
UPDATE:
after adding app:showAsAction="ifRoom|collapseActionView" in my search menu xml (thanks to #Ashwinikutre) i got this
now icon is visible
and its expanding by removing title which is fine but no text colour and close button (which is now on another side but its invisible )
Try to explicitly add the following attribute to show it is a search view
<?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:icon="#drawable/search"
android:orderInCategory="100"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
Of course, you probably have this correctly setup too
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.global_search, menu);
final MenuItem item = menu.findItem(R.id.action_search);
MenuItemCompat.expandActionView(item);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setQueryHint(getString(R.string.search_customers));
searchView.setOnQueryTextListener(this);
return true;
}
Let me know if it helps!
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="80"
android:title="#string/action_search"
app:actionLayout="#layout/search"
app:showAsAction="ifRoom|collapseActionView" />
search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical">
<EditText
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:inputType="text|textAutoCorrect"
android:imeOptions="actionSearch"
android:background="#android:color/transparent"/>
<ImageButton
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close_white_24dp"
android:contentDescription="#string/button_close"
style="#android:style/Widget.ActionButton"/>
</LinearLayout>
I want to create a menu in my android app but it doesn't appear after running. this is my code
Setting.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fr.learning_adventure.android.itac.android_app_activity.EspacePersonnelActivity"> <item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="connexion"
/>
</menu>
EspacePersonnelActivity:
public class EspacePersonnelActivity extends ActionBarActivity {
private WifiManager wifiManager;
private Learner learner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_espacepersonnel);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
return true;
}
Try this...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fr.learning_adventure.android.itac.android_app_activity.EspacePersonnelActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
Hope this help you...
This can be considered as a extension of this Question
I have added following to the code to get a searchView Widget
res/menu/main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Mainactivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
System.out.println("TESTING: "+searchView);
return super.onCreateOptionsMenu(menu);
}
---
---
}
The app shows up fine but on touching the search button nothing shows up.
Have set the Sdk versions as follows:(4.0.3 -4.2.2)
android:minSdkVersion="15"
android:targetSdkVersion="17"
Change you menu to,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always" />
</menu>
You're missing "collapseActionView" in your menu:
app:showAsAction="ifRoom|collapseActionView"