Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created February 10, 2016 19:34
Show Gist options
  • Save fredgrott/b3c34cea9c1654896d5a to your computer and use it in GitHub Desktop.
Save fredgrott/b3c34cea9c1654896d5a to your computer and use it in GitHub Desktop.
ASL2.0 Copyright(C) 2016 Fred Grott
public class NoReflectionLayoutInflaterFactory implements LayoutInflaterFactory {
private AppCompatActivity appCompatActivity;
public NoReflectionLayoutInflaterFactory(AppCompatActivity appCompatActivity) {
this.appCompatActivity = appCompatActivity;
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View result = null;
if (TextUtils.equals(name, "DebugDrawerLayout")) {
result = new DebugDrawerLayout(context, attrs);
} else if (TextUtils.equals(name, "ScrimInsetsFrameLayout") {
result = new ScrimInsetsFrameLayout(context, attrs);
}
//and so on...
if (result == null) {
// Try to let the Activity handle it (inflating fragments from XML)
result = appCompatActivity.onCreateView(name, context, attrs);
}
if (result == null) {
// Get themed views from app compat
result = appCompatActivity.getDelegate().createView(parent, name, context, attrs);
}
return result;
}
}
to call it in an AppCompatActivity is
private LayoutInflater layoutInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layoutInflater = getLayoutInflater().cloneInContext(this);
LayoutInflaterCompat.setFactory(layoutInflater, new NoReflectionLayoutInflaterFactory());
setContentView(R.layout.activity_main);
}
@NonNull
@Override
public LayoutInflater getLayoutInflater() {
return layoutInflater;
}
@Override
public Object getSystemService(@NonNull String name) {
if (name.equals(LAYOUT_INFLATER_SERVICE)) {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater) super.getSystemService(name);
}
return layoutInflater;
}
return super.getSystemService(name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment