Here i am having menu bar with two items called notification_icon and favourite_icon when i pressed favourite_icon that should be changed as favourite_icon2.So that i have tried selector. Selector only works for android:state_pressed="true" not android:state_selected="true". s Please guide me to do that.Here i have added the following code for your refreence
menu_clg.xml (under menu)
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this is icon_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/vijay"
/>
<item
android:state_selected="false"
android:drawable="#drawable/favourite_icon"
/>
</selector>
this is activity code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
favouriteClg();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, what we want to do is, when the user clicks on the add icon “+” we will add a new item and change the add icon to remove “X” icon.
onOptionsItemSelected() will be called when a user clicks on an item.
onOptionsItemSelected(MenuItem item) MenuItem object is passed as a parameter which is a reference to the clicked item. Using this object we can know which item has been clicked.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
boolean canAddItem = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_addItem){
// ( 1 ) add a new item
// ( 2 ) change add to remove
}
else{
// if a the new item is clicked show "Toast" message.
}
return super.onOptionsItemSelected(item);
}
}
We need to call onPrepareOptionsMenu(Menu menu) to add, remove and modify menu items.
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, we need to call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
boolean canAddItem = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast;
if(item.getItemId() == R.id.action_addItem){
invalidateOptionsMenu();
}
else{
toast = Toast.makeText(this, item.getTitle()+" Clicked!", Toast.LENGTH_SHORT);
toast.show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(canAddItem){
menu.getItem(0).setIcon(R.drawable.ic_content_remove);
MenuItem mi = menu.add("New Item");
mi.setIcon(R.drawable.ic_location_web_site);
mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
canAddItem = false;
}
else{
menu.getItem(0).setIcon(R.drawable.ic_content_new);
canAddItem = true;
}
return super.onPrepareOptionsMenu(menu);
}
}
Hope it helps.
Related
I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android
I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
menu.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/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I am having list of Colleges to display and click the perticular college it goes to the detail page. In there there is a menu bar with notification and Favourite_icon.Here i am having the Favourite_icon and Favourite_icon1. If users clcik the favourite_icon it stored as favourited in server and the icon changed as Favourite_icon1. After do some process i have visted the Favourited college. That time it should show the Favourite_icon1 in menu bar. I have tried the following method but nothing happend . I have added the the code which i have tried
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this Activity code
private boolean canAddItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if (item.getItemId() == R.id.action_favourite) {
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (canAddItem) {
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
} else {
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
this is the code for check wheather the college is already favourited or not in onCreate() method
public void chechFavourite() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
//method name changed here
//getAllEventFromUser method used for getting all previously send events of current user
return favouriteDelegates.getAllCollegeDetails(userMO, context);
}
#Override
protected void onPostExecute(String collegelists) {
if (collegelists != "null") {
initCollegeMO = gson.fromJson(collegelists, new TypeToken<InitCollegeMO>() {
}.getType());
collegeMOs = initCollegeMO.getCollegeMOs();
for (CollegeMO collegeMO1 : collegeMOs) {
//here the list of college has eceived from server so i checked all the college id with current college id
collegeId = collegeMO1.getCollegeId(); //here collegeMO isthe object which is accessed by parcelable from another activity
if (collegeMO.getCollegeId() == collegeId) {
canAddItem = true;
} else {
canAddItem = false;
}
}
} else {
canAddItem = false;
}
}
}.execute(null, null, null);
}
}
Hope below code would be helpful for you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.actionbar, menu );
userItem = menu.findItem(R.id.userItem);
return menu;
}
Edit 1:
Method which will update menu programatically.
private void updateMenuTitles() {
if(isFavorite){
userItem.setIcon(new BitmapDrawable(getResources(), favoriteBitmap));
}else{
userItem.setIcon(new BitmapDrawable(getResources(), unFavoriteBitmap));
}
invalidateOptionsMenu();
}
Hope this would help you.
You can change the title of your MenuItem runtime and manage the click event according to their title.
See this code, hope it will help you.
You can call updateMenuTitles() after getting response from API and change the title.
private Menu menu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Create your menu...
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
if (item.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
// do what you want
}
else if(item.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
// do what you want
}
return true;
}
return super.onOptionsItemSelected(item);
}
/*
Update title of your menuItem whenever you want
*/
private void updateMenuTitles() {
MenuItem MenuItem = menu.findItem(R.id.action_favourite);
if (MenuItem.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
}
else if(MenuItem.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
}
}
I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.
In Android if the textview is selected then a word gets selected and a contextual action bar comes at the top...i want to modify that CAB and make it look like a quick action bar...keeping the text selection feature intact...please help me...
you can select textview or anything like only in simple layout CAB appear in the top and there was some method onActionItemClicke that you want to click an item and then you modify your action in CAB default behavior................................
public class MainActivity extends Activity {
protected Object mActionMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.lay);
view.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.cab, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
Toast.makeText(MainActivity.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.shan:
Toast.makeText(MainActivity.this, "Selected shani",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
and in menu xml file create menu that you would like to appear in top CAB
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/toast"
android:title="Toast">
</item>
<item
android:id="#+id/shan"
android:title="shani">
</item>
</menu>