Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Selected Day background drawable is becoming oval #1052

Open
anallur opened this issue Jul 9, 2020 · 4 comments
Open

Custom Selected Day background drawable is becoming oval #1052

anallur opened this issue Jul 9, 2020 · 4 comments

Comments

@anallur
Copy link

anallur commented Jul 9, 2020

We wanted to reduce the size of the Selected day background circle.
We used a custom drawable to put as the selection drawable in a new Decorator.
We have a custom tile height, which makes the day view as a rectangle than a square.

circle_drawable
?xml version="1.0" encoding="utf-8"?>
inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetTop="5dp"
android:insetLeft="13dp"
android:insetRight="13dp"
android:insetBottom="5dp">
shape android:shape="oval">
solid android:color="@color/colorPrimary" />
size
android:width="@dimen/dimen_12"
android:height="@dimen/dimen_12" />
/shape>
/inset>

inset tag close

Can you please help, on how we can get a circle background drawable always?

Screenshot 2020-07-09 at 10 23 07 AM

Screenshot 2020-07-09 at 10 20 41 AM

@anallur
Copy link
Author

anallur commented Jul 9, 2020

I am not able to post the correct circle_drawable code, i have left out < infront of the tags on purpose

@dalewking
Copy link

We are experiencing the same thing with out using a custom drawable. For some reason when the calendar first appears the circle starts as an oval then switches to a circle after a fraction of a second. So you see a flash of the oval, which is enough to make it really look unprofessional.

@dalewking
Copy link

So after some debugging I think I see what is going on. In DayView.onLayout:

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    calculateBounds(right - left, bottom - top);
    regenerateBackground();
  }

Here you calculate the bounds which sets circleDrawableRect for the standard circle background and tempRect with the bounds for a custom drawable. Then in regenerateBackground you actually build the circle drawable or get the custom drawable and set it as the backgroundDrawable for the DayView, BUT you did not set the bounds on this drawable so it has no bounds.

You don't actually set the bounds for the background drawable until onDraw():

  @Override
  protected void onDraw(@NonNull Canvas canvas) {
    if (customBackground != null) {
      customBackground.setBounds(tempRect);
      customBackground.setState(getDrawableState());
      customBackground.draw(canvas);
    }

    mCircleDrawable.setBounds(circleDrawableRect);

    super.onDraw(canvas);
  }

The problem is that in the View.draw code the backgroundDrawable is drawn before onDraw is called. The drawing of the backgroundDrawable will resize it to the size of the DayView because no bounds have been set. After it gets drawn at the size of the DayView you then change the bounds in onDraw and it will get redrawn the correct size.

@dalewking
Copy link

After playing with it some more the real issue is that trying to control the bounds of the background drawable is not the right way to resize it because the first time it is drawn its bounds will get set to the size of the view. What you really need to do instead of adjusting the bounds of the drawable is wrap the drawable in an InsetDrawable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants