Reputation: 6026
I am new to Android development and am following the question here, which adds a badge bubble on the top-left side of a button by setting its xml file. Since I would like to dynamically create such UI instead of statically creation, can I know how to create equivalent UI using java code?
Upvotes: 0
Views: 191
Reputation: 4258
You just have to dynamically set FrameLayout.LayoutParams on your dynamically created ImageView (assuming you are going for the approach of a FrameLayout containing the button and bubble).
Some example code, written in notepad like a pro:
Button button = new Button(this);
// initialize button - text, background, etc
layout.addView(button, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
ImageView badgeBubble = new ImageView(this);
// initialize badge - source image/drawable, scale type, etc
layout.addView(badgeBubble, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT));
This assumes your FrameLayout has been initialized and is called layout
.
Upvotes: 1