I have a simple app that I'm trying to create. When a button is clicked it hides or shows a square on the the device screen. The only problem that I currently have is that "setContentView()" will over write the buttons that I have placed with the layout design view.
My question is how would I prevent a second call from setContetView() from over writing my button?
MainActivity.java
package com.example.hps.shapes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new CustomView(this));
}
}
CustomView.java
package com.example.hps.shapes;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
/**
* Created on 7/2/2016.
*/
public class CustomView extends View {
private Rect rectangle;
private Paint paint;
public CustomView(Context context) {
super(context);
int x = 150;
int y = 150;
int sideLength = 200;
// create a rectangle that we'll draw later
rectangle = new Rect(x, y, sideLength, sideLength);
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.GRAY);
}
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawColor(Color.BLUE);
canvas.drawRect(rectangle, paint);
}
}