I'm making an app on Android and when I add a button with Eclipse's plugin the button is transparent.For example: http://kakko76.free.fr/buttontransparent.png
But I don't want a transparent button!
I tried with background attribute on button but I losed the other effect on click or others.
How can I just disable button's transparency?
Thanks.
Found the perfect and simplest solution:
Just set the background of your button to
android:background="#android:drawable/btn_default"
But make sure to do it directly in the XML, not in the "Graphic Layout" interface because it probably won't work
Hope it saves someone's day ;)
By default the button shouldn't be transparent. However you could just create a Selector for your Button, so you can set it as its background but still have a click-effect whenever you click it.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="insert color"
android:endColor="insert color"
/>
<stroke
android:width="3dp"
android:color="insert color" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_pressed="false" >
<shape>
<gradient
android:endColor="insert color"
android:startColor="insert color"
/>
<stroke
android:width="3dp"
android:color="insert color" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
These are the default images for the Button in API Level <10 :
The selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_erase_pressed" />
<item android:state_focused="true" android:state_pressed="false"
android:drawable="#drawable/btn_erase_selected" />
<item android:drawable="#drawable/btn_erase_default" />
</selector>
And the images you'll find in your
\android-sdk\platforms\android-10\data\res\ folder
e.g:
As i found the Ice-Cream resources, the button background image is transparent, so you need to change the background to your own image, using selectors.
Related
I was trying to change the button background colors (default, pressed and focus), but I couldn't find a good way. I have read many posts about styling a buuton, but unfortunately these were post to create a compete new style. I want to keep everything the same except for the background colors.
I prefer using xml because I'm using a single layout for multiple activities. I was also looking for a solution that works for android 4.0 and up and below 4.0.
Can someone please help me?
If you want to change the default button color without losing the shadow, Then I recommend changing the style of the required button.
You can simply do this by adding this line to the button tag
style="#style/Widget.AppCompat.Button.Colored"
Sample code will look like this
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.AppCompat.Button.Colored"
android:text="Rate now"/>
now change the colorAccent to the desired color in the color.xml located under res->values->colors.xml file.
<color name="colorAccent">your color here</color>
you can visit the official developer page to get more information Styling buttons
Create a button_style.xml with the settings you want for your button like the example below, and place this file inside the drawable folder under the res folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#color/MyButtonDarkGray"
android:endColor="#color/MyButtonDarkGray"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#color/Green"
android:startColor="#color/Green"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#color/green_leaf_top"
android:startColor="#color/green_leaf_bottom"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Now, in your styles.xml under the res\values folder, add
<style name="button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/button_style</item>
</style>
This style will work for all your buttons only.
NOTE: The colors are defined separately in res\values\colors.xml, you can use your own colors.
Here is an xml for changing colors based on the states of button:
Create a drawable resource as below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#android:color/black" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#android:color/holo_orange_dark" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#android:color/holo_green_dark" />
<item
android:state_enabled="true"
android:drawable="#android:color/holo_blue_bright" />
</selector>
In java code set the drawable resource as below:
Button btn = (Button) findViewById(R.id.btn1);
btn.setBackground(this.getResources().getDrawable(R.drawable.mydrawable));
In xml set the drawable as background of the button in the xml:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mydrawable" />
NOTE: you can either set in java code or in xml.
I have some buttons which I set its background color as red, green and blue separately. When I press the button, click event is generated, but there is no change in gui for the user to know the button is pressed. Android button’s default background grayish color changes to orange and come back to grayish color after releasing the pressed state. How to implement this on colored button?
That is implemented via a StateListDrawable, represented by selector in XML. Reference: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Here is an example of a drawable that will be white by default, black when pressed:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:color/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
As mentioned by K-Ballo you can use StateListDrawable to implement a view with various different graphics depending on state. In your case Button is the view where two states are Button pressed and button not pressed.
We need to create a buttonselector.xml file in the drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_not_pressed" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/button_pressed" android:state_pressed="true"/>
</selector>
Create two separate xml files for the state of the button
button_not_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
</shape>
button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000"
android:centerColor="#FF0000"
android:endColor="#FF0000"
android:angle="270" />
</shape>
You will notice two html color codes #FF0000 & #FFFFFF which represent background color of the button according to the state.
In you main.xml where you set the style of your custom button
<Button
android:id="#+id/customButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="test"
android:textColor="#000000" />
In you activity class add the following two lines
Button customButton = (Button) findViewById(R.id.customButton);
customButton.setBackground(getResources().getDrawable(R.drawable.buttonselector));
Hope it helps
Try out this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<solid
android:color="#1E669B" />
<stroke
android:width="2dp"
android:color="#1B5E91" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1E669B"
android:endColor="#1E669B"
android:angle="270" />
<stroke
android:width="4dp"
android:color="#1B5E91" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
I am trying for repeat for buttons. I am able to get repeat for regular background type. But i was not able to find right, left and center using the repeat xml file.
If you know some solution with the xml file please help me.
Thanks in advance
The repeat xml file i have used is
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<bitmap
android:src="#drawable/barslice"
android:tileMode="repeat"
android:dither="true"/>
</item>
The button i require is:
you may get round-corner gradient buttons without even using images.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#449def"
android:endColor="#2f6699"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and simply set it as background to your button, textview or whatever you want
This will be nicely handled by 9 patch image.
you will have an image like this
and android will handle it automatically.
Just set it as background of any button.
Note: image for other states like pressed, focused and disabled should also be provided otherwise it will not change buttons state.
You can use 9-patch images. Check the following links.
http://developer.android.com/guide/topics/resources/drawable-resource.html#NinePatch
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
I have googled it already but can't find a good hint on how to create buttons in android that look like glossy shiny buttons with a slight 3d effect. See pic:
How can I programmatically do this?
Create a selector like this and apply it to your button drawable. Customize it to your requirements:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:endColor="#0905FB"
android:startColor="#9796FD"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#65655B" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
...
...
...
</item>
<item android:state_pressed="true" >
...
...
...
</item>
</selector>
First off i want to say im attempting (and mostly succeeding) to use purely XML for styling purposes.
I ahve a list view, displaying the standard textview. Everyhting is working, including my custom xml defined drawables.
Except that my clear "buttons" turn green where transparent, as is the android 2.2 default. Is there any way to override this?
Attached below is the drawable code for the button. The Top colors for gradients are solid, bottoms are 100% transparent.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#color/butttop"
android:endColor="#color/buttbot"
android:angle="270"
android:dither="true"
/>
<!-- <stroke
android:width="1dp"
android:color="#color/stroke2" />-->
<corners
android:radius="20dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#color/focustop"
android:startColor="#color/focusbot"
android:angle="270" />
<!--<stroke
android:width="1dp"
android:color="#color/stroke1" />-->
<corners
android:radius="20dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#color/butttop"
android:startColor="#color/buttbot"
android:angle="90" />
<!--<stroke
android:width="1dp"
android:color="#color/stroke1" />-->
<corners
android:radius="20dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
next is the style pertaining to them, incase its needed. *Note: menubutt is the name of the above file.
<style name="DrawerItems1">
<item name="android:textSize">16px</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">3</item>
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/menubutt</item>
</style>
Answer found at https://stackoverflow.com/a/6201407/924939. Works for me.
You have to set android:listSelector="#drawable/listcolor" in ListView.
Then you define a drawable named listcolor.xml with a transparent solid color (or whatever you need), so that list item background will appear and default green or orange color will disappear:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000"/>
</shape>
This may also have to be mixed with the following code in your ListView:
android:cacheColorHint="#color/transparent"
android:background="#color/transparent"