Changing XML color from java code - java

I want a button shaped as a circle which i can change the background color of from my .java file.
I dont want to use circular images which change to accomplish this because i want to potentially use any hex color and i would need far too many images.
I have created an XML file called roundbutton which displays a round button by using 45 corner radius, but i need to be able to change the colour attribute(android:color="#ff0000" for instance)
from my .java
The round button displays ok right now but only displaying the colour i have set in the xml file, If I use buttonname.setBackgroundColor(Color.rgb(0, 255, 0)); the circle will be replaced by a square, so using the XML colour attribute would work if only i could change it from .java!
By the way im changing the colour of this round button on the press of another button.
hope this makes sense, thanks in advance!
XML circle:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000" /> <<<<NEED TO CHANGE THE COLOUR CODE HERE FROM WITHIN .JAVA
<corners android:bottomRightRadius="45dp"
android:bottomLeftRadius="45dp"
android:topRightRadius="45dp"
android:topLeftRadius="45dp"/>
</shape>

Create another shape xml which have different color and access file using .setBackgroundColor(getResources().getColor(R.color.your_color_in_xmlfile));

use .setBackgroundColor(getResources().getColor(R.color.your_color_in_xmlfile)); in onClickListener() on button which you want to press for changing color else use
View.setBackgroundColor(Color.parseColor("#E7FC3A"));
and if you are using .java file i.e dunamically than dont set color from .xml file else it will not show effect on Orientation change

Related

Rounded corners in layout with custom color on Android

I've got an xml for the shape and color of my drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/light_red"/>
<corners android:radius="10dp"/>
</shape>
But I would like to use this drawable as a global one, like having the corners rounded when I'm using it, but being able to change the backgroundColor.
But when I'm trying to change the backgroundColor programatically, it overrides the color like I want but it also override the rounded corners which becomes flat.
For the moment, if I want to have rounded corners with different colors, I need to create multiple XML files with only the android:color changing.
To programmatically change a color globally defined in a shape, use this:
findViewById(R.id.your_xml_element).getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
Just for other people coming here after, I did some research with what you gave me, this setColorFilter is deprecated since API 29.
I found the solution for non deprecated method (a little bit longer but working):
findViewById(R.id.your_xml_element).getBackground().setColorFilter(BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.BLUE, BlendModeCompat.SRC_ATOP));

How to programmaticaly change color of a color resource?

I want to change a button that has a shape with a color set:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/custom"/>
<corners android:bottomLeftRadius="10dp"/>
</shape>
and in my colors list resources I have <color name="custom">#A281E0</color>.
Is it possible to programmatically change the color of custom? Thanks in advance
Maybe it will be better to use dynamically color changing, like this:
String color = "your hex color"
Int colorToUse = Color.parseColor(color)
Then set a background color of your button:
Int buttonId = findViewById(R.id.button_id)
buttonId.setBackgroundColor(colorToUse)
Set a color that you need to color variable. Something like this.
I can't comment because i don't have enough reputation.
Why do you need to do this? Perhaps there is another plan of attack to solve the requirement, for instance a state list drawable, which will allow for different drawables based on varying state - ie selected, focused, etc.
https://developer.android.com/guide/topics/resources/drawable-resource#StateList
Because I'm a newbie I can't mark this a duplicate but I think this question will give you the answer you want. Apparently, you can create a class that extends Resources and override the method getColor(int).

Changing a color resource programmatically?

I got a little circle TextView from Vicky Vicent ("Android: Creating a Circular TextView?")
In my situation this one should show a Level and I would like to define the background color for certain areas by myself. How can I change this one with my Activity?
<solid android:color="#9FE554" />
To change the solid color of the xml drawable resource use the below code.
GradientDrawable bgShape = (GradientDrawable)textview.getBackground();
bgShape.setColor(Color.BLACK);

How to draw geometric shapes on buttons

We are making a quiz app as a school project where we are supposed to display a question and show 4 answers. For 8 seconds we are supposed to show 4 buttons with different geometric shapes of different colors(and text for the answer). Then when the timer hits zero the geometric shapes will change colors and switch positions. Then the buttons should be clickable and we can hit the correct answer.
My problem is that I can't find a way to draw these objects on a button, or anything clickable. I could have just used images of triangles, circles etc. on ImageButton, but as the objects need to change colors, it will be difficult. Here is a photo of what it is supposed to look like:
(edit) https://i.stack.imgur.com/05Far.png
Hope someone can help me with this, thanks.
Step 1
For creating geometrical shapes you will have to create a different xml file in the drawable folder for each shape. [for drawing circle you need to oval]. XML file should be like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="any_shape_name*" >
<stroke
//optional: for setting border.
android:dashGap="4dp"
android:dashWidth="10dp"
android:width="6dp"
android:color="#color/black" />
<solid android:color="#color/white" />
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" /></shape>
Step 2
Create a button in main layout file and add this xml as the background
android:background = "#drawable/shape_file_name"
This was just a simple 2 step method.
Note:- You can also draw shapes dynamically and set them as the background of the button.
For this way please refer to this beautiful link.
Hope this helps.

Android Java how to make single bar graphic

I need to show a "graphic" with part of the box colored in.
In the box it it will have text, but then I want to be able to put a color on the background to show how high the numbers are. see the graphic I made in excel. (I guess I am not allowed to upload a picture. So not sure how to explain this..
Click here to see the excel picture
Anyone know how to do this?
You can simply make an XML Drawable, something like this (save under res/drawable):
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
<gradient
android:angle="0"
android:type="linear"
android:startColor="#FF00FF00"
android:endColor="#FFFFFFFF"
/>
<stroke
android:width="1dp"
android:color="#FF000000"
/>
/>
then just use a TextView, with this Drawable set as your android:background attribute.
Do you want the colour to dynamically change colour for each value? Or are you okay with just having a few colour "levels" (e.g. red, yellow, green).
The easier route would be to just have a few set colours and create those gradients in photoshop and then apply those textures to the back of an ImageView using the background attribute.
Here are some tutorials on how views work: http://developer.android.com/resources/tutorials/views/index.html

Categories

Resources