Android Opensources - ButterKnife, Lombok

897 views
817 views

Published on

How to use ButterKnife, Lombok, and how these change the way you code :)

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
897
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android Opensources - ButterKnife, Lombok

  1. 1. Android Opensources Wooseong Kim (김우성) 1 Yooii Studios
  2. 2. 1. ButterKnife 2. Lombok 3.Android Annotations Yooii Studios2
  3. 3. ButterKnife? 3 Yooii Studios
  4. 4. • http://jakewharton.github.io/butterknife/ • using Annotation Processing • No more findViewById in Activity, View, ListAdapter etc. • Simplify click listeners ButterKnife 4 Yooii Studios
  5. 5. ButterKnife - before / at activity class ExampleActivity extends Activity { TextView title; TextView subtitle; TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); title = (TextView) findViewById(R.id.title); subtitle = (TextView) findViewById(R.id.subtitle); footer = (TextView) findViewById(R.id.footer); //TODO Use views... } } 5 Yooii Studios
  6. 6. ButterKnife - after / at activity class ExampleActivity extends Activity { @InjectView(R.id.title)TextView title; @InjectView(R.id.subtitle)TextView subtitle; @InjectView(R.id.footer)TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); //TODO Use "injected" views... } } 6 Yooii Studios
  7. 7. ButterKnife - after / at view public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2; @OverrideView onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.inject(this, view); //TODO Use "injected" views... return view; } } 7 Yooii Studios
  8. 8. ButterKnife - after / at list adapter public class MyAdapter extends BaseAdapter { @Override publicView getView(int position,View view,ViewGroup parent) { ViewHolder holder; view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); holder.name.setText("John Doe"); // etc... return convertView; static classViewHolder { @InjectView(R.id.title)TextView name; @InjectView(R.id.job_title)TextView jobTitle; publicViewHolder(View view) { ButterKnife.inject(this, view); } } } 8 Yooii Studios
  9. 9. ButterKnife - onClick @OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "Try again", LENGTH_SHORT).show(); } } 9 Yooii Studios
  10. 10. Lombok ? 10 Yooii Studios
  11. 11. • http://projectlombok.org/ • No more Getter / Setter! Lombok 11 Yooii Studios
  12. 12. Lombok - before public class GetterSetterExample { private int age = 10; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } protected void setName(String name) { this.name = name; } } 12 Yooii Studios
  13. 13. Lombok - after public class GetterSetterExample { @Getter @Setter private int age = 10; @Setter(AccessLevel.PROTECTED) private String name; } 13 Yooii Studios
  14. 14. Android Annotations ? 14 Yooii Studios
  15. 15. Android Annotations - before public class BookmarksToClipboardActivity extends Activity { BookmarkAdapter adapter; ListView bookmarkList; EditText search; BookmarkApplication application; Animation fadeIn; ClipboardManager clipboardManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN); setContentView(R.layout.bookmarks); bookmarkList = (ListView) findViewById(R.id.bookmarkList); search = (EditText) findViewById(R.id.search); application = (BookmarkApplication) getApplication(); fadeIn = AnimationUtils.loadAnimation(this, anim.fade_in); clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); View updateBookmarksButton1 = findViewById(R.id.updateBookmarksButton1); updateBookmarksButton1.setOnClickListener(new OnClickListener() { 15 Yooii Studios
  16. 16. Android Annotations - after @NoTitle @Fullscreen @EActivity(R.layout.bookmarks) public class BookmarksToClipboardActivity extends Activity { BookmarkAdapter adapter; @ViewById ListView bookmarkList; @ViewById EditText search; @App BookmarkApplication application; @RestService BookmarkClient restClient; @AnimationRes Animation fadeIn; @SystemService ClipboardManager clipboardManager; @AfterViews // instead of onCreate() (called after injecting) void initBookmarkList() { adapter = new BookmarkAdapter(this); bookmarkList.setAdapter(adapter); } 16 Yooii Studios
  17. 17. To be continued… • Robolectric (2.2+) -Testing - http://robolectric.org/ • Otto - Event handler using bus - http://square.github.io/otto/ 17 Yooii Studios
  18. 18. Thank you! 18 Yooii Studios

×