Action items not selectable Android Sherlock Actionbar - android

I'm implementing action bar with items. I can't see the selected item effect (background selection color) when I click on an item in the actions :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="TTTTTTT"
android:checkable="true"
/>
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="AAAAAA"
android:checkable="true"
/>
</menu>
On My SherlockActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

UPDATE:
You need to do something like this (switching the icons ids):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
-----------
Have you done something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
This is needed to inflate the XML file.

Maybe setting identical id's on both items is causing the problem. Did you try to change one of them?

Related

How to hide action Overflow menu (Not menu item) from activity and keep in Fragment? [Not solved]

I have a requirement that action Overflow menu icon should show in fragment, but not activity.
Code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter:
openFilterDialog();
return true;
default:
break;
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mail_list, menu);
return true;
}
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="com.dwisehealthcare.pmstablet.consult.activity.ConsultActivity">
<item
android:id="#+id/filter"
android:title="Filter"
android:icon="#drawable/ic_baseline_filter_list_24"
app:showAsAction="always" />
</menu>
I searched, but everyone is talking about menu item, not action icon.
Try this link may be it will help you, with the help of this you can add menu anywhere you want.Just add an ImageView to that fragment and when you click that imageView you create a PopUpMenu

Image is not showing on ImageButton for app:actionViewClass

I implemented sorting functionality for users. I used Image button in my menu file, but it is not showing actual image.
Current Screenshot (See next to search image on toolbar):
Expected Screenshot:
Code:
menu_sort:
<?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">
<item
android:id="#+id/filter"
android:icon="#drawable/ic_sort_black_24dp"
android:title="Filter"
app:actionViewClass="android.widget.ImageButton"
app:showAsAction="always">
</item>
</menu>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_fragment_search, menu);
getMenuInflater().inflate(R.menu.menu_sort, menu);
ImageButton locButton = (ImageButton) menu.findItem(R.id.filter).getActionView();
locButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopup(v);
}
});
return true;
}
public void showPopup(View view) {
PopupMenu popup = new PopupMenu(this, view, R.style.PopupMenu);
popup.getMenuInflater().inflate(R.menu.popup_menu_sort, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.ascending:
sendOrderBy(ASECNDING);
getViewPagerPosition(ASECNDING);
break;
case R.id.descending:
sendOrderBy(DESCNDING);
getViewPagerPosition(DESCNDING);
break;
}
return false;
}
popup_menu_sort:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ascending"
android:title="A-Z"/>
<item
android:id="#+id/descending"
android:title="Z-A"/>
</menu>
Please Note: Please don't tell me to do this any other ways like item inside group or you can do in single file, I know that. Why I change because of Popup style for theme. Yes, it is necessary to build my own Popup menu. It's my requirement and I can not change it. So don't suggest me to do that without own popup menu and use default menu.
Hope following steps will be useful to you.
Remove this line: app:actionViewClass="android.widget.ImageButton"
Modify onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_fragment_search, menu);
getMenuInflater().inflate(R.menu.menu_sort, menu);
return true;
}
Your onOptionItemSelected() will be like:
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.filter) {
View menuItemView = findViewById(R.id.filter); // SAME ID AS MENU ID
showPopup(menuItemView);
}
return true;
}
Hope it will work....
Why you are treating menu item as the image button you can do like this.
menu_sort
<?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">
<item
android:id="#+id/filter"
android:icon="#drawable/ic_sort_black_24dp"
android:title="Filter"
app:showAsAction="always">
</item>
MainActivity :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_sort, menu);
super.onCreateOptionsMenu(menu, inflater);
menuItem = menu.findItem(R.id.filter);
// count=0;
// notify1();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.filter) {
View menuView = findViewById(R.id.filter); // SAME ID AS MENU ID
showPopup(menuView);
}
return super.onOptionsItemSelected(item);
}
use your show pop up like this
this is output

Menu items are not showing on my android phone

I am running this code on my android phone and i am unable to see menu items on my android phone but when i run this on emulator, i can see menu items.
Please help . Here is the screenShot of.
<item android:title="About Us"
android:id="#+id/aboutUs_ID"/>
<item android:title="Preferences"
android:id="#+id/preferences_ID"/>
java code in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu_file, menu);
return true;
}
Try to use below solution;
res/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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Color Theam"
app:showAsAction="never" />
<item
android:id="#+id/action_appMahiti"
android:orderInCategory="100"
android:title="#string/tab_refrence"
app:showAsAction="never" />
</menu>
Here is the code snippiest you can add in your activity class
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// perform operation or display message
Toast.makeText(this, "CLick me", Toast.LENGTH_SHORT).show();
break;
case R.id.action_appMahiti:
// perform operation
startActivity(new Intent(this, XYZ.class));
break;
}
return super.onOptionsItemSelected(item);
}
I will suggest to try to use above solution.

menu android not switching activities

Well this is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.news:
Intent j = new Intent(MainScreen.this,LatestNewsScreen.class);
startActivity(j);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
this is my manifest lines I added:
<activity android:name=".LatestNewsScreen"
> </activity>
and i the latestNewsScreen I must see an simple textView. I press menu and if I click nothing happens. Why Is that?
this is my menu.xml code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/news"
android:icon="#drawable/icon"
android:title="About us" />
</menu>
Any help?
When you click menu, you should see the menu items (in this case just the one) appear at the bottom of the screen. When you click that item then your other activity should start.
Maybe your icon is not the right size, try changing it to the system one, like
android:icon="#android:drawable/ic_menu_info_details"
and see what happens

Android menu will not appear when the menu button is pressed in AVD

I have defined a menu as laid out by http://developer.android.com/guide/topics/ui/menus.html
Here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_home"
android:icon="#drawable/ic_menu_home"
android:title="Main Menu" />
<item android:id="#+id/menu_signout"
android:icon="#drawable/ic_menu_signout"
android:title="Sign Out" />
</menu>
and my code, placed in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(ManageUsersActivity.this, MainMenuActivity.class));
finish();
return true;
case R.id.menu_signout:
//TODO: issue signout to clear the cookie
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Unfortunately, when I click the menu button, nothing happens.
Try chaining to the superclass in onCreateOptionsMenu(). Here is a sample project demonstrating the use of MenuInflater for options and context menus.

Categories

Resources