Reputation: 399
I am creating a custom Button and I am using the Drawable.setTintList to have colors for default,pressed and disabled states.
I want to actually add a border to the button, which I am trying by doing:
val d = newGradientDrawableForShape()
d.setShape(GradientDrawable.RECTANGLE)
d.setColor(Color.WHITE)
d.setStroke(20, Color.GREEN)
With this the border is not visible, however if I do not use tint list then I do see the border.
Is there a way I can use setStroke and TintList?
I tried d.setStroke(width, colorList) and it didn't work either.
Upvotes: 3
Views: 2229
Reputation: 518
Have you tried:
setStroke(
(strokeSize * resources.displayMetrics.density).toInt(),
ColorStateList.valueOf(ContextCompat.getColor(context,colorRes)),
0f,
0f
)
Upvotes: 0
Reputation: 21
I have the same issue but I also achieved another approach.
Here's the XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/view_card_header_large_border_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.card.MaterialCardView
android:id="@+id/view_card_header_large_border_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardBackgroundColor="?paper_color"
app:cardCornerRadius="10dp"
app:cardElevation="0dp"/>
</FrameLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view_card_header_large_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here's the code:
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {leftColor, rightColor});
// card background
gradientDrawable.setAlpha(70);
gradientDrawable.setCornerRadius(borderBaseView.getRadius());
container.setBackground(gradientDrawable);
// card border's background, which depends on the margin that is applied to the borderBaseView
gradientDrawable.setAlpha(90);
gradientDrawable.setCornerRadius(12dp);
borderContainer.setBackground(gradientDrawable);
The idea is to set background to two views and the top view covers the "border" base view but leaves a 1dp
margin to be the "border".
So that you will have a gradient background in the view and also the gradient background in the border.
Upvotes: 0
Reputation: 1862
you can use drawable with state example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="10dp" android:color="#6699ff" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="10dp" android:color="#669900" />
</shape>
</item>
</selector>
Upvotes: 1