I am looking for a way to make a progress bar fill effect on Android.
I have a FrameLayout called progressBar and inside this FrameLayout is a LinearLayout called filledBar. I don't know the literal widths of either progressBar or filledBar, but I would like filledBar to take up a certain amount of progressBar. How would I do this? (I am creating both bars programatically)
quite simple, do a XML layout like this:
FrameLayout
View // background set to be a custom drawable
TextView // gravity right
in code you inflate it and set the drawable:
private CustomDrawable drawable;
drawable = new CustomDrawable(context);
View v = layout.findViewById(...
v.setBackground(drawable);
then to update the percentage
drawable.setLevel(10); // for 10% for example
when you create your custom drawble inside the draw method it's a simple call to drawRect
{
canvas.drawRect(0, 0, getLevel()/100*canvas.getWidth(), canvas.getHeight, paint);
}
the paint you create with the color you want it to be.
That's a very rough idea, but I hope it's enough to guide you to the correct direction.
Related
I'm new to android, and I am currently working on a simple connect three game, I have nine ImageViews which contain transparent images, when the ImageView is clicked, the resource is changed to x or o.
I've tried adding more animation, and setting resource to null rather than a transparent image, but it didn't work, only restarting the activity seems to fix it.
public void oneClick (View view)
{
//image view onClick
ImageView one = (ImageView) findViewById(R.id.one);
one.setImageResource(R.drawable.x); //setting x or y
one.animate().rotation(180).setDuration(500); //animation
}
Here's how I reset the images,
one.setImageResource(R.drawable.transp)
After this, if the onClick activity is called again, the image is set.
However the animation doesn't seem to work.
What am I doing wrong?
My image was rotated 180, so it wasn't rotating again, the simple solution was to reset the orentation of the image then run the rotate animation again, which seems to have fixed it!
clicked.setImageResource(R.drawable.x);
clicked.animate().rotation(180).setDuration(500);
after this
clicked.animate().rotation(0);
now if I use the rotation animation again, it seems to work!
This is a simple fix. Simply add .start() to your animation. It should look like this afterward:
one.animate().rotation(180).setDuration(500).start();
Is there a way to define a clickable area in an image? Something equivalent or similar to map-area tags in HTML? Or is there a standard way of doing this sort of thing?
A possible method
1) get the width, and height of the image view using (in pixels)
2) get the coordinates of the image view (which is the top left corner of the image view)
3) create a touch listener, and record the coordinates of the users touch
4) if the user's touch coordinates fall on the image do whatever you want to
If you only want to do certain things based on where the user touches you will have to calculate the coordinates of those regions using the coordinates of the image view, and the image views width and height.
I think there might be no standard way for this, but you can achieve it by placing a transparent VIEW over your imageview. Set click listener to that view and make imageview clickable false.
When i have to do something similar, i use an include view.
As for the layout the include view will be accessing, i would use FrameLayout as root.
Its children would be the ImageView that i want to depict, along with a LinearLayout infront of it containing Buttons at the positions i want the user to choose from.
Then in code, i set listeners to these buttons.
Lastly, i would set the buttons so that there background is transparent and text is not available.
For example in a project i am working on, i have an ImageView with an Image of Ocean as background.
In front of the view i have a LinearLayout with smaller ImageViews with images of islands.
I place listener on the islands to see when they are clicked to open a new activity.
I have a listview and need to create listitem that has labels (star and 15 min) that slightly stand out of list item- looks like they are bigger.
What kind of background must i make for listitem layout?
Make a background that has a few pixels of transparency at the top with a 9patch image, no need for a XML shape (unless you are drawing the background there), just use a PNG file and you're good to go.
just create a custom listview Adapter for your listview
Create a Background Shape xml which is transparent about 8dp, then use that xml as your listview background
Actually, the subject is a question.
I want to draw on top of the map a view as overlay, simply my view consist of linear layout with 9patch background and two textview inside. I need to draw those layouts in runtime and they will be linked to geo location
There is no problems to define such view, but it is problematic to create an overlay that will handle such draw...
Yes - this is what a FrameLayout is for! Children in a FrameLayout appear on top of each other, with the last item displayed on top and the first item displayed on the bottom.
Put your MapView as the first child of a FrameLayout and your LinearLayout as the second.
Actually there is no way to do what I want, I've found a way to convert any layout to bitmap, but firstly you need to invoke layout on the root view and define it boundaries, so in my case it is the same to simple drawing on canvas which I've used for my solution. Converting layouts to bitmap is a good thing when your layout is already drawen on the screen, but when you need to draw layout on the canvas from scratch there is no benefits due to simple drawing on canvas.
You could create a drawable out of your view by converting it to a bitmap.
Or you can try to use this. Has the same effect: https://github.com/jgilfelt/android-mapviewballoons#readme
And you can modify it like you want to show whatever view you like
Is there a possibility to add a view or derivative (button, textview, imageview etc...) to another view without having to go through layouts?
I have a class called ScreenView, it derives from View (e.g. public class ScreenView extends View).
I wanted to display text into it, labels, images etc..
On an iPhone it's trivial, simply create my objects and use addSubview method ; you can embed any UI objects into another. Most UI toolkits work that way too.
Problem with layouts is that I haven't found an easy way to properly position a subview (say a text view) in a given location of the enclosing View. I want one to contain the other...
I was hoping I had missed something in the documentation (which is far from being intuitive that's for sure) and I could do something like myView.addView(subview). But it ain't so :)
As a side question, what is the closest to iPhone's UILabel in the Android world?
A UILabel can contain a text or a graphic. Right now I've been using TextView and ImageView but they aren't anywhere as flexible when it comes to layout as a UILabel
Edit: Making my View inherit from ViewGoup instead, let me add subview..
However, I want to draw in my view. I found that when ScreenView extends ViewGroup, the onDraw function is never called then.
Something like:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// make the entire canvas yellow
paint.setColor(Color.YELLOW);
canvas.drawPaint(paint);
}
Would paint in yellow my view if ScreenView extends View ; but not if it extends ViewGroup.
How do you draw directly in the view then?
Thanks
JY
Use a ViewGroup, that's what they are for. And then you can call addView() :)
After making my class inherit from RelativeLayout instead, it does draw my subviews.
GroupView doesn't draw anything by default, and onLayout needs to be fully written. With no documentation on this matter it's going to be a hard thing to do.
onDraw isn't called by default, it does get called if you set the background colour or add any drawable item.
Otherwise you need to draw during dispatchDraw instead.