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

Two rectangles showing up on top and bottom of CircleView #71

Open
MostafaAryan opened this issue Apr 29, 2020 · 2 comments
Open

Two rectangles showing up on top and bottom of CircleView #71

MostafaAryan opened this issue Apr 29, 2020 · 2 comments

Comments

@MostafaAryan
Copy link

Hi,
Two rectangles are showing up on top and bottom of my CircleView.
Screenshot is from an Android API 20 Genymotion device.

shape of view error

<com.github.florent37.shapeofview.shapes.CircleView
                                    android:id="@+id/circle_view"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    app:layout_constraintEnd_toStartOf="@+id/switch_view"
                                    app:layout_constraintTop_toTopOf="@+id/switch_view"
                                    app:layout_constraintBottom_toBottomOf="@+id/switch_view">

                                    <ir.uneed.app.app.components.widgets.MyTextView
                                        android:id="@+id/badge_text"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:padding="5dp"
                                        tools:text="20"
                                        android:textSize="11dp"
                                        android:textColor="@color/text_white"
                                        android:background="@color/background_blue"/>

                                </com.github.florent37.shapeofview.shapes.CircleView>
@kcochibili
Copy link

I have the same issue, but before I share my solution I need to mention that this does not happen in all devices.
I am testing with a Samsung Galaxy s8 : issue occurs
and a Samsung Galaxy Tab s6: issue does not occur

The issue is caused by the inability of this library to properly handle dynamic dimensions assigned to the view. (just like you are using wrap_content in your case)
The circle view works correctly only when the height and width are the same. So to fix this, I run a code at runtime that makes the length of the longest side of the CircleVIew match the length of the shortest side.

Doing that fixes the issue. here is my code..

    public void fixCircleViewDimension(CircleView cv){
        cv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                cv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                int width = cv.getWidth();
                int height = cv.getHeight();

                if(width > height){
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(height, height);
                    params.gravity = Gravity.CENTER;
                    cv.setLayoutParams(params);

                }else{
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, width);
                    params.gravity = Gravity.CENTER;
                    cv.setLayoutParams(params);
                }

            }
        });


    }

side note: replace the FrameLayaout with whatever the Parent of your CircleView is.

@MostafaAryan
Copy link
Author

I have the same issue, but before I share my solution I need to mention that this does not happen in all devices.
I am testing with a Samsung Galaxy s8 : issue occurs
and a Samsung Galaxy Tab s6: issue does not occur

The issue is caused by the inability of this library to properly handle dynamic dimensions assigned to the view. (just like you are using wrap_content in your case)
The circle view works correctly only when the height and width are the same. So to fix this, I run a code at runtime that makes the length of the longest side of the CircleVIew match the length of the shortest side.

Doing that fixes the issue. here is my code..

    public void fixCircleViewDimension(CircleView cv){
        cv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                cv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                int width = cv.getWidth();
                int height = cv.getHeight();

                if(width > height){
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(height, height);
                    params.gravity = Gravity.CENTER;
                    cv.setLayoutParams(params);

                }else{
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, width);
                    params.gravity = Gravity.CENTER;
                    cv.setLayoutParams(params);
                }

            }
        });


    }

side note: replace the FrameLayaout with whatever the Parent of your CircleView is.

Thanks for your response.

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