I have a refresh icon on my menu which when clicked will refresh the contents of the Activity. But the problem is that the refresh icon is still clickable while it is refreshing which I do not want. Here is my menu.xml file
<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_refresh"
android:orderInCategory="100"
android:icon="#mipmap/ic_refresh"
android:enabled="true"
android:title="#string/action_settings"
app:showAsAction="always"/>
And here is where I handle the click event for the icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
// getData() fetches the data and updates the display
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Also, is there a way to maybe dim the icon so that the user knows that they have already clicked the icon?
When you click the icon, use the isEnabled() method of your Menuitem to check if it is currently enabled. If it is, then disable it until the data is loaded. Once the data loads successfully, then you can enable it again. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(item.isEnabled()) {
// "Dim" the icon like you said. You can use other alpha values if you like.
item.getIcon().setAlpha(130);
// Disable the menu icon
item.setEnabled(false);
}
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, you said that you have a getData() method which updates the display after it fetches the data. Once the data is fetched, check if the Menuitem is disabled. If it is disabled, then enable it again. Example:
if(item != null && item.isEnabled() == false) {
// "Undim" the icon
item.getIcon().setAlpha(255);
// Enable it again
item.setEnabled(true);
}
You can use setEnabled(true) method to enable/disable menu item
You could create a second menu.xml, that is used after the refresh button is clicked. To force a menu redraw call invalidateOptionsMenu();
In this second menu.xml, the refresh button could be disabled, or switched to a diffrent one, indicating the user, that the refresh is on going.
Easiest way to fix this is to hold on to the menu item in your activity.
private MenuItem mRefreshItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infl = new MenuInflater(this);
infl.inflate(R.menu.menu, menu);
mRefreshItem = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
/** Call this when you begin or end loading */
private void setRefreshEnabled(boolean enabled) {
mRefreshItem.setEnabled(enabled);
}
Related
At the beginning I have two items and the 2nd gets a listener with:
final View view = findViewById(R.id.list_item);
if (view != null) {
view.setOnLongClickListener(v -> {
Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
return true;
});
}
}
But if I add a new item with menu.add("name"); the listener is on the newly added item and not on the old one (list_item)
I have already tested many other alternatives, but they don't work!
I just need help on how to keep the item ID or how the listener stays on the item.
When you add an item using ``menu.add(...)` it returns a reference to the newly added item. You can set the listener like this:
final MenuItem newItem = menu.add("New item");
newItem.setOnMenuItemClickListener(menuItem -> {
// Handle your click here!
return true;
});
However, you should add/remove menu items by overriding onPrepareOptionsMenu(Menu menu). When you use menu.add(...) use the overload that allows you to set the itemId:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 314159, Menu.NONE, "New item");
return super.onPrepareOptionsMenu(menu);
}
Note that the above will be called each time the menu is going to be shown (very convenient).
Then you can handle the menu being selected from onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 314159) {
// Handle menu selection here!
}
return super.onOptionsItemSelected(item);
}
Check the AppBar/Menu guide here: https://developer.android.com/training/appbar
I have a menu upside my page and it has an action item. like this:
action item
I set app:showAsAction="always" for it.
I have invisible view (in UI) in my page too.
I want to if I click on it, my invisible view appears. so I set a boolean parameter for that.
My boolean set true but my object doesn't appear at all. How can I fix it?
here is my code:
public static boolean myBoolean=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this if doesn't check after change parameter
if(myBoolean)
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_setting: {
myBoolean=true;
return true;
}}}
What I am getting from this is you want to show a view based on the click of some menu item in action bar right?. if this is true then do like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_setting:
if(myBoolean){
findViewById(R.id.checkBox).setVisibility(View.GONE);
myBoolean = false;
}else{
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
myBoolean = true;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can't decide the visibility earlier in your code since click may happen sometime after onCreate() only.
Graying out the Save item in toolbar only happens in design mode but never on runtime.
I'm using this code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(count > 0) {
menuItemSave.setEnabled(true);
} else {
menuItemSave.setEnabled(false);
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
menuItemSave = menu.findItem(R.id.save);
return true;
}
It's enabling/disabling but it just really not turning into gray when disabled.
You can use custom color to change color when button is enabled or disabled. [ Change color according to your requirement ]
colors/custom_button_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
In menu.xml file set to your required button item like
android:textColor="#color/custom_button_color"
According to Android docs onPrepareOptionsMenu is called just before menu is opened.But if menu is always open, it will not be called.
So,
On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu that is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.
I am trying to create a button on the action bar that will activate a method that is in the same activity.
Below is 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=".AddContactActivityOCR">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/undo"
android:orderInCategory="100"
android:title="UNDO"
app:showAsAction="always"/>
</menu>
Below are my relevant codes in the activity:
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_contact_undo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.undo:
onRewrite();
return true;
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRewrite(View v){
listName.setText(recognizedText);
listOwner.setText(recognizedText);
companyName.setText(recognizedText);
companyAddress.setText(recognizedText);
companyNumber.setText(recognizedText);
mCurrentLang = mFromLang;
validator(recognizedText);
}
I am trying to start "onRewrite()" when I click the "UNDO" button on my action bar.
However it shows the error below:
I tried using "android:onClick="onRewrite" on a normal button and it is able to start this method. Why is it not starting "onRewrite" in this case?
why its show error?
you have create method with parameter View like onRewrite(View v) and you are trying to call it without parameter that's why its show you that message.
remove parameter from method. like below
public void onRewrite(){ .... // your code}
I tried using "android:onClick="onRewrite" on a normal button and it
is able to start this method.
why its working?
want to use method to work in both case. create two method with same name & use same code inside that.
public void onRewrite(){ .... // your code}
public void onRewrite(View v){ .... // your code}
another solution is create button click in activity and remove android:onClick="onRewrite from layout.xml, call onRewrite() from click event as well as from onOptionsItemSelected
Change your Activity code
Error Because you calling method On onRewrite() without Parameter
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_contact_undo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.undo:
onRewrite();
return true;
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRewrite(){
listName.setText(recognizedText);
listOwner.setText(recognizedText);
companyName.setText(recognizedText);
companyAddress.setText(recognizedText);
companyNumber.setText(recognizedText);
mCurrentLang = mFromLang;
validator(recognizedText);
}
Hope this will helps you
For the first time i use the contextual actionbar. When i click in the item of my ListView i want have the possibility to share that item in some way (whatsapp/mail/gmail/drive etc etc..). I'm trying in with this method but no works:
the menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/>
</menu>
The contextual actionbar class:
public class CABMode implements ActionMode.Callback {
private ShareActionProvider mshare;
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// don't need to do anything
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Mode = null;
}
public Intent Share() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Share");
return intent;
}
}
Well, the contextual actionbar it opens well when i do an long click, but the share button in the actionbar doesn't work. When i say "not works" i mean that seems not clickable. Nothing works when i click over the item. Nothing appears. What i want it's open the multiple share ways. Thanks
When i say "not works" i mean that seems not clickable
ActivityChooserView, the action view ShareActionProvider returns, disables the "share" button when there are no items to display, which in your case if because you're setting up ShareActionProvider incorrectly. All of the "on click" events are handled internally, you won't receive any callbacks to ActionMode.Callback.onActionItemClicked.
Rather than settings it up in ActionMode.Callback.onActionItemClicked, move that code into ActionMode.Callback.onCreateActionMode and call Menu.findItem to initialize it.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
final MenuItem item = menu.findItem(R.id.your_share_action_provider_id);
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}