Remove Shadow of Floating action button programmatically - android

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?

Related

Different text colors in navigation view for each items?

I am using Navigation View in android studio to create a navigation drawer in my app. In the navigation drawer, I want one item to be colored in green, one in red and others in black (just like the one in the screenshot below). However, I can't seem to find the solution to this. I know I can change the color of all the items using 'itemTextColor' in XML but that's not what I want to do.
So it seems it not as easy to change specific items colours, its either all or nothing (via app:itemIconTint and app:itemTextColor on the com.google.android.material.navigation.NavigationView), but it seems you can do something like this (I did it in Java as you never mentioned if you were using Kotlin or Java):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .. other code
NavigationView navigationView = findViewById(R.id.nav_view);
// .. other code
int menuItemPosition = 0; // the position of the menu item in NavigationView you want to change the color of
MenuItem menuItem = navigationView.getMenu().getItem(menuItemPosition);
this.changeMenuItemColor(menuItem, Color.RED);
}
private void changeMenuItemColor(MenuItem menuItem, #ColorInt int color) {
SpannableString coloredMenuItemTitle = new SpannableString(menuItem.getTitle());
coloredMenuItemTitle.setSpan(new ForegroundColorSpan(Color.RED), 0, coloredMenuItemTitle.length(), 0);
menuItem.setTitle(coloredMenuItemTitle);
}

How to replace the actionbar by a popup menu?

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.
Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

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

expand background of action bar menuItem to height of actionbar

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);
}

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