Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Last active July 3, 2019 15:32
Show Gist options
  • Select an option

  • Save wszdwp/3e4fd4379da43b5e946b90e4bc83218a to your computer and use it in GitHub Desktop.

Select an option

Save wszdwp/3e4fd4379da43b5e946b90e4bc83218a to your computer and use it in GitHub Desktop.
Android View Util
// 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