Michelangelo
Layout inflation library for Android which uses annotation processing to write the code you don't want to write and simplify your compound views.
How to use
An example of use:
@InflateLayout(R.layout.custom_view)
public class MyCustomView extends FrameLayout {
public MyCustomView(Context context) {
super(context);
}
@AfterInflate
public void updateTextView() {
((TextView) findViewById(R.id.my_text_view)).setText("hey!");
}
}
R.layout.custom_view
:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</merge>
Inflating that View is pretty straightforward:
MyCustomView view = Michelangelo.build(this, MyCustomView.class);
Post inflate methods
Use the annotation @AfterInflate
on your compound view's methods you want to run straight after the layout is inflated with Michelangelo.
Michelangelo and ButterKnife
Michelangelo plays very well with Jake Wharton's library ButterKnife. Just add the annotation @InjectViews
to your ViewGroup
and Michelangelo will do the rest.
Example using ButterKnife:
@InflateLayout(R.layout.item_painting)
@InjectViews
public class PaintingItemView extends LinearLayout {
@InjectView(R.id.image) ImageView image;
@InjectView(R.id.title) TextView title;
public PaintingItemView(Context context) {
super(context);
setOrientation(HORIZONTAL);
}
public void bind(Painting painting) {
image.setImageResource(painting.getDrawableResId());
title.setText(painting.getTitle());
}
}
Order of events
When you call Michelangelo.build()
:
- The view gets inflated
- If specified, the injector is applied (
ButterKnife.inject(view)
) - If exist, methods annotated with
@AfterInflate
are run.
Sample
See the sample for a common use of this library with ListView
adapters.
License
Copyright 2014 Romain Piel
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.