0

I'm new on android development . I'm making a program and I want to change the button style , and I can do that.

here the picture about what is the button style recommend : enter image description here

as you see it is smooth and pretty ! But when I want to change the color of the button ( with a style ) I got problem ! : enter image description here

how can I change the style ( Button color ) and keep the button smooth and good-looking ?

hrskrs
  • 4,447
  • 5
  • 38
  • 52
F. Dev
  • 23
  • 3
  • this may help you http://stackoverflow.com/questions/26519979/coloring-buttons-in-android-with-material-design-and-appcompat – H Raval Oct 27 '16 at 10:14
  • 1
    you need to create your custom style similar to the Base style. and add put background color as Black – Manishika Oct 27 '16 at 10:15

4 Answers4

3

Use style as theme :

 <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:textColor="@android:color/white"
        android:theme="@style/blackButton"
    />

And add this style in your styles.xml

<style name="blackButton" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">@android:color/black</item>
        <item name="colorControlHighlight">@color/colorAccent</item>
    </style>
Manishika
  • 5,478
  • 2
  • 22
  • 28
  • thanks ! but I have still a question , can I add styles like array ? I mean I have 10 material colors and I want to add 10 style with these colors , can I add style like array ? – F. Dev Oct 27 '16 at 10:42
  • Array as in?? I didn't get your question. Do you want to add different styles in one ? – Manishika Oct 27 '16 at 10:43
  • No , I mean , when we want to add so many items we use Array , for example like this : double[] myList = {1.9, 2.9, 3.4, 3.5}; Now I want to set many colors as style , can I use array ? – F. Dev Oct 27 '16 at 10:47
  • I dont think there's any way to achieve that. As far as my knowledge you have to define the styles in xml and then you are add them dynamically to the views. But if you only want to change the color with the same theme in your layout then you can use android:backgroundTint="@color/desiredcolor" to your buttons with the desired colors. (Note : backgroundTint attribute is only used in API 21 or higher). – Manishika Oct 27 '16 at 11:11
0

Changing the background color removes the default material design style, you should instead create your own style for that button with parent theme Widget.AppCompat.Button.Colored :

 <!--fix to keep material design for buttons and add background color-->
    <style name="blackButton" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">@color/colorBlack</item>
    </style>

and than apply this theme to button using android:theme="@style/blackButton"

KlajdPaja
  • 959
  • 1
  • 8
  • 17
  • Before you answer my question I changed the accentColor ( in them editor ) but when I saw your answer I did added your texts , but it didn't work ! – F. Dev Oct 27 '16 at 10:31
0

Make one style which extend default button style and set background color to it. In styles.xml

<style name="black_button_style"
       parent="Base.Widget.AppCompat.Button.Colored">
             <item name="android:layout_width">match_parent</item>
             <item name="android:layout_height">wrap_content</item>
             <item name="android:typeface">sans</item>
             <item name="android:background">#000000</item>
</style>

In Your Activity xml.

   <Button 
    style="@style/black_button_style/>
Nikita Shah
  • 156
  • 10
  • I did that , but it gave me same result ( same result mean my second picture in my question ) – F. Dev Oct 27 '16 at 10:29
  • can you provide me a code snippet which you used as according to picture you don't need to give both style and color. – Nikita Shah Oct 27 '16 at 11:16
0

Add this code in styles.xml

<resources>
 <style name="App.Button.Style" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@color/red</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
</resources>

And change your button style

 <Button
     android:id="@+id/btnPlayVideo"
     android:text="Play Video"
     style="@style/App.Button.Style"/>
ramji
  • 1,982
  • 2
  • 13
  • 13