I have toolbar with a view pager and two tabs which are fragments in my application. When the user swipes between the two, a different menu item displays in the toolbar. I am able to do this with no problem. However, I want to get a fade animation between the two menu items when they are switching.
How I achieve the switch between the menu items follows.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
this.menu = menu; //get reference to menu
return super.onCreateOptionsMenu(menu);
}
I get a reference to the menu onCreateOptionsMenu() method.
Then using an update method I pass a value when the tabs are switched.
private void updateMenuTitles(int value)
{
if(value == 0)
{
MenuItem sort = menu.findItem(R.id.sort_menu);
sort.setVisible(true);
MenuItem dateRange = menu.findItem(R.id.date_range);
dateRange.setVisible(false);
}
else if(value == 1)
{
MenuItem dateRange = menu.findItem(R.id.date_range);
dateRange.setVisible(true);
MenuItem sort = menu.findItem(R.id.sort_menu);
sort.setVisible(false);
}
}
XML for the menu.
<item
android:id="#+id/date_range"
android:title="#string/sort_options"
android:icon="#drawable/ic_date_range_white_24dp"
android:orderInCategory="2"
android:visible="false"
app:showAsAction="ifRoom">
</item>
<item
android:id="#+id/sort_menu"
android:title="#string/sort_options"
android:icon="#drawable/ic_sort_white_24dp"
android:orderInCategory="2"
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/sortName"
android:title="#string/sort_name"
app:showAsAction="never"
/>
<item
android:id="#+id/sortSize"
android:title="#string/sort_size"
app:showAsAction="never"
/>
<item
android:id="#+id/sortDate"
android:title="#string/sort_date"
app:showAsAction="never"
/>
<item
android:id="#+id/sortLastUsed"
android:title="#string/sort_usage"
app:showAsAction="never"
/>
</menu>
</item>
The above works fine. The problem arises when I want to add an animation between
sort menu item and the dateRange menu item, when switching tabs.
I have search and tried to use the following stackoverflow posts with no
success.
Changing view on Menu Item in Action Bar
getActionView() of my MenuItem return null
Animated Icon for ActionItem
Add ActionBar Items with a fade animation
Using the above information I tried setting an android:actionViewClass which
returns null when I try to fetch it. Then I tried app:actionViewClass which makes my menu item disappear.
I tried to setActionView in the code instead of XML, then try to animate
that with no luck.
I also tried to use android(app):actionLayout. Make an ImageView layout, but that makes my menu item unclickable and I was still unable to animate the fade.
Related
I am trying to set switch in item list beside title in toolbar. Is there is any way to set the switch beside the title in item list, as I have shown in Image. Can any one help me to find out the solution.
Thank you.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/extend"
android:title="#string/clear_once_a_week"/>
</menu>
I think the below solution might work for you. So give it a try and go through the defined steps.
Step 1: Create a layout with a switch (for ex.switch_layout):
RelativeLayout
-> Add a Switch/SwitchCompat inside your parent layout
Step 2: In your activity override onCreateOptionsMenu as below:
#Override
public boolean onCreateOptionsMenu(#NonNull Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem item = menu.findItem(R.id.id_of_your_menu_item);
item.setActionView(R.layout.switch_layout); // Set your custom layout here
SwitchCompat mSwitch = item.getActionView().findViewById(R.id.id_of_your_switch);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
});
return true;
}
Or you can set an action view in your menu item directly instead of
adding it programmatically. Choose the best way according to your requirement.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
I am having an issue where i have 2 items in my action bar (one 'refresh' button and one 'Save' Button, but for some reason they do not show, instead they are nested inside an options menu (3 dots). Would anyone know how to remove the 3 dots menu and display my 2 items? I have tried many things but ultimately I just end up removing all three items. Thanks in advance.
Here is my code
add_event_action.xml (this is my menu xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_save"
android:showAsAction="always"
android:title="#string/save"/>
</menu>
Here is my Java class
public class RandomActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_list);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_event_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
case R.id.action_save:
Toast.makeText(this, "Save selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
If I understood correctly, you need two menu buttons in your toolbar.
This works for me, place it in your menu.xml:
<item
android:id="#+id/done"
android:title="#string/done"
app:showAsAction="always|withText"/>
Try to use app:showAsAction instead of android:showAsAction
To expand on the other answers somewhat:
When your activity extends from AppCompatActivity, your menu items should use app:showAsAction. When your activity does not (that is, when you're not using the AppCompat support libraries), your menu items should use android:showAsAction.
Documentation: http://tools.android.com/tips/lint-checks
AppCompatResource
Summary: Menu namespace
Priority: 5 / 10
Severity: Error
Category: Correctness
When using the appcompat library, menu resources should refer to the
showAsAction in the app: namespace, not the android: namespace.
Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.
I am working with the navigation drawer and i want to display different icon on different fragment when user click on any item of listview on navigation drawer.
i have did a code in xml of activity which contain navigation drawer.
<item
android:id="#+id/action_settings"
android:icon="#drawable/icon_setting"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
When i click on any item on list the icon should change. i tried to get reference like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
mItem = menu.getItem(0);
return true;
}
But it gives null pointer exception.thank you in advance.
Try findItem:
mItem = menu.findItem(R.id.action_settings);
I want to animate a ic_action_refresh in the ActionBar. I'm using AppCompat, and so far I have done this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main,menu);
MenuItem item = menu.findItem(R.id.action_refresh);
if(item != null)
refreshView = MenuItemCompat.getActionView(item);
super.onCreateOptionsMenu(menu, inflater);
}
The results are: item is ok, but refreshView is null.
Any idea of what can I do, or what am I missing?
Edit
<item android:id="#+id/action_refresh"
android:title="#string/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
To display an animation (I'll assume a spinning ProgressBar) in a menu item use the following:
item.setActionView(new ProgressBar(YOUR APP CONTEXT));
and when you want to get rid of the progress bar simply do item.setActionView(null);
You have to use ..
item.getActionView() // to get the current action view set for the item
but make sure you have already set action view for this item using..
item.setActionView(view)
I'm stuck while changing some properties on my options menu at onCreateOptionsMenu(). It seems like findItem() returns null, even though I'm pretty sure that the reference to the menu item is correct. My code looks as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_profile, menu);
MenuItem leftie = menu.findItem(R.id.menu_profile);
leftie.setIcon(R.drawable.ic_menu_mapmode);
leftie.setTitle(R.string.back_map);
leftie.setIntent(authIntent);
return true;
}
I really don't know what can be wrong there. Thanks in advance :)
EDIT: I forgot to include the actual problem.
You can mention title and image for that menu item in XML.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/newsItem"
android:icon="#drawable/news_tab"
android:title="#string/menu_news"/>
<item
android:id="#+id/dryiceItem"
android:icon="#drawable/dryice_tab"
android:title="#string/menu_dryice"/>
</menu>
and can set intent on menuItem like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newsItem:
// start News activity
//write your intent here.
break;
case R.id.dryiceItem:
//start another activity
//write your intent here.
break;
}
}
I figured it out. The String that references to the menu index, R.menu.activity_profile was the wrong path so it was inflating an empty menu. I changed the string to R.menu.layout and now it works as expected.
System.out.println(menu.size());
MenuItem leftie = menu.findItem(R.id.menu_profile);
System.out.println(leftie);
leftie.setIcon(R.drawable.ic_menu_mapmode);
leftie.setTitle(R.string.back_map);
leftie.setIntent(authIntent);
I've also had this happen when I had a submenu that wasn't properly nested in item tags, like
<item />
<menu>
</menu>
or
<item >
<menu>
</menu>
<item>
Typecast your findItem statement with MenuItem
MenuItem leftie = (MenuItem) menu.findItem(R.id.menu_profile);