I've got a 5 buttons. They have a similar corners radius but different background color. Can i make one shape for radius and change buttons color with xml resources? So, main problem is set background color for them. I can't use android:background attribute twice. I thought about style, but in the syles i also need to set thia attribute twice. Is t possible to make only 1 shape file, or should create own file for each button?
here's what works:
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="#0f0"/>
</shape>
and in onCreate in your java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button);
b.setBackgroundColor(Color.BLUE);
}
im Glad if i helped:)
Related
I have a set of buttons in Android which all use the same custom drawable as the background, When a button is pressed, I'd like the drawable that is used for the background to change to a different custom drawable. The code I used to do this is below:
selectedButton.setBackgroundResource(R.drawable.roundedbuttongreen)
// Change the background resource to the roundedbuttongreen drawable
the XML for the custom drawable called roundedbuttongreen.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="#4CAD71"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomLeftRadius="50dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"/>
</shape>
The problem that occurs is that the colour of the button is changed but not to the correct colour the colour it changes to instead is grey, it should be green. The screenshot is below:
image of the button after the background resource has been changed, it should be green
If anyone could let me know what I'm doing wrong that would be great
I suggest you to add checked status on custom shape like this then add this shape as a background for your buttons .
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"
android:state_checked="false"/>
<item android:drawable="#drawable/roundedbuttongreen"
android:state_checked="true"/>
</selector>
I hope it will work with you .
I had a look through my xml code for my layout and in particular my buttons, The issue was that the buttons backgroundTint property had a value which was grey, as a result when I tried to change the background drawable, the colour of the button was always grey.
To fix the issue, I changed the value of the background tint to green and the issue was resolved
This might be something basic, but I was unable to find the solution anywhere. So, suppose I had an .xml file called properties.xml in the drawable folder.
Say it looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
<shape
android:shape="rectangle">
<corners
android:radius="10dp"/>
<solid android:color="#color/GreenGray">
</shape>
</item>
</selector>
If I have multiple buttons in my activity_main.xml, and I want them all to have their background set to properties.xml, is there a way to change ONLY the background colors of the buttons so that they're all different from each other without making a whole new xml file for every button?
Kind of like how with classes with css, you can set an object to a class and change only specific properties of that class?
You could try this:
//get the button by id which you want do modify
Button myBtn = (Button) findViewById(R.id.your_btn_id);
//get drawable from button
GradientDrawable drawable = (GradientDrawable) myBtn.getDrawable();
//set color
drawable.setColor(color)
I'm trying to set a drawable background to a button which is created programmatically.
Below is the code where I create the button and set the background
Button increaseQuantity = new Button(this);
increaseQuantity.setText("+");
//increaseQuantity.setBackgroundResource(R.drawable.quantity_button);
increaseQuantity.setBackgroundDrawable(getResources().getDrawable(R.drawable.quantity_button));
Below is the xml which has the code for the drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<stroke android:width="1dp" android:color="#ee404040" />
<size android:width="2dp" android:height="2dp"/>
<gradient android:startColor="#11809100" android:centerColor="#11910000" android:endColor="#55FFB900" android:angle="45" />
</shape>
As per the xml my button should be in round shape (because width and height are the same), but I'm not able to get a round shaped button, instead I'm getting an oval shaped button. Could anyone correct this code to get a circular button?
just two things here:
Replace the deprecated methods: setBackgroundDrawable and getDrawable are deprecated.
Set the dimensions programmatically
This should do the work:
increaseQuantity.setBackground(ContextCompat.getDrawable(this, R.drawable.round));
increaseQuantity.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
//replace 100 with your desired diameter of the button.
Also, you will have to replace LinearLayout to the parent layout you're associating your button with.
After the call of EditText.setBackgroundColor(Color.RED) I see only a red rectangle with text inside it, but I want to change only the text background and to keep the standard bottom border which is grey when the field has no focus and blue if it has focus. Can I do it programmatically?
I want to change the text background to red if the input is invalid and to transparent if it is valid.
I think the best way is to work with drawable/shape.xml as background and upon logic code situation call EditText.setBackground(some_other_shape.xml). Here is an example for shape file xml demonstrating how to use:
Border color ("stroke"): In this example some custom color from colors.xml
Fill color ("solid"): In this example Android default transparent color
Even image icon inside
<!--Example for custom shape file xml design-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#mipmap/icon_image" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/orange_primary" />
</shape>
</item>
</layer-list>
So just prepare several shapes for each situation you need. Another approach is to work with layout params etc. but I think this one is faster and gives more control for custom design in easy to understand code.
in my current project I'm building an App with API 10 which should look like an ICS App.
Since my GUI is not very complex I can build this all manually, but I have some problems with button states.
I googled a lot and found nothing that would work for me so I would appreciate any help ;)
I designed an action bar with google ICS stock icons. When for example someone touches the search-loupe-icon, which has an 8dp padding around it, it's background color should change to a defined highlight-color-value.
So now you would suggest probably a selector drawable. But my case is: I have an icon-drawable which has an background-color, which I want to change. I dont want to change the icon, so since I cant change the backgroundcolor from a selector-xml this doesn't work.
(I thought I can setup multiple drawable-xml with my icon static bg colors so that I could refer with one selector to those different colored icons, but since a shape-drawable-xml cannot have a source and an bitmap-drawable-xml cannot have a background color, this is no sollution.)
If I try to change the background color in my onclick-listener (also tried onTouch) with:
ImageButton searchIcon = (ImageButton) findViewById(R.id.upper_actionbar_icon_search);
searchIcon.setBackgroundColor(R.color.android_ics_blue_light);
The Icon gets a dark grey bg color. This is also true for any other color altough nothing lies on top of the ImageButton. I also tried an ImageView.
So could someone explain to my please how the hell I can define a highlighted bg color-image without any hacks (default-gone image that lies above the icon and is set visible onclick).
Thanks so much!!
Okay the solution is simple.
Write yourself a drawable selector which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#color/android_ics_blue_light"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/android_ics_blue_light"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/light_grey"/>
</shape>
</item>
Within your ImageButton code define your icon as src and your new drawable as background, like this:
<ImageButton
<!-- ... -->
android:src="#drawable/icon_search"
android:background="#drawable/actionbar_ontouch"
/>
Works like a charm!