Last active
July 3, 2019 15:32
-
-
Save wszdwp/3e4fd4379da43b5e946b90e4bc83218a to your computer and use it in GitHub Desktop.
Android View Util
This file contains hidden or 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
| // Set dynamic height of a listview | |
| setupListViewHeight(context, myListView, myListData.size(), 30); // 30 dp per row | |
| /** | |
| * Setup listview height based on the number of rows and unit row height | |
| * | |
| * @param listView | |
| * @param numberOfRows float | |
| * @param rowHeightInDp | |
| */ | |
| public static void setupListViewHeight(Context context, ListView listView, int numberOfRows, float rowHeightInDp) { | |
| ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | |
| layoutParams.height = dpToPx(context, numberOfRows * rowHeightInDp); | |
| listView.setLayoutParams(layoutParams); | |
| } | |
| /** | |
| * Utility function to convert dp to px used in layoutParams | |
| * @param context | |
| * @param dp | |
| * @return | |
| */ | |
| public static int dpToPx(Context context, float dp) { | |
| return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment