How to make selected item clickable in bottom navigation - android

I have a bottom navigation, I check the selected item on its selection, and this prevent it from re selecting the same item again.
So if I am on the home screen and I click home screen navigation item again, it does not do anything.
I have a case where I need to make home navigation item selectable even if its already selected.
This is my code that I run in onCreate method.
public void updateNavigationBarState() {
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = bottomNavigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
item.setEnabled(false);
break;
}
}
}
Is there any way to make item clickable even if its selected.

Use setNavigationItemSelectedListener.
Like below
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId()) {
case R.id.menuItemId:
// do your stuff here
return true;
default:
return true;
}
});

Related

Change MenuItem text in onMenuItemSelected

I'm trying to change dynamically the text of one of my button in my FabSpeedDial. When the user tap on a specific menu of this FAB, I want to change the text of this menu item but it doesn't work, nothing change...
#Override
public boolean onMenuItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.action_one) {
menuItem.setTitle(getString(R.string.my_new_text)); // doesn't work
} else if (menuItem.getItemId() == R.id.action_two) {
} else if (menuItem.getItemId() == R.id.action_three) {
}
return false;
}
Any ideas ?
Call
invalidateOptionsMenu();
after menuItem.setTitle("Any title"); to populate changes
your should invalidate the menu so that it will have an impact on UI.
menuItem.setTitle(getString(R.string.my_new_text));
invalidateOptionsMenu();

How to handle ActionBar's icons visibility when using SearchView

Well, what I want is to:
Initialize ActionBar with all items, except "searchSettings";
When click on the Search icon, searchSettings appears and the rest of the icons disappear;
When close the search EditText (pressing device's back button or ActionBar's back button), ActionBar returns to its original state (all icons appearing, except "searchSettings").
My actual code is the following:
(I've imported android.support.v7.widget.SearchView instead of android.widget.SearchView. When I was using android.widget.SearchView this worked fine but other things don't)
private MenuItem searchIteam, searchSettings;
private SearchView searchView;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menuMain);
searchItem = menu.findItem(R.id.search);
searchSettings = menu.findItem(R.id.action_searchSettings);
searchView = (SearchView)MenuItemCompat.getActionView(item);
searchSettings.setVisible(false); // hide searchSettings Item when Menu is created
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
(...)
return false;
}
});
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setItemsVisibility(menuMain, item, false);
searchSettings.setVisible(true);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
supportInvalidateOptionsMenu(); //shouldn't this reload the Action Bar as it was when onCreate?
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
supportInvalidadeOptionsMenu();
super.onBackPressed();
}
This code doesn't work, when I press "back" first time, it only closes the Search's EditText and the icons don't change. If I press back again, the Activity goes a level up but I can see the icons getting as the beginning (getting as they should when I pressed "back" for the first time) a little while before the Activity close...
--- EDIT ---
Currently, if I click on Search ActionBar Icon, and then begin to press "Back Button" repeatedly, the following happens:
1st pressing: the keyboard hides, but the search EditText is still open;
2nd pressing: the searching ends (search EditText closes and the normal activity's content is shown);
3rd pressing: the activity closes.
Then, for testing purposes, I did this:
boolean pressed1, pressed2, pressed3;
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pressed1 = false;
pressed2 = false;
pressed3 = false;
setItemsVisibility(menuMain, item, false);
searchSettings.setVisible(true);
}
});
#Override
public void onBackPressed() {
if (!pressed1) {
pressed1 = true;
}
else if(!pressed2) {
pressed2 = true;
}
else if(!pressed3) {
pressed3 = true;
supportInvalidateOptionsMenu();
}
else {
super.onBackPressed();
}
}
And now, what happens is:
1st pressing: the keyboard hides, but the search EditText is still open;
2nd pressing: the searching ends (search EditText closes and the normal activity's content is shown);
3rd pressing: nothing happens;
4th pressing: nothing happens;
5th pressing: the ActionBar reloads as I wanted;
6th pressing: the activity closes;
--- EDIT 2 ---
Then I've changed to this:
#Override
public void onBackPressed() {
if (!pressed1) {
pressed1 = true;
onBackPressed();
}
else if(!pressed2){
pressed2 = true;
onBackPressed();
}
else if(!pressed3){
pressed3 = true;
supportInvalidateOptionsMenu();
}
else {
super.onBackPressed();
}
}
What is happening now is:
1st pressing: the keyboard hides, but the search EditText is still open;
2nd pressing: the searching ends (search EditText closes and the normal activity's content is shown);
3rd pressing: the ActionBar reloads as I wanted;
4th pressing: the activity closes;
--- EDIT 3 --- (SOLUTION) ---
I guess that the methods setOnSearchClickListener and setOnCloseListener are from android.widget.SearchView... As I've imported android.support.v7.widget.SearchView instead, I've changed them to:
MenuItemCompat.setOnActionExpandListener(searchItem,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
setItemsVisibility(menu, searchItem, false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
supportInvalidateOptionsMenu();
return true;
}
});
Now it's working just fine (:
I hope I get you right: the first time you press BACK, the keyboard gets hidden. The second time you press BACK, the app quits.
If this is the case, then everything works fine. Becuase this is what super.onBackPressed() is supposed to do. It will try to hide the keyboard if it's shown. If not, it will try to go the previous activity. If there is none, it will quit the app.
So what you need to do is play with the onBackPressed() method. Basically, you don't necessarily need to call the super method if you are sure what you should do.
#Override
public void onBackPressed() {
if ( isSearching) {
supportInvalidadeOptionsMenu();
isSearching = false;
} else {
super.onBackPressed();
}
}
So now, when never you press BACK button, the activity will check if isSearching, and decide to re-render the action bar or take its normal actions as usual.
You'll need to add some logic to set the boolean flag isSearching, for example, set isSearching to true when clicking the Search.
I 'm not sure if supportInvalidadeOptionsMenu() would reset your action bar. Anyway, you can adjust the visibility for each view instead.

Triggering multiple listviews for a single contextual menu. Not sure if it is even possible

I currently have multiple lisview's that have contextual menu's that allows users t delete items. I havent been able to find any topics on this. Is it possible to have all of the listviews triggered at the same time to delete them without having to click on each list to get their own contextual menu?
Currently I am only able to click on one listview after the contextualmenu is brought to front. I beleive it has to to with :
listBreakfast.setMultiChoiceModeListener(this);
listLunch.setMultiChoiceModeListener(this);
They are being implemented separately. Below is the implementations within the activity.
listBreakfast.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listBreakfast.setMultiChoiceModeListener(this);
listLunch.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listLunch.setMultiChoiceModeListener(this);
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_delete :
for (int i = listBreakfast.getCount() - 1; i >= 0; i--) {
if (listBreakfast.isItemChecked(i)) {
lb = logsAdapter1.getItem(i);
lb.delete1();
logsAdapter1.remove(logsAdapter1.getItem(i));
logsAdapter1.notifyDataSetChanged();
}
}
for (int i = listLunch.getCount() - 1; i >= 0; i--) {
if (listLunch.isItemChecked(i)) {
ll = logsAdapter11.getItem(i);
ll.delete();
logsAdapter11.remove(logsAdapter11.getItem(i));
logsAdapter11.notifyDataSetChanged();
}
}
mode.finish();
logsAdapter.notifyDataSetChanged();
logsAdapter11.notifyDataSetChanged();
return true;
default :
return false;
}
}

Android Options Menu Without ActionBar?

what I'm looking for is to make an options menu but without the ActionBar. In the Google music app I saw that they have a options menu sort of thing with no action bar. Below is a picture of what I was talking about in the Google music app.
Thank you in advance! :)
That's just a simple popop. You can do that on any view. Throw an icon on a view, like the overflow menu icone and set a click listener on it.
This example is a list of devices (smartphones) in a catalog. I populate the tag with an object so I know which one the user clicks on.
public void showDeviceMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.inflate(R.menu.cart_device_menu);
DeviceTag tag = (DeviceTag) v.getTag();
final String groupId = tag.groupId;
final String sku = tag.sku;
final String productId = tag.productId;
SpannableStringBuilder text = new SpannableStringBuilder(tag.name);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
popup.getMenu().findItem(R.id.menu_name).setTitle(text);
invalidateOptionsMenu();
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.duplicate_device:
duplicateDevice(sku, productId);
return true;
case R.id.update_device:
updateWirelessItemInCart(sku,groupId);
return true;
case R.id.delete_device:
removeItemFromCart(groupId);
return true;
default:
return false;
}
}
});
popup.show();
}

part of optionsMenu is hidden on HTC One X

I'm developing a weather app, but I have a problem when displaying an optionsMenu with some smartphones. I want to display a menu with 3 options, but on the HTC one X, only 2 options are displayed.
The problem is that the black bar at the bottom of the screen hides the third option of my menu.
Here is a screenshot of the problem : (we should see "Recharger", "Voir cette image" and the last option : "Autres cartes")
The code :
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,100,0,m_res.getStringArray(R.array.menu_reload)[Commons.currentLanguage]).setIcon(R.drawable.reload);
menu.add(0,101,0,m_res.getStringArray(R.array.menu_view_single)[Commons.currentLanguage]).setIcon(R.drawable.see);
if(m_model.getUrl2().size() !=0)
menu.add(0,102,0,m_res.getStringArray(R.array.menu_more)[Commons.currentLanguage]).setIcon(R.drawable.more);
SubMenu moreMaps = menu.addSubMenu(0,103,0,m_res.getStringArray(R.array.menu_others_maps)[Commons.currentLanguage]).setIcon(R.drawable.france);
if(m_time == 1)
{
for(int iBoucle = 0 ; iBoucle < m_model.getNames1().size() ; iBoucle++)
moreMaps.add(1,iBoucle,1,m_model.getNames1().get(iBoucle));
}
else
{
for(int iBoucle = 0 ; iBoucle < m_model.getNames2().size() ; iBoucle++)
moreMaps.add(1,iBoucle,1,m_model.getNames2().get(iBoucle));
}
onContextItemSelected(moreMaps.getItem());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 100:
if(Commons.getNetworkState() || Commons.getWifiState())
{
eraseData();
loadingData();
}
else
Commons.getConnectivityErrorMessage(m_Context);
return true;
case 101:
Intent sia = new Intent(m_Context, SingleImageActivity.class);
sia.putExtra("url", correctURL(m_urlImages.get(m_step)));
sia.putExtra("choiceNumber", -1);
if(m_textIsHour)
{
if(m_time == 1)
sia.putExtra("title", m_model.getNames1().get(m_mode)+" - "+m_model.getHours1().get(m_step)+"h");
else
sia.putExtra("title", m_model.getNames2().get(m_mode)+" - "+m_model.getHours2().get(m_step)+"h");
}
startActivity(sia);
break;
case 102:
if(Commons.getNetworkState() || Commons.getWifiState())
{
Intent modelSubList = new Intent(m_Context, ModelViewActivity.class);
modelSubList.putExtra("model",m_modelNumber);
modelSubList.putExtra("mode",m_mode);
if(m_time == 1)
modelSubList.putExtra("time",2);
else
modelSubList.putExtra("time",1);
startActivity(modelSubList);
finish();
}
else
{
Commons.getConnectivityErrorMessage(m_Context);
}
break;
case 103:
break;
default:
if(Commons.getNetworkState() || Commons.getWifiState())
{
Intent otherMapsIntent = new Intent(m_Context, ModelViewActivity.class);
otherMapsIntent.putExtra("model",m_modelNumber);
otherMapsIntent.putExtra("mode",item.getItemId());
otherMapsIntent.putExtra("time",m_time);
startActivity(otherMapsIntent);
finish();
return true;
}
else
{
Commons.getConnectivityErrorMessage(m_Context);
return false;
}
}
return false;
}
This code is showing three menu options on the US and European One X phones I have here:
https://github.com/lnanek/Misc/tree/master/OneXFullscreenMenuTest
Can you try it on your device? If it doesn't work, then we know it is the software on your device. If it does work, then it could be something in your code.
Can you post more about your situation? I'm particularly interested in the code for how you turn on fullscreen and your AndroidManifest.xml (for things like the uses-sdk line and theme settings).
I don't see anything wrong offhand in the code you posted, but could you comment out this line just for a quick check?
if(m_model.getUrl2().size() !=0)
If that check isn't passing, then there should be only two menu items, not three. So commenting it out would be a quick check (or watching in a debugger to see if all three adds are called, of course).
Please note that onCreateOptionsMenu is only called the very first time the Activity instance shows the options menu, as opposed to onPrepareOptionsMenu which is called every time. So if adding that third menu option is not done the very first time, then it never will be.

Categories

Resources