Friday, December 15, 2017

android - Standard button with different color

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="#ffffff"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Normal Button"
        />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Accent Color Button"
        style="@style/Widget.AppCompat.Button.Colored"
        />
    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Green Color Button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:theme="@style/Colored.Button.Green"
        />
    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Blue Color Button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:theme="@style/Colored.Button.Blue"
        />
    <Button
        android:id="@+id/btn_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OrangeRed Color Button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:theme="@style/Colored.Button.OrangeRed"
        />
    <Button
        android:id="@+id/btn_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lime Color Button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:theme="@style/Colored.Button.Lime"
        />
</LinearLayout>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    <!-- colored button style -->
    <style name="Colored.Button.Green" parent="AppTheme">
        <item name="colorAccent">#008000</item>
    </style>

    <style name="Colored.Button.Blue" parent="AppTheme">
        <item name="colorAccent">#0000FF</item>
    </style>

    <style name="Colored.Button.BlueViolet" parent="AppTheme">
        <item name="colorAccent">#8A2BE2</item>
    </style>

    <style name="Colored.Button.OrangeRed" parent="AppTheme">
        <item name="colorAccent">#FF4500</item>
    </style>

    <style name="Colored.Button.Lime" parent="AppTheme">
        <item name="colorAccent">#00FF00</item>
    </style>

</resources>