Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] button – How to make an irregular shape component clickable? Android

Posted on November 11, 2022 By

Solution 1 :

With the Material Components Library you can define CornerTreatment to apply to the components.
There are some built-in CornerTreatment like CutCornerTreatment or RoundedCornerTreatment but you can built your own CornerTreatment.

Something like:

class ConcaveRoundedCornerTreatment : CornerTreatment() {

  override fun getCornerPath(
      shapePath: ShapePath,
      angle: Float,
      interpolation: Float,
      radius: Float
  ) {
    val interpolatedRadius = radius * interpolation
    shapePath.reset(0f, interpolatedRadius, ANGLE_LEFT, ANGLE_LEFT - angle)
    shapePath.addArc(
        -interpolatedRadius,
        -interpolatedRadius,
        interpolatedRadius,
        interpolatedRadius,
        ANGLE_BOTTOM,
        -angle
    )
  }

  companion object {
    const val ANGLE_LEFT = 180f
    const val ANGLE_BOTTOM = 90f
  }
}

And just apply it to the Button:

<com.google.android.material.button.MaterialButton
    android_id="@+id/concave"
    app_cornerRadius="16dp"
    ..>

with:

    val materialButton = findViewById<MaterialButton>(R.id....)
    val concaveRoundedCornerTreatment = ConcaveRoundedCornerTreatment()

    materialButton.shapeAppearanceModel = materialButton.shapeAppearanceModel.toBuilder()
            .setTopRightCorner(concaveRoundedCornerTreatment)
            .build()

enter image description here

Solution 2 :

create a shape drawable like this:

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns_android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns_android="http://schemas.android.com/apk/res/android"
        android_shape="rectangle">
        <solid android_color="@color/colorPrimary" />
        <stroke
            android_width=".05dp"
            android_color="#d2d2d2" />
        <corners
            android_bottomLeftRadius="10dp"
            android_bottomRightRadius="10dp"
            android_topLeftRadius="10dp"
            android_topRightRadius="10dp" />
    </shape>
</item>
<item
    android_bottom="410dp"
    android_left="100dp"
    android_right="-100dp"
    android_top="-300dp">
    <shape xmlns_android="http://schemas.android.com/apk/res/android"
        android_shape="oval">

        <solid android_color="#ffffff" />
    </shape>
</item>

then set this drawable as background for your view(eg:linearlayout)
Note:please adjust the dimensions according to your need

Problem :

I want to make a clickable component with this irregular shape, but I have no idea how I could do it, I have been looking for a couple of days but I have not seen any solution. There should be text and images inside the component.

enter image description here

Any suggestion?

Thank you

Comments

Comment posted by Gabriele Mariotti

Use a MaterialButton with a custom ShapeAppareance

Comment posted by JoSeBu

@gabrielemariotti I’m looking for but can’t see the shapeappareance property for MaterialButton

Comment posted by CornerTreatments and EdgeTreatment

Check the

Comment posted by JoSeBu

@gabrielemariotti yes, but is for rounded corners or cut corners, i need an “inverse rounded” corners. Or something I’m not seeing … Can you clarify it me?

READ  [FIXED] uri - The correct way to create a file path on Android 10.0
Powered by Inline Related Posts

Comment posted by Gabriele Mariotti

You can build your own CornerTreatment. Just “draw” the corner path. In my answer you can find an example.

Comment posted by Gabriele Mariotti

@JoSeBu Yes, you can apply the

Comment posted by JoSeBu

I am testing changing only: val materialButton = findViewById(R.id.concave) but When I compile the field does not appear, do I have to change something else?

Comment posted by Gabriele Mariotti

@JoSeBu Just use the

Comment posted by Gabriele Mariotti

I am using the 1.2.0-beta01, but to be honest I am not sure that the issue is the version. There is nothing special in my layout.

Comment posted by JoSeBu

because indeed the version was the problem, with your version it works correctly, thank you very much

Comment posted by JoSeBu

ok, this is to put it vertical, I have tried with rotate and I could not rotate it, only one square comes out. And with this I would have the problem that I cannot put any type of shadow, in the end this is like putting a background image. From what I see making forms of this style it is not contemplated in android, right?

Android Tags:android, button

Post navigation

Previous Post: [FIXED] Android Studios error : Unable to locate adb location
Next Post: [FIXED] android – Here Maps Navigation samples seem to be missing the Navigation Library

Related Posts

[FIXED] xamarin – How mono runtime is embbedded into Android application Android
[FIXED] android – Action Required: Your app is not compliant with Google Play Policies – app bundle delete Android
[FIXED] Service Binder object casting issue during service connection Android
[FIXED] android – error: expecte when setting up binding adapters for two way data binding Android
[FIXED] java – Android management API service file allocation Android
[FIXED] How to use Android ConstraintLayout component to display any image within a square Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme