Reputation: 1171
I have a grid view with 3 columns and unlimited rows. What I hope is:
I have thought that I could possibly create 4 xml files for each scenario, but I realize that I'd need to create not just 4. I'd need to create one more in case when there is only 1 item in the grid. 2 more in case there is only 1 row in the grid, one more for middle items, and who knows...
I am just wondering if there is a more elegant solution to this problem.
Upvotes: 0
Views: 484
Reputation: 4352
The following code may help you,
GradientDrawable drawable;
makeBackground(drawable, 5, R.color.black, 2, R.color.white);
private void makeBackground(GradientDrawable drawable, int radius, int backgroundColorResource, int borderWidth, int borderColorResource){
drawable.setColor(getColor(getContext(), backgroundColorResource));
drawable.setStroke(dpToPx(borderWidth), getColor(getContext(), borderColorResource));
drawable.setCornerRadius(dpToPx(radius));
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
public static int getColor(Context context, int res){
return ContextCompat.getColor(context, res);
}
And for different values in different corner, you have to use setCornerRadii instead of setCornerRadius.
Upvotes: 2