Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ms2030
2,973 PointsBackground Image & Canvas
How do I open a view for future use as a canvas and yet see image from XML underneath?
setContentView(R.layout.activity_main) -> shows my image from XML
setContentView(myView) -> deletes image from view even though I have Alpha set to 0 (transparent).
package self.draw.adrawprogram;
import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = new MyView(this);
setContentView(myView); // this clears the image from view
}
class MyView extends View {
Paint myPaint = new Paint();
MyView(Context context) {
super(context);
myPaint.setAlpha(0);
}
}
}
1 Answer
Ben Jakuben
Treehouse TeacherIn general you don't want to call setContentView more than once. You could try adding your MyView programmatically, using the addView method of your main layout like this example from StackOverflow: http://stackoverflow.com/a/10419021/475217