I'm creating camera app with grids and I want user to be able to change color of the grid by clicking button of the color he wants to use. The grids are created using onDraw, which is in DrawGrid class and this class is called from GridMenuFragment fragment, whereas color buttons are in ColorFragment fragment.
Right now, I successfully pass the color I want to use to the DrawGrid, yet it doesn't update and the color of the grid doesn't change. I use invalidate() in setCurrentColor method. I tried doing color change the same way I change the grid, yet it doesn't work. Any help will be appreciated.
Here's DrawGird.java code:
public class DrawGrid extends View {
int whichGrid;
Paint paint;
int currentColor=Color.WHITE;
public DrawGrid(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(currentColor);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
}
public void setWhichGrid(int number) {
whichGrid = number;
invalidate();
}
public void setCurrentColor(int color) {
switch(color){
case 0:
currentColor=Color.WHITE;
break;
case 1:
currentColor=Color.RED;
break;
case 2:
currentColor=Color.BLUE;
break;
}
paint.setColor(currentColor);
invalidate();
Log.d("DrawGrid", "currentColor is now1 " + currentColor);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path = new Path();
int width = getWidth();
int height = getHeight();
switch (whichGrid) {
case 1:
float lineV1 = width / 3f;
float lineV2 = (width / 3f) * 2f;
float lineH1 = height / 3f;
float lineH2 = (height / 3f) * 2f;
Log.d("DrawGrid", "currentColor is now3 " + currentColor);
canvas.drawLine(lineV1, 0, lineV1, height, paint);
canvas.drawLine(lineV2, 0, lineV2, height, paint);
canvas.drawLine(0, lineH1, width, lineH1, paint);
canvas.drawLine(0, lineH2, width, lineH2, paint);
break;
case 2:
float lineV = width / 3f;
float lineH = height / 3f;
canvas.drawLine(lineV, 0, lineV, height, paint);
canvas.drawLine(0, lineH, width, lineH, paint);
break;
}
}
}
Edit: adding OnCreateView from ColorFragment where I call DrawGrid.setCurrentColor():
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_color, container, false);
drawGrid=new DrawGrid(getContext(), null);
FrameLayout mainLayout = ((MainActivity)getActivity()).getRelativeLayout();
mainLayout.removeView(drawGrid);
mainLayout.addView(drawGrid);
Button red = view.findViewById(R.id.button1);
Button blue = view.findViewById(R.id.button2);
red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("ColorClass", "Button clicked");
drawGrid.setCurrentColor(Color.RED);
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(ColorFragment.this).commit();
}
});
blue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawGrid.setCurrentColor(Color.BLUE);
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(ColorFragment.this).commit();
}
});
return view;
}
I just copied your code and it works.
Can you attach the code which you use to call
DrawGrid.setCurrentColor(color)? I bet you just pass to it the value different from (0,1,2) and visually nothing changes. Even if you pass the correct value, the problem is not in theDrawGridclass, because it works properly.If you will pass there 0,1 or 2, then the View will be redrawn with passed color.
Why don't you just provide to this method color. And the caller side will determine which color it wants to use. Something like that: