0

Is it possible to extend Button class without loosing AppCompat features like coloring/tinting/shadows?

For now, if I create custom class which extends Button and use it in layout it becomes white background/black foreground.

curioushikhov
  • 2,461
  • 2
  • 30
  • 44
  • Is this what you are looking for? http://stackoverflow.com/questions/26519979/coloring-buttons-in-android-with-material-design-and-appcompat – Rohit5k2 Jan 19 '16 at 12:16
  • You could start with this api: https://github.com/Clans/FloatingActionButton Have a look how he has created a floating button custom using AppCompoact Library – zIronManBox Jan 19 '16 at 12:16
  • @Rohit5k2 - no I am not looking for coloring with styles, but I need some custom button with all render features of AppCompatButton. – curioushikhov Jan 19 '16 at 13:09
  • @zIronManbox - your button is fully overwrites drawing of button buy I need only to add some logic in class without touching rendering. – curioushikhov Jan 19 '16 at 13:09
  • @curioushikhov What kind of logic. If so, you could extend the already available Button component which is using AppCompact, and use this as a view into your Layout. – zIronManBox Jan 19 '16 at 13:10
  • Lear more about creating custom components, I think that's what you are looking at. – zIronManBox Jan 19 '16 at 13:10
  • FYI:http://developer.android.com/training/custom-views/create-view.html http://www.vogella.com/tutorials/AndroidCustomViews/article.html – zIronManBox Jan 19 '16 at 13:11
  • @zIronManBox I know how to build custom views, there is problem because AppCompat library uses custom inflater which converts button tag in layout to AppCompatButton instance. It doesn't recognize any subclasses of Button or AppCompatButton classes and because of this it looses theming. – curioushikhov Jan 19 '16 at 14:26
  • 1
    I think, then you would have to use the styling xmls for your custom view class. I guess, when your custom class gets extended, the BaseStyle xml is lost, I think you would have to apply or redefine baseStyle. – zIronManBox Jan 19 '16 at 14:31

1 Answers1

2

For example custom button:

package me.shikhov.buttontest;

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;

/**
 * Created by andrew on 19.01.16.
 */
public class MyButton extends
        AppCompatButton
{
    public MyButton(Context context) {
         this(context, null);
    }

    public MyButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

Example layout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="me.shikhov.buttontest.MainActivity">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST BUTTON"
            android:layout_centerInParent="true"
            android:id="@+id/standard_button"
            />

        <me.shikhov.buttontest.MyButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:theme="@style/Widget.AppCompat.Button"
                android:text="MY BUTTON TEST"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/standard_button"
                />

</RelativeLayout>

Important notes:

  1. MyButton should extend AppCompatButton and not android.view.Button

  2. You should explicitly set android:theme. That theme should be derived from Widget.AppCompat.Button theme.

curioushikhov
  • 2,461
  • 2
  • 30
  • 44