Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view inside Button

Tags:

android

Is there a way to put custom_view.xml in a Button instead of the title? I want the title of my button to have 2 different strings, both using different fonts. I figure the only way to do this is to have a custom view inside of my button?

EDIT

I need to derive from the Button component to have the ripple effect when I click on the button. I don't want to use a FrameLayout with a touch listener.

EDIT 2

Would there be any way to get the shadow/border effect of a button too? I pretty much want to make it look/feel like a natural Android button, with the same look/feel when clicked, but with a custom view inside it.

like image 627
Drake Avatar asked Nov 27 '25 01:11

Drake


1 Answers

You can just take the style of the button and apply it to any ViewGroup. This takes care of most properties (padding, minHeight, background drawable, ripple drawable, elevation when pressed etc.).

For example to make a simple colored button with an icon (style for TextView was changed to match the button style as well):

<LinearLayout
    android:id="@+id/buttonViewGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    style="@style/Widget.AppCompat.Button.Colored">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_icon_test"/>

    <TextView
        style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
        android:text="Hallo world!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Then you are free to set any OnClickListener to LinearLayout and you're set.

like image 132
Pawel Avatar answered Nov 29 '25 17:11

Pawel