Last active
August 29, 2015 14:06
-
-
Save tobrun/5ab43beee185ab792462 to your computer and use it in GitHub Desktop.
Generic findViewById
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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