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);
Related
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));
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).
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.
I'm working on an App and want to have an elevation effect on ImageView or any View (!CardView in support library) in Pre L APIs. But i'm not able to achieve that what i tried is used android:elevation property but it doesn't have any effect (No elevation).
I can't find any API, if someone point out any documentation to achieve this in Pre L or any snippet from Support library is highly appreciated.
If you want to set views in 3D shape, View.setElevation() and View.setTranslationZ() is a good idea.
But unfortunately, the two attributes and methods are introduced since Android API 21. So, you can't use them on pre-L or API 21- devices.
But, there is still a way to custom your views' shadows and outlines.
The bounds of a view's background drawable determine the default shape of its shadow. Outlines represent the outer shape of a graphics object and define the ripple area for touch feedback.
Consider this view, defined with a background drawable:
<TextView
android:id="#+id/myview"
...
android:elevation="2dp"
android:background="#drawable/myrect" />
The background drawable is defined as a rectangle with rounded corners:
<!-- res/drawable/myrect.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>
The view casts a shadow with rounded corners, since the background drawable defines the view's outline. Providing a custom outline overrides the default shape of a view's shadow.
To define a custom outline for a view in your code:
Extend the ViewOutlineProvider class.
Override the getOutline() method.
Assign the new outline provider to your view with the
View.setOutlineProvider() method.
You can create oval and rectangular outlines with rounded corners using the methods in the Outline class. The default outline provider for views obtains the outline from the view's background. To prevent a view from casting a shadow, set its outline provider to null.
Hopefully it helps.
P.S.:
yourAppNs:elevation="4dp" will be a good idea if you are using android-design-library.
ViewCompat.setElevation()/getElevation() doesnt work on pre Lollipop.
There isnt anything worth checking out at the support library, at least until version 21.1.1
As for creating an elevation effect by yourself
take a look at
this
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