I am new to stackoverflow! I have been working on an app in android, but got stuck at a specific point.
I have an activity (activity 1) which has four buttons and a canvas drawn to it. The canvas is created in a custom view (view 1) which is then drawn to the acitivty through an xml file. I want to save the canvas as a jpg file to my sd card. The action of saving has to happen at a button press.
How I intended it to work was as follows: By clicking on the button a boolean is set to true. This boolean has to be sent from activity 1 to the view 1 in which the canvas is drawn. This view 1 would then monitor if the boolean is set to true or false. If the boolean is set to true a snippet of code would run that would save the canvas to a jpg.
My problem is at the monitoring of the boolean. How can I continuoisly monitor the state of the boolean in a custom view(view 1)?
Activity 1: So here I have my main activity in which I have a button that sets the boolean to true when clicked. (I stripped down the code to the essential part for the forum)
public class ShowImage extends Activity implements ColorPickerDialog.OnColorChangedListener {
public boolean saveCanvas = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.image);
Button saveButton = (Button)findViewById(R.id.savePic);
saveButton.setOnClickListener(saveListener);
}
private OnClickListener saveListener = new OnClickListener(){
public void onClick(View v) {
saveCanvas = true;
};
View 1: This is the custom view I created in which the canvas is drawn. In this code I want to continuously monitor the state of the boolean that I just created in Acitivty 1.
public class MyView extends View {
boolean saveCanvas = false;
Context context;
public MyView(Context c, AttributeSet as) {
super(c, as);
....
}
//CONTINUOUSLY MONITOR THE STATE OF THE BOOLEAN
//IF TRUE SAVE PICTURE, ELSE CONTINUE CODE
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
....
}
@Override
protected void onDraw(Canvas canvas) {
....
}
}
I hope my question is somehow clear and hopefully I provided enough of my code to have someone help me out. Please let me know if additional information is needed.
Cheers!
If I understand you correctly, you want to save the current canvas to a jpg as soon as a user hits a button. In such an interactive case, polling for a boolean to change is not the way you want to go about this. A user action is provoking an application action so you should tell the view to capture a picture when the user clicks the button.
For instance, the view could have a method
then in your onClick handler you’d simply invoke this method