Reputation: 3457
I have a class called OverlayBuild, with the purpose of drawing "things" on views, but for some reason I experienced a problem that seemed simple, but still costs me a lot of time. The problem is that when the class is used for the second (and 3rd, 4th etc.) the drawing/view is not added to its parent view, or not visible at least!
I tried simplifying the class to identify the cause of the error, but it still occurs.
So here is my code for the OverlayBuild class first:
public class OverlayBuild {
private String tag;
private Context context;
private ViewGroup container;
private RelativeLayout overlay;
public static boolean layoutLoaded;
private int backgroundWidth = 1500;
private int backgroundHeight = 1500;
private boolean overlayLayoutCompleted;
public OverlayBuild(Context context, ViewGroup container, String tag) {
this.tag = tag;
this.context = context;
this.container = container;
this.overlay = new RelativeLayout(context);
layoutLoaded = false;
}
public OverlayBuild setLayoutListener(final LayoutListener layoutListener) {
container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!layoutLoaded) {
layoutListener.layoutPrepared(container);
layoutLoaded = true;
}
}
});
return this;
}
public OverlayBuild setBackgroundDimens(int width, int height) {
this.backgroundWidth = width;
this.backgroundHeight = height;
return this;
}
public OverlayBuild hideOverlay(){
if(overlay != null){
overlay.setVisibility(View.GONE);
}
return this;
}
public OverlayBuild commit() {
View view = getViewByTag(tag);
if (view == null) {
container.addView(overlay);
overlayLayoutCompleted = true;
overlay.setTag(tag);
overlay.bringToFront();
overlay.setVisibility(View.VISIBLE);
container.setBackgroundResource(R.color.red);
overlay.setBackgroundResource(R.color.blue_mid);
}
return this;
}
private View getViewByTag(String tag) {
return container.findViewWithTag(tag);
}
public interface LayoutListener {
void layoutPrepared(ViewGroup view);
}
And here is the method where I use the class to "draw":
public static void showEmptyState(final Context context, final Button createAgentBtn, final ViewGroup parentView, final String tag) {
final OverlayBuild builder = new OverlayBuild(context, parentView, tag);
builder.setLayoutListener(new OverlayBuild.LayoutListener() {
@Override
public void layoutPrepared(ViewGroup view) {
final ViewGroup.MarginLayoutParams btnParams = (ViewGroup.MarginLayoutParams) createAgentBtn.getLayoutParams();
final int parentWidth = parentView.getWidth();
final int parentHeight = parentView.getHeight();
builder.setBackgroundDimens(parentWidth,parentHeight)
.commit();
}
});
}
As you can see in the method called "commit()", I am setting some background colors (for testing purposes). In conclusion I can see that the color is visible on the screen the first time, but not the next time etc.
I have debugged it (a lot!) and can verify that it reaches all of the places in the code that it should.
Upvotes: 0
Views: 240
Reputation: 3457
I found the answer myself. By changing the field:
public static boolean layoutLoaded
to "non-static":
public boolean layoutLoaded
The problem was solved, as there was only a single instance of this boolean, which caused confusion of the state when called the second time.
Upvotes: 1
Reputation: 2749
I can't be sure, but it looks like when commit()
is called a second time, the view probably does not == null
. And so the view will not get added again, because your addView()
call is within the null check.
You should test your current code with the "Show layout bounds" setting turned on. This has helped me out a lot when views aren't displaying as they should. Go to Settings -> Developer Options -> Turn on "Show layout bounds". Then, run your code to where it is supposed to add the second view, and check if you can see the layout bounds of your second overlay view that is supposed to be added.
Also, have you tried not calling overlay.bringToFront();
. You may also want to try keeping track of your added overlay views in a stack and pushing/popping as you add/remove them.
Hope this helps!
Upvotes: 1