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

Button Revamp-Pm ccct 536 #2896

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
-ui changes for rounded button
pm-dimagi committed Nov 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 146e5abdf1d44ead3e34afbd274d8e8abe41565e
Binary file added app/res/font/roboto_bold.ttf
Binary file not shown.
Binary file added app/res/font/roboto_italic.ttf
Binary file not shown.
Binary file added app/res/font/roboto_medium.ttf
Binary file not shown.
Binary file added app/res/font/roboto_regular.ttf
Binary file not shown.
140 changes: 140 additions & 0 deletions app/src/org/commcare/activities/components/RoundedButtonDrawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.commcare.activities.components;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

public class RoundedButtonDrawable extends Drawable {
private int backgroundColor;
private float cornerRadius;
private boolean borderVisible;
private int borderColor;
private float borderRadius;
private float borderWidth;
private Paint backgroundPaint;
private Paint borderPaint;

// Constructor
public RoundedButtonDrawable(int cornerRadius, int backgroundColor, boolean borderVisible, int borderColor, float borderRadius, float borderWidth) {
this.cornerRadius = cornerRadius;
this.backgroundColor = backgroundColor;
this.borderVisible = borderVisible;
this.borderColor = borderColor;
this.borderRadius = borderRadius;
this.borderWidth = borderWidth;

// Initialize background paint
backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backgroundPaint.setColor(backgroundColor);

// Initialize border paint if border is visible
if (borderVisible) {
borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
}
}

// Method to set the background color
public void setBackgroundColor(int color) {
this.backgroundColor = color;
backgroundPaint.setColor(color);
invalidateSelf();
}

// Method to get the corner radius
public float getCornerRadius() {
return cornerRadius;
}

// Method to set the corner radius
public void setRadius(float radius) {
this.cornerRadius = radius;
invalidateSelf();
}

// Method to set border visibility
public void setBorderVisible(boolean visible) {
this.borderVisible = visible;
if (visible && borderPaint == null) {
borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
}
invalidateSelf();
}

// Method to set border color
public void setBorderColor(int color) {
this.borderColor = color;
if (borderPaint != null) {
borderPaint.setColor(color);
}
invalidateSelf();
}

// Method to set border radius
public void setBorderRadius(float radius) {
this.borderRadius = radius;
invalidateSelf();
}

// Method to set border width
public void setBorderWidth(float width) {
this.borderWidth = width;
if (borderPaint != null) {
borderPaint.setStrokeWidth(width);
}
invalidateSelf();
}

@Override
public void draw(Canvas canvas) {
// Draw the background with rounded corners
canvas.drawRoundRect(new RectF(getBounds()), cornerRadius, cornerRadius, backgroundPaint);

// Draw the border if visible
if (borderVisible && borderPaint != null) {
float halfStrokeWidth = borderPaint.getStrokeWidth() / 2;
RectF borderRect = new RectF(getBounds());
borderRect.inset(halfStrokeWidth, halfStrokeWidth);
canvas.drawRoundRect(borderRect, borderRadius, borderRadius, borderPaint);
}
}

@Override
public void setAlpha(int alpha) {
backgroundPaint.setAlpha(alpha);
if (borderPaint != null) {
borderPaint.setAlpha(alpha);
}
invalidateSelf();
}

@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
backgroundPaint.setColorFilter(colorFilter);
if (borderPaint != null) {
borderPaint.setColorFilter(colorFilter);
}
invalidateSelf();
}

@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}

private int dpToPx(int dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round(dp * density);
}
}