Android Studio - creating a transparent view displayed above the current view
So I have one activity with its own XML file, and I have a separate view that only uses canvas and does not have xml. The code is explained here, the problem and the question is below the code.
Here is 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 to 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 action that invokes 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 has been running for some time, it lets the activity know through the listener. That's what I want; ballView STAYS IN VIEW, only in the background, and the button in the XML file is displayed above it. This will allow the ballView (to be substantially frozen) and the button that can be clicked.
I looked at Stack Overflow and all solutions use the transparent focus of the activity (but then changing setContentView (activity_main) would destroy the previous view, and it wouldn't matter if transparent or not). Or they just define everything in the same XML file so they can just set the ContentView to that XML and be good. Note that I cannot do this because one of my views is using Canvas and the other is using XML.
So how do I display 2 views at the same time, with one transparently below the other?
source to share
Why not include the ball representation in XML?
<RelativeLayout>
<Button android:id="@+id/restart_button"
...design stuff
android:visibility="gone"/>
<com.packagename.BallView
android:id="@id/ball_view"/>
</RelativeLayout>
Then in the view, you can do:
public class CircleBounceScreen extends ActionBarActivity{
View ballView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.... //do stuff
}
// some triggermethod
public void trigger(){
// restart_button set visibility visible
//fade the button in
//wait for the button to be clicked
}
}
You may need to add additional constructors to the BallView so that it can be inflated in XML:
public class BallView extends View {
public BallView (Context context) {
this(context, null);
}
BallView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
BallView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
...//create Ball objects
}
}
source to share