Skip to content

An implementation of ContextWrapper that provides pre- and post-inflation callbacks.

License

Notifications You must be signed in to change notification settings

minyushov/inflater

Repository files navigation

Inflater

Maven Central

An implementation of ContextWrapper that provides pre- and post-inflation callbacks.

Getting started

Include inflater as a Gradle compile dependency:

dependencies {
    implementation 'io.github.minyushov:inflater:$version'
}

Implement InflationInterceptor or PostInflationListener:

private final ContextWrapper.InflationInterceptor inflationInterceptor = new ContextWrapper.InflationInterceptor() {
  @Nullable
  @Override
  public View onCreateView(@NonNull Context context, @Nullable View parent, @NonNull String name, @Nullable AttributeSet attrs) {
    if (name.equals("TextView")) {
      return new CustomTextView(context, attrs);
    }
    return null;
  }
};
private final ContextWrapper.PostInflationListener postInflationListener = new ContextWrapper.PostInflationListener() {
  @Override
  public void onViewCreated(@NonNull View view, @Nullable AttributeSet attrs) {
    if (view instanceof TextView) {
      ((TextView) view).setTypeface(Typeface.MONOSPACE);
    }
  }
};

Attach interceptors and listeners to an Activity using ContextWrapper:

public class MainActivity extends AppCompatActivity {
  @Override
  protected void attachBaseContext(Context baseContext) {
    super.attachBaseContext(new ContextWrapper.Builder(baseContext)
         .addInterceptor(inflationInterceptor)
         .addListener(postInflationListener)
         .build());
  }
}

From this point all TextViews will be replaced with CustomTextViews, and all TextViews will use monospace typeface.

About

An implementation of ContextWrapper that provides pre- and post-inflation callbacks.

Topics

Resources

License

Stars

Watchers

Forks

Languages