I implemented the Navigation View (Support Library 24.0.0):
But a strange behavior occurred in which i have been trying to control since.
Here is my drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Categories">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_news"
android:title="Top Stories" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_music"
android:title="Entertainment" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_sport"
android:title="Sport" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_lifestyle"
android:title="Lifestyle" />
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_tech"
android:title="Technology" />
<item
android:id="#+id/navigation_item_6"
android:icon="#drawable/ic_world"
android:title="International" />
</group>
</menu>
</item>
</menu>
But the result is :
in which only one should be selected.
I solved it using :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
for (int i = 0; i < 6; i++) {
MenuItem top = navigationView.getMenu().getItem(0).getSubMenu().getItem(i);
if (top.isChecked()) top.setChecked(false);
}
return true;
}
});
Unchecking all items in the menu
return true, that checks the clicked item.
Related
In one Fragment, I am using NavController from a context menu, which has a nested menu.
If I try to navigate to one of the nested menu items it gives me the following error:
java.lang.IllegalArgumentException: Destination id == 0 can only be
used in conjunction with a valid navOptions.popUpTo
How can I avoid this error and navigate to the nested destinations?
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single"
android:title="file"
>
<item
android:id="#+id/nav_misa"
android:title="#string/lbl_misa_hoy" />
<item
android:id="#+id/nav_homilias"
android:title="#string/lbl_homilias_hoy" />
<item
android:id="#+id/nav_lecturas"
android:title="Lecturas" />
<item
android:id="#+id/nav_comentarios"
android:title="#string/lbl_comentarios_hoy" />
</group>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_santo"
android:title="#string/lbl_santo_hoy" />
</group>
<item android:title="Liturgia de las Horas">
<menu>
<item
android:id="#+id/nav_mixto"
android:title="Oficio+Laudes" />
<item
android:id="#+id/nav_oficio"
android:title="Oficio" />
<item
android:id="#+id/nav_laudes"
android:title="Laudes" />
<item
android:id="#+id/nav_tercia"
android:title="Tercia" />
<item
android:id="#+id/nav_sexta"
android:title="Sexta" />
<item
android:id="#+id/nav_nona"
android:title="Nona" />
<item
android:id="#+id/nav_visperas"
android:title="VĂsperas" />
<item
android:id="#+id/nav_completas"
android:title="Completas" />
</menu>
</item>
</menu>
Code for navigation
public boolean onContextItemSelected(MenuItem item) {
Bundle bundle = new Bundle();
bundle.putString("FECHA", mDate);
NavController navController = NavHostFragment.findNavController(this);
navController.navigate(item.getItemId(),bundle);
return true;
}
You have to check whether the menuId is 0 or not, this is because your nested menu's root ("Liturgia de las Horas") has no ID and thus it will return 0
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == 0) {
return true;
}
Bundle bundle = new Bundle();
bundle.putString("FECHA", mDate);
NavController navController = NavHostFragment.findNavController(this);
navController.navigate(item.getItemId(),bundle);
return true;
}
I have created widgets_chkboxes file in layout
In menu create widgets.xml file
enter code here
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Filtered"/>
<item android:title="SortedBy">
<menu>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="Low To High"/>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="High To Low"/>
</menu>
</item>
</menu>
try this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check2"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check3"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_check) {
if(item.isChecked());{
// your code or action
}
}
return super.onOptionsItemSelected(item);
}
It seems impossible to find any answer on my question/problem regarding af scollable TextSelectionMenu! I have searched the whole Stackoverflow and Google without any succes and I have tried various ideas out.
I have 10 menu items in my custom TextSelectionMenu. Only 6 of them is visible, the rest is of course not, since you can't scroll to the others. Is it possible to implement a Scrollview/HorizontalListview of any sort to be able to scroll to the rest?
My menu_main.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">
<item
android:id="#+id/textcolor"
android:icon="#drawable/textcolor"
android:showAsAction="always"
android:visible="true"
android:title="ColorPicker"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/bold"
android:icon="#drawable/bold2"
android:showAsAction="always"
android:visible="true"
android:title="Bold"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/italic"
android:icon="#drawable/italic2"
android:showAsAction="always"
android:title="Italic"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/underline"
android:icon="#drawable/underline2"
android:showAsAction="always"
android:title="Underline"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/stroke"
android:icon="#drawable/strike"
android:showAsAction="always"
android:title="Strikethrough"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/increase"
android:icon="#drawable/increase"
android:showAsAction="always"
android:title="Increase"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/decrease"
android:icon="#drawable/decrease"
android:showAsAction="always"
android:title="Decrease"
tools:ignore="AppCompatResource" />
The CustomTextSelectionMenu class:
public class CustomTextSelectionMenu extends FragmentActivity implements android.view.ActionMode.Callback {
EditText editText = (EditText) findViewById(R.id.edittext);
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.paste);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
// Just a lot of switch cases inside here...
return true;
}
}
I think, there is no way to scroll the text selection menu.
But you can do is, show the few menu items say 2 or 3 as showAsAction="always" and the rest of the menu items are automatically can be shown in overflow menu.
I want to be able to show the free-dot menu always without using ActionBar. Just 3 dots menu + items in it, that's it.
How can I do that? Here's my code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyMainActivity">
<item
android:id="#+id/empty"
android:showAsAction="always"
android:title="#string/dot_menu"
android:icon="#android:drawable/ic_menu_more">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
android:title="#string/item1" />
<item
android:id="#+id/item2"
android:showAsAction="ifRoom"
android:title="#string/item2" />
<!-- ........... -->
And:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//.....
}
It doesn't show a menu at all.
Try this code, I hope it's helpful.
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/item_admin"
android:showAsAction="ifRoom|withText"
android:title="#string/item_admin"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_masterpass_preferences"
android:showAsAction="ifRoom|withText"
android:title="#string/text_1"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_shipping_addresses"
android:showAsAction="ifRoom|withText"
android:title="#string/text_2"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_switch_language"
android:showAsAction="ifRoom|withText"
android:title="#string/text_3"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_support_contact"
android:showAsAction="ifRoom|withText"
android:title="#string/text_4"
android:visible="true"
tools:ignore="AppCompatResource" />
I want to show icon in Option Menus. I tried many methods but I can't find success.In my toolbar some icon already put but i want icon in my Option menu.
I want same as below image:-
My menu.xml:-
<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:title="#string/action_search"
android:orderInCategory="1"
android:icon="#drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_cart"
android:title="#string/action_search"
android:orderInCategory="2"
android:icon="#drawable/shoppingcart"
android:actionLayout="#layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_overflow"
android:title="#string/my_cart"
android:icon="#drawable/ic"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/login"
android:title="#string/login"
android:orderInCategory="1"
android:icon="#drawable/account"
app:showAsAction="always|withText"/>
<item
android:id="#+id/my_order"
android:title="#string/my_order"
android:orderInCategory="2"
android:icon="#drawable/track"
app:showAsAction="withText"/>
<item
android:id="#+id/wishlist"
android:title="#string/wishlist"
android:orderInCategory="3"
android:icon="#drawable/wishlist"
app:showAsAction="withText"/>
</menu>
</item>
</menu>
I am new in android developing anyone can help me how to show icon in option menu. Thanks in advance!
By default the overflow menu will disable the icon and only show text, you need some workaround.
1st way
You can enable it by adding the following codes into your class
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
2nd way
Create your own overflow item and a nested menu to show both the icon and the text.
Example,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
try this code its works for you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_action_menu"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
- See more at: http://www.theappguruz.com/blog/android-working-android-actionbar#sthash.ArNQTEBF.dpuf
Create your menu xml as follow
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}
this is completely working code.Mark this answer up if it helps. :) Happy coding.
you Need to add tools:context="your class" to menu tag
<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.BaseActivity">
<item
android:id="#+id/action_notification1"
android:icon="#drawable/one"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification2"
android:icon="#drawable/two"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification3"
android:icon="#drawable/three"
android:title="action_notification"
app:showAsAction="always"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
you shold write tool:context to menu tag then run you will get icons to your text