Android Java how to make single bar graphic - java

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

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).

Zoomable RoundImageView - RoundImageView is still rectangle

I am trying to make zoomable RoundImageView. I am using this TouchImageView
but I extend it with RoundedImageView not with standard ImageView. This TouchImageView is in some FrameLayouts which have these these backround:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF"/>
This background also have TouchImageView. When I call on it function setOval(true) everything seems fine, until I start zooming. It's look like it only create rounded bitmap, but ImageView is still rectangle...
PS: I need to have oval not circle.
There are some images:
Before zooming:
After zooming:
Thank you for every answer! ;)
For thoose who have the same issue. I find MaskableFrameLayout which is pretty easy to use. It extends FrameLayout so it's possible to have zoomable view in it. Just create oval or your shape in drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF"/></shape>
and use setMask(drawable). Also you need to setPorterDuffXferMode(...). I used DST_IN.
Hope it helps!

Best way generate small solid circle on a blank screen in a specific position?

--> a number of circles will be drawn over a period of time but only one will be shown on screen at a time.
--> user will click on the circle when the program registers the click, it will show the next circle.
--> the position of each circle is known, only they will appear randomly at different execution.
I have considered a linear layout filled with a lot of imageview(30 of them), every imageview has the same source, a small red dot, generated and stored in the res/drawable folder. code for the dot is:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Large red circle -->
<item>
<shape
android:shape="oval">
<stroke
android:width="0dp"
android:color="#000000" />
<solid android:color="#FF0000" />
<size
android:width="30dp"
android:height="30dp"/>
</shape>
</item>
I am thinking about using isVisible() on these imageviews to hide and show them at different times. this doesn't seem very efficient to me. any suggestions?
The best way to achieve that is to create a single custom View with onDraw overridden.
Then, inside onDraw you can simply draw as many circles as you want on the Canvas object. There are thousands of threads here on StackOverflow describing how to do that. E.g. this one.
Remember that you will have to call setWillNotDraw(false) on your View to cause your custom onDraw to be executed.
When the user clicks on the circle, simply record that even and call invalidate(). onDraw will be called once again and you can draw proper number of circles for the updated state.

Changing XML color from java code

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

Categories

Resources