Skip to content

Instantly share code, notes, and snippets.

@MFlisar
Last active January 14, 2019 19:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MFlisar/55161f185909f150646e9889341373e3 to your computer and use it in GitHub Desktop.
Save MFlisar/55161f185909f150646e9889341373e3 to your computer and use it in GitHub Desktop.
IconicsBottomNavigationView - reflection based extension to support iconics icons
import android.content.Context;
import android.support.design.internal.BottomNavigationPresenter;
import android.support.design.widget.BottomNavigationView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import com.mikepenz.iconics.utils.IconicsMenuInflaterUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IconicsBottomNavigationView extends BottomNavigationView
{
public static final String TAG = IconicsBottomNavigationView.class.getSimpleName();
public IconicsBottomNavigationView(Context context)
{
super(context);
}
public IconicsBottomNavigationView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public IconicsBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
@Override
public void inflateMenu(int resId) {
Menu mMenu = getMenu();
try {
Field fieldPresenter = BottomNavigationView.class.getDeclaredField("mPresenter");
fieldPresenter.setAccessible(true);
BottomNavigationPresenter mPresenter = (BottomNavigationPresenter)fieldPresenter.get(this);
mPresenter.setUpdateSuspended(true);
Method methodGetMenuInflator = BottomNavigationView.class.getDeclaredMethod("getMenuInflater");
methodGetMenuInflator.setAccessible(true);
MenuInflater menuInflator = (MenuInflater)methodGetMenuInflator.invoke(this);
IconicsMenuInflaterUtil.inflate(menuInflator, getContext(), resId, mMenu);
mPresenter.setUpdateSuspended(false);
mPresenter.updateMenuView(true);
} catch (NoSuchFieldException e) {
super.inflateMenu(resId);
Log.e(TAG, e);
} catch (IllegalAccessException e) {
super.inflateMenu(resId);
Log.e(TAG, e);
} catch (NoSuchMethodException e) {
super.inflateMenu(resId);
Log.e(TAG, e);
} catch (InvocationTargetException e) {
super.inflateMenu(resId);
Log.e(TAG, e);
}
// Original code:
// mPresenter.setUpdateSuspended(true);
// IconicsMenuInflaterUtil.inflate(getMenuInflater(), getContext(), resId, mMenu);
// mPresenter.setUpdateSuspended(false);
// mPresenter.updateMenuView(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment