I have this menu filters.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">
<group android:checkableBehavior="single" android:id="#+id/menuGroup_1" >
<item android:id="#+id/a_1" android:title="a1" />
<item android:id="#+id/a_2" android:title="a2" />
</group>
<group android:checkableBehavior="single" android:id="#+id/menuGroup_2" >
<item android:id="#+id/b_1" android:title="b1" />
<item android:id="#+id/b_2" android:title="b2" />
</group>
</menu>
and here are the methods in the .java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.filters, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
item.setChecked(!item.isChecked());
return true;
}
This works fine checkableBehavior="single", but the item doesn't uncheck when I press. Still checked. (item.isChecked()->true)
I try to edit the .xml:
<item android:id="#+id/..." android:checkable = "true" />
Now when a select any item they check/uncheck correcltry,
but this checkableBehavior="single" doesn't work. In other words, I can check two items of the same group.
you should let onOptionsItemSelected return true;
Related
I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.
Faced an error in the onCreateOptionsMenu method during menu inflate.
I do not understand why he refers to this field - it seems that I brought everything and did it correctly under androidx.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Поиск последний новостей ...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (query.length() > 2) {
onLoadingSwipeRefresh(query);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchMenuItem.getIcon().setVisible(false, false);
return true;
}
menu_main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.MainActivity">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="always"
app:actionProviderClass="androidx.appcompat.widget.SearchView"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never" />
<item
android:id="#+id/action_weather"
android:orderInCategory="100"
android:title="Weather"
android:icon="#drawable/ic_weather"
app:showAsAction="always" />
</menu>
How can this be fixed? Thank you in advance!
P.S. A log with a description of the error is listed below:
Process: com.sv.newsapp, PID: 16248
java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to androidx.core.view.ActionProvider
at androidx.appcompat.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:425)
at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:179)
at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:129)
at com.sv.newsapp.activities.MainActivity.onCreateOptionsMenu(MainActivity.java:150)
You need to use app:actionViewClass, not app:actionProviderClass, as SearchView is a CollapsibleActionView, not an ActionProvider.
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
/>
So, I am trying to add a PopupMenu to a ActionBar item that sorts the list. I have seen some answers in stackoverflow but none of them worked for me. Below is the code.
XML
Menu Item:
<item
android:id="#+id/sortByBackers"
android:icon="#drawable/ic_sort"
android:title="Sort By Backers"
app:showAsAction="always" />
PopupMenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/default_list"
android:title="Default" />
<item
android:id="#+id/sort"
android:title="Sort" />
</menu>
Activity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
//...
//...
MenuItem sortItem = menu.findItem(R.id.sortByBackers);
sortItem.setOnMenuItemClickListener(item -> {
View view = findViewById(R.id.default_list);
popupMenu = new PopupMenu(getApplicationContext(), view);
popupMenu.inflate(R.menu.sort_or_default);
popupMenu.setOnMenuItemClickListener(item1 -> {
switch (item1.getItemId()) {
case R.id.default_list:
kickstarterAdapter.setData(kickstartersList);
break;
case R.id.sort:
kickstarterAdapter.sortList();
break;
default:
kickstarterAdapter.sortList();
}
popupMenu.show();
return true;
});
return true;
});
`
I have create a menu in the action bar but I don't know how to use the items inside of it as a button.
That's my menu 'xml' code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="#drawable/ic_add_circle_outline_black_24dp"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
That's what i have in java:
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
// "Creating" my menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
How I use the items I just declare?
For example the "Exit" option
I think it is the most basic thing although I don't know how to reach the item as a button.. Or its already define it self as a button ?
I would like to get an example and explanation.
you are using menu item inside the item, which is not the correct way to group item in menu.
Try this xml to generate your menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/my_menu" android:checkableBehavior="single">
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</group >
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
For receiving events of the click on menu:
#override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// action needed
return true;
case R.id.item2:
// action needed
return true;
}
}
Use this
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exitMenu:
//Your Logic
return true;
}
}
Use onOptionsItemSelected Method.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.exitMenu:
//code here
return true;
}
return(super.onOptionsItemSelected(item));
}
Use switch case to identify the ids of menu items.
I tried to add two action buttons on the action bar, for visibility one is defined as:
android:showAsAction="ifRoom"
another one is defined as:
android:showAsAction="never"
The problem is I can see the ic_action_search icon but I could not see the ic_action_overflow icon. This is the main_activity_actions.xml in menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyFirstApp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
MyFirstApp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:icon="#drawable/ic_action_overflow"
android:title="#string/action_settings"
MyFirstApp:showAsAction="never" />
</menu>
and this is the Java code that includes the buttons:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So whats wrong with my code?
cheers
<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=".MainActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
it is work