Skip to content

Instantly share code, notes, and snippets.

@tobrun
Last active August 29, 2015 14:06
Show Gist options
  • Save tobrun/5ab43beee185ab792462 to your computer and use it in GitHub Desktop.
Save tobrun/5ab43beee185ab792462 to your computer and use it in GitHub Desktop.
Generic findViewById
public class ViewFinder {
@SuppressWarnings("unchecked")
public static <T extends View> T find(Activity activity, int id) {
return (T) activity.findViewById(id);
}
@SuppressWarnings("unchecked")
public static <T extends View> T find(Fragment fragment, int id) {
return (T) fragment.getView().findViewById(id);
}
@SuppressWarnings("unchecked")
public static <T extends View> T find(View view, int id) {
return (T) view.findViewById(id);
}
}
// Example code, no casting required
RelativeLayout bottombar = ViewFinder.find(this, R.id.bottom_bar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment