Skip to content

Instantly share code, notes, and snippets.

@Krylez-zz
Last active December 17, 2015 17:59
Show Gist options
  • Select an option

  • Save Krylez-zz/5650346 to your computer and use it in GitHub Desktop.

Select an option

Save Krylez-zz/5650346 to your computer and use it in GitHub Desktop.
Better than findViewById
public class UIUtils {
public static <T extends View> T findView(View root, int id) {
return (T) root.findViewById(id);
}
}
// You can import static methods for minimum verbosity
import static com.package.UIUtils.findView;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
// Look, ma! No cast!
EditText edit = findView(root, R.id.edit_text);
TextView text = findView(root, R.id.text_view);
Button button = findView(root, R.id.button);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment