BrotherV/Floating-ArcMenu

FloatingActionButton.setIcon dont work with vector drawables!

Opened this issue ยท 4 comments

Hi thanks for developing this great lib i used it in my project,thanks
but there is an issue when setting resource id to FloatingActionButton.setIcon
after lunching activity the arc menu items don't display their icon and we should to convert to drawable and then set as an icon
example: don't display Fab icon ๐Ÿ‘Ž
private static final int[] ARCE_MENU_ITEM_DRAWABLES = { R.drawable.ic_phone, R.drawable.ic_message,R.drawable.ic_email}; FloatingActionButton item = new FloatingActionButton(this); // Use internal FAB as child FloatingActionButton item = new FloatingActionButton(this); // Use internal FAB as child item.setSize(FloatingActionButton.SIZE_MINI); // set initial size for child, it will create fab first item.setIcon(ARCE_MENU_ITEM_DRAWABLES[i]);//item.setIcon(R.drawable.ic_email); don't work item.setBackgroundColor(ContextCompat.getColor(ViewAdActivity.this,R.color.colorButton)); // it will set fab child's color menu.setChildSize(item.getIntrinsicHeight()); // set absolout child size for menu

example :display Fab icon ๐Ÿ‘
and we should first convert our resources to drawable and then set as icon it will be work
Drawable fabIconDrawable = ContextCompat.getDrawable(getApplicationContext(),ARCE_MENU_ITEM_DRAWABLES[i]); item.setIcon(fabIconDrawable);

Hello dear Edalat,
Actually This library has 2 methods, first one you can add an image by resource id(make sure you are using the right id) and with the second one you can add image as drawable, I tested it with different algorithm to change the resource id to drawable and both are working well.

/**
* Set the drawable that is used as this button's icon.
* @param resId The drawable resource.
*/
public void setIcon(@DrawableRes int resId){
try {
//Bitmap b = new BitmapFactory().decodeResource(getResources(), resId);
//Drawable mIcon = new BitmapDrawable(getResources(), b);
Drawable mIcon = ContextCompat.getDrawable(getContext(), resId);
setIcon(mIcon);
}catch (Exception e){
e.printStackTrace();
}
}

/**
 * Set the drawable that is used as this button's icon.
 * @param icon The drawable.
 */
public void setIcon(Drawable icon){
          //////////////
} 

Hi, thanks for your response.
I have used the correct resource id as you know if i use incorrect resource ids android studio don't build the project because there is a compile time error for unknown resource ids.

the problem is that i am using vector drawables for icons and the setIcon method only works for simple image file extensions such as .jpg or .png so if i want to use vector drawables as icon for the FloatingActionButton i should do it this way:

Drawable fabIconDrawable = ContextCompat.getDrawable(getApplicationContext(),ARCE_MENU_ITEM_DRAWABLES[i]); item.setIcon(fabIconDrawable);

I changed the first method, now check it with new library.

the problem is solved,thanks ๐Ÿ‘