expand background of action bar menuItem to height of actionbar - android

In my app, I highlight a check button in the action bar when the user makes a change. I changed the background of the menuitem drawable, but it appears that the button does not expand the entire height of the actionbar (in the image below, what is currently being displayed is on the left; on the right is what I would like the button to look like)
How do I adjust the menuItem button so that its background expands the height of the action bar (and extends to the right edge of the screen)?
This is what I have:
if (myHorizontalLayout.getItemList().size() == 0) {
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check);
} else{
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check_highlight);
}

Related

Change StatusBar color and show ActionMode at the same time

I change status bar color whenever action mode is shown using this piece of code:
override fun onActionModeStarted(mode: ActionMode?) {
super.onActionModeStarted(mode)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window?.statusBarColor = colorBlue
}
}
The problem is action mode shows up with an animation while window.statusBarColo = changes status bar color immediately and as a result status bar color is changed a little bit sooner than action mode being displayed and it is so unpleasant to see. Do you guys have any solution for this?

How to disable all clickable items in AppBarLayout?

I'm trying to disable all clickable items in the app bar layout when the opaque background appears after clicking the floating action button. But I also need to make sure that all the floating action buttons are all still clickable. I'm thinking maybe I can disable all items in the app bar programmatically?
How to achieve this?
UPDATE
Code to set fab visibility and animation. When the Fabs displayed, the tabs and toolbar finally disabled and unclickable. But i want to make my Fabs still clickable. How can i do this? Please advice. Thank you!
public void fabVisibility(){
if (isOpen){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabClose);
textViewActivities.startAnimation(fabClose);
fabPost.startAnimation(fabClose);
textViewPosts.startAnimation(fabClose);
fabMedia.startAnimation(fabClose);
textViewMedia.startAnimation(fabClose);
fabPlus.startAnimation(fabRotateAntiClockwise);
fabActivity.setClickable(false);
fabPost.setClickable(false);
fabMedia.setClickable(false);
shadowView.setVisibility(View.INVISIBLE);
isOpen = false;
}else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabOpen);
textViewActivities.startAnimation(fabOpen);
fabPost.startAnimation(fabOpen);
textViewPosts.startAnimation(fabOpen);
fabMedia.startAnimation(fabOpen);
textViewMedia.startAnimation(fabOpen);
fabPlus.startAnimation(fabRotateClockwise);
fabPlus.setEnabled(true);
fabActivity.setClickable(true);
fabActivity.setEnabled(true);
fabPost.setClickable(true);
fabPost.setEnabled(true);
fabMedia.setClickable(true);
fabMedia.setEnabled(true);
shadowView.setVisibility(View.VISIBLE);
isOpen = true;
}
}
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To disable action bar (app bar) buttons on clicking FAB icon, you could set a flag, let's say DisableAppBarButton.
Now call invalidateOptionsMenu() which will trigger onCreateOptionsMenu and will regenerate your menu.
Modify your onCreateOptionsMenu to disable the buttons.
if (DisableAppBarButton) {
menu.someItem(R.id.yourItem).setEnabled(false);
} else {
menu.someItem(R.id.yourItem).setEnabled(true);
}
Two options:
1 - You can set a view group to be disabled by recursively setting all of its children disabled. Example:
public static setEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setEnabled(viewGroup.getChild(i), enabled);
}
}
}
Then in your code in response to the FAB showing or hiding:
ViewUtil.setEnabled(mAppBarLayout, true /* or false */);
2 - You can make the opaque background focusable and clickable to intercept clicks while it's overlayed over the app bar, while the FAB buttons that float above that background would still receive clicks.
Hope that helps!

Remove Shadow of Floating action button programmatically

I am using FAB in my listview layout with a delete icon. Now what iam doing is that there is a main FAB in layout but when any list view item is long pressed then contextual action bar showed up and also the FAB changed to "select all" icon so that user can select all items when clicked on FAB. But the problem is that when i changed my FAB to another FAB then the shadow of FAB becomes darker. I know this is because the first FAB is still behind the second FAB. so what should i do to remove the shadow so that even if second FAB is above first FAB the shadow doesnot show. Here is my code in Oncreate for FAB.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainm);
FloatingActionButton fabButton = new FloatingActionButton.Builder(this)
.withDrawable(getResources().getDrawable(R.drawable.aaqw))
.withButtonColor(Color.parseColor("#E3564C"))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 16, 16)
.create();
and here I am changing to another FAB.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual, menu);
FloatingActionButton fabButtonn = new FloatingActionButton.Builder(AfterTab.this)
.withDrawable(getResources().getDrawable(R.drawable.selectall))
.withButtonColor(Color.parseColor("#E3564C"))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 16, 16)
.create();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
FloatingActionButton fabButtonnn = new FloatingActionButton.Builder(AfterTab.this)
.withDrawable(getResources().getDrawable(R.drawable.aaqw))
.withButtonColor(Color.parseColor("#E3564C"))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 16, 16)
.create();
}
so basically i again show the first FAB when onDestroy method called.
But shadow obviously becomes darker. How to solve this issue or is there any way to remove first FAB and then show second and then remove second and again show first?

How to get the "up" button used on the Toolbar?

This is a short question:
I'm trying to force the action bar (used by a Toolbar) to use LTR alignment. I've succeeded making the layout itself use LTR, but not the "up" button (as I've done here, before Toolbar was introduced) .
It seems this view doesn't have an ID, and I think using getChildAt() is too risky.
Can anyone help?
The answer
Here's one way I've found to solve this, based on this answer .
I made it so that it is guarranteed to find only the "up" button, and whatever it does, it will revert back to the previous state it was before.
Here's the code:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public boolean onCreateOptionsMenu(final Menu menu)
{
// <= do the normal stuff of action bar menu preparetions
if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
{
final ArrayList<View> outViews=new ArrayList<>();
final CharSequence previousDesc=_toolbar.getNavigationContentDescription();
for(int id=0;;++id)
{
final String uniqueContentDescription=Integer.toString(id);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if(!outViews.isEmpty())
continue;
_toolbar.setNavigationContentDescription(uniqueContentDescription);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty())
if (BuildConfig.DEBUG)
throw new RuntimeException(
"You should call this function only when the toolbar already has views");
else
break;
outViews.get(0).setRotation(180f);
break;
}
_toolbar.setNavigationContentDescription(previousDesc);
}
//
return super.onCreateOptionsMenu(menu);
}
It seems this view doesn't have an ID
You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.
View.findViewsWithText comes with two flags:
View.FIND_VIEWS_WITH_TEXT
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.
Here's an example implementation:
final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);
final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
outViews.get(0).setRotation(180f);
Results

Hide only bottom action bar Android

Currently, I have a split action bar, and there is an edit text near the bottom of the screen.
When I click on the edit text, the keyboard shows up, but the bottom action bar ALSO shows up on top of the keyboard, making it cluttered. Here is what it looks like:
How can I hide only the bottom action bar when I click on edit text?
Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//bottom action bar
getMenuInflater().inflate(R.menu.defaultbottom_tabs, menu);
setActionBar();
return true;
}
// custom layout top action bar
private void setActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
// displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.home_top,null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
You can add the android:windowSoftInputMode="adjustPan" attribute to the Activity that shows your EditText. Alternatively, if you really want to do this in code, you can find the split action bar View by using Resources.getIdentifier.
final int id = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(id);
if (splitActionBar != null) {
// Toggle the visibility
}

Categories

Resources