Copyright (c) 2014, Altinn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redis...
If you think the Android project ExampleApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package net.simonvt.menudrawer.compat;
//fromwww.java2s.comimport android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.lang.reflect.Method;
finalclass ActionBarHelperCompat {
privatestaticfinal String TAG = "ActionBarHelperCompat";
private ActionBarHelperCompat() {
}
publicstaticvoid setActionBarUpIndicator(Object info, Activity activity, Drawable drawable, int contentDescRes) {
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.mUpIndicatorView != null) {
sii.mUpIndicatorView.setImageDrawable(drawable);
final String contentDescription = contentDescRes == 0 ? null : activity.getString(contentDescRes);
sii.mUpIndicatorView.setContentDescription(contentDescription);
}
}
publicstaticvoid setActionBarDescription(Object info, Activity activity, int contentDescRes) {
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.mUpIndicatorView != null) {
final String contentDescription = contentDescRes == 0 ? null : activity.getString(contentDescRes);
sii.mUpIndicatorView.setContentDescription(contentDescription);
}
}
publicstatic Drawable getThemeUpIndicator(Object info) {
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.mUpIndicatorView != null) {
return sii.mUpIndicatorView.getDrawable();
}
return null;
}
publicstatic Object getIndicatorInfo(Activity activity) {
returnnew SetIndicatorInfo(activity);
}
publicstaticvoid setDisplayHomeAsUpEnabled(Object info, boolean enabled) {
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.mHomeAsUpEnabled != null) {
try {
sii.mHomeAsUpEnabled.invoke(sii.mActionBar, enabled);
} catch (Throwable t) {
if (ActionBarHelper.DEBUG) {
Log.e(TAG, "Unable to call setHomeAsUpEnabled", t);
}
}
}
}
privatestaticclass SetIndicatorInfo {
public ImageView mUpIndicatorView;
public Object mActionBar;
public Method mHomeAsUpEnabled;
SetIndicatorInfo(Activity activity) {
try {
String appPackage = activity.getPackageName();
try {
// Attempt to find ActionBarSherlock up indicator
finalint homeId = activity.getResources().getIdentifier("abs__home", "id", appPackage);
View v = activity.findViewById(homeId);
ViewGroup parent = (ViewGroup) v.getParent();
finalint upId = activity.getResources().getIdentifier("abs__up", "id", appPackage);
mUpIndicatorView = (ImageView) parent.findViewById(upId);
} catch (Throwable t) {
if (ActionBarHelper.DEBUG) {
Log.e(TAG, "ABS action bar not found", t);
}
}
if (mUpIndicatorView == null) {
// Attempt to find AppCompat up indicator
finalint homeId = activity.getResources().getIdentifier("home", "id", appPackage);
View v = activity.findViewById(homeId);
ViewGroup parent = (ViewGroup) v.getParent();
finalint upId = activity.getResources().getIdentifier("up", "id", appPackage);
mUpIndicatorView = (ImageView) parent.findViewById(upId);
}
Class supportActivity = activity.getClass();
Method getActionBar = supportActivity.getMethod("getSupportActionBar");
mActionBar = getActionBar.invoke(activity, null);
Class supportActionBar = mActionBar.getClass();
mHomeAsUpEnabled = supportActionBar.getMethod("setDisplayHomeAsUpEnabled", Boolean.TYPE);
} catch (Throwable t) {
if (ActionBarHelper.DEBUG) {
Log.e(TAG, "Unable to init SetIndicatorInfo for ABS", t);
}
}
}
}
}