Android Studio - How to create a transparent view that displays above current view



So I have one activity, with its own XML file, and I have separate view that only uses canvas and has no xml. Code is explained here, problem and question is below the code.


Heres the first view, that uses canvas do draw:



public class BallView extends View {
public BallView (Context context) {
super(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

...//create Ball objects
}
}


The ball object, that actually draws on the canvas:



public Ball(int xMax, int yMax) {
bounds = new RectF();
paint = new Paint();
...(initialize variables)
}

public void begin(Canvas canvas){
bounds.set(...);
paint.setColor(...);
canvas.drawOval(bounds, paint);
}


Here's the activity that calls the above view:



public class CircleBounceScreen extends ActionBarActivity{
View ballView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ballView = new BallView(this);
setContentView(ballView);
.... //do stuff
}

// some triggermethod
public void trigger(){
setContentView(R.id.activity_main);
//fade the button in
//wait for the button to be clicked
}
}


Now, I have a button in the activity_main XML file



<LinearLayout
...alignment stuff >
<Button android:id="@+id/restart_button"
...design stuff
android:visibility="gone"/>
</LinearLayout>


After the ballView executes for some time, it lets the activity know through a listener. Heres what I want to happen; the ballView STAYS IN VIEW, just in the background, and the button in the XML file is displayed above it. This will allow the ballView(which will be essentially frozen) and the button able to be clicked.


I have looked all over stack overflow, and all the solutions use a transparent activity background (but then changing setContentView(activity_main) would wipe out the previous view and it wouldnt matter if transparent or not.) Or, they merely define everything in the same XML file so they can simply setContentView to that XML file and be good. Note that I cannot do that because one of my view uses Canvas and the other uses XML.


So how do I display 2 views at the same time, with one transparently below the other?


No comments:

Post a Comment