Change background color of MenuItem when clicked - android

What is the correct way to change background color of a MenuItem? I would like to change it to sinalize that MenuItem is active but i didn't find how to do it.
By now, i'm changing icon for another one with different color but i don't need it, i just need to put some color on background when it is clicked.
Here is what i'm doing to change icon:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnNow:
item.setIcon(getResources().getDrawable(R.drawable.now_icon_active);
this.displayFragment(getNowFragment());
break;
default:
super.onOptionsItemSelected(item);
break;
}
return false;
}
How can i change background color when it's clicked and keep the color until the user choose a new option?
Thank in advance.

Related

How to change a menu item background color in toolbar? -Android-

I'm working on Android. I have a menu item icon in my toolbar and, when I click on it, it displays a notification list.
I want to change the icon's background color when the list is displayed. Just as eToro app does it:
IMAGE: Icon hasn't been clicked and the background is the same as the toolbar
IMAGE: Icon has been clicked and the background color has changed
How do I achieve this?
My code:
//Here, I inflate the options in the activity's menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_management, menu);
MenuItem menuItem = menu.findItem(R.id.notification_action);
menuItem.setIcon(
Converter.convertLayoutToImage(
ManagementActivity.this,
mNotificationCount,
R.drawable.ic_notifications_white_24dp
)
);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.notification_action:
//Here,on the item click event, I want to change the item's background color, as Etoro does.
//...
//Then I trigger the action..
openNotificationListFragment();
}
return true;
}

How to set layout background fade out?

I added an option menu in my app.
I want activity's whole background appear dark when menu key is touched so the user can see option menu well.(like when a dialog is displayed)
Should I use animation to do this or is there any other way?
As you're using OptionMenu, you could do:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.your_option_menu:
mRootLayout.setAlpha(0.5f);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
mRootLayout would be the root layout from the xml you set the view in your activity. Just use findViewById to set it.
After you select a final option in your menu, make sure to return the alpha property of the root layout to 1.

Make app icon clickable without back icon

Can i make my Action Bar app icon clickable without displaying the back icon?
This is my code, it works, I have only layout problem:
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my activity layout, what I want is remove back icon, is it possible?
Try to use setHomeButtonEnabled(boolean enabled) instead of setDisplayHomeAsUpEnabled(boolean enabled). The latter do exactly the same thing as former which is enabling home button but additionally put up affordance sign which you want to get rid off.

Action bar with written clickable?

I would like to have an ActionBar in Android similar to evernote where in the top right there is a sign that if touched triggers an action. How do I put this in writing clickable? And as I move to the right?
You need to override OnOptionsItemSelected() method in your activity. You can also check the for offical doc for full tutorial
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuItem1:
action1();
case R.id.menuItem2:
action2();
default:
return super.onOptionsItemSelected(item);
}
Good luck

Keeping the pressed state of ActionBar Icons in android

States of actionBar
pressed state::
Default state::
What i am doing ::
I am using the splitted actionbar
On click of icon in bottom, I am changing the fragment
Functionality is working fine
What i want::
Is it possible to keep the pressed state of the actionbar item until
i click the next actionbar icon
Then when i say click the star in figure, i want to keep the star in
pressed state as blue color and search icon to return to normal state
Code that i use::
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
//((View) item).setBackgroundResource(R.color.actionBarIconPressed);
//item.setIcon(R.color.actionBarIconPressed);
item.setEnabled(true);
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
//Condition to check whether the fragment is already in container & based on that do appropriate actions
if (getSupportFragmentManager().findFragmentByTag("SearchFragmentTag")==null){
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.addToBackStack(null);
ft1.commit();
}else{
ft1.remove(fragSearch);
ft1.add(R.id.content_frame, fragSearch, "SearchFragmentTag");
ft1.commit();
}
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.actionbar_sort_menu, menu);
return super.onCreateOptionsMenu(menu);
}
My Questions::
How to modify my code to achieve this feature ?
Is it possible ?
What are the possible ways ?
In onOptionsItemSelected, you can cast the MenuItem into a View and then, change its background. I got a start answer however I think you can improve it and change it according to your needs:
Init your ids in an array:
int[] listItemId = { R.id.searchID, R.id.ratindID, R.id.likeID, R.id.shareID };
Call your options item selected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.searchID:
// call private method with int 1
onItemChangeBackground(1);
... // do some stuff
return true;
case R.id.ratindID:
onItemChangeBackground(2);
... // do some stuff
return true;
case R.id.likeID:
onItemChangeBackground(3);
...
return true;
case R.id.shareID:
onItemChangeBackground(4);
...
return true;
}
return super.onOptionsItemSelected(item);
}
Change the background item by switching an int value:
private void onItemChangeBackground(int i) {
// Loop to reinit the background items
for(int ii = 0; ii < listItemId.length; ii++) {
// Cast the MenuItem into a View
( (View) findViewById(listItemId[ii]) )
// And set the default background (for example dark)
.setBackgroundColor(getResources().getColor(R.color.dark));
}
// Selected background
switch(i) {
case 1: ( (View) findViewById(R.id.searchID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 2: ( (View) findViewById(R.id.ratindID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 3: ( (View) findViewById(R.id.likeID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
case 4: ( (View) findViewById(R.id.shareID) )
.setBackgroundColor(getResources().getColor(R.color.blue));
break;
}
}
You can also set a drawable with a selector by using setBackgroundDrawable() when you want to reinitialize the background items (as you do in the styles.xml). Tested, it works!
Another solution might be to use invalidateOptionsMenu(). This will clear and redraw the menu by recalling onCreateOptionsMenu() again. Keep in mind without call invalidateOptionsMenu() the last method will never be recalled, it's only called at the first rime of the activity's creation. That's why we need to invalidate its content:
Update an integer for each item selected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.searchID){
nbItemSelected = 1;
invalidateOptionsMenu();
...
} else if(...) {
nbItemSelected = 2;
invalidateOptionsMenu();
} ...
}
Then, change the background while you create the options:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
if(nItemSelected != 0) {
// Find item, cast the selected item to a view
( (View) menu.findItem(listItemId[nItemSelected]) )
// Change its background
.setBackgroundColor(getResources().getColor(R.color.blue));
}
...
return true;
}
I guess it is possible in this way too, it might work.
Are you creating a TabBarView like for example those you can find in iOs?
If so, an split actionbar is not the best component, it is better to use a ViewPagerIndicator with a ViewPager component. In there you can create your custom icons with your custom icons states and backgrounds.
I have an spare snippet of code I created long time ago for an application that required that component. Maybe you can look it around and take what you need (but I advice you that the code is not the best you can find as I was in a hurry, things can be done better, much better):
https://gist.github.com/aldoborrero/70fbeb062fab0ccc7d87
#Casper did you tried setting custom selector for your ActionBar?
Refer one below
drawable/tab_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:drawable="#drawable/tab_selected_customtabs" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="#drawable/ab_transparent_customtabs" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="#drawable/tab_selected_pressed_customtabs" android:state_pressed="true"/>
<!-- Selected tab -->
<item android:drawable="#drawable/tab_selected_focused_customtabs" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
</selector>

Categories

Resources