Drawing chart in a dialog - android

I have a conceptual question to ask:
I created a custom dialog (extends Dialog) and I want to draw a chart (dynamic data, not static) in the top third of the dialog.
What's the best (only?) way to approach this?
A) Get a canvas to the dialog and draw to it? Seems like I need access to the dialog's draw, yes, or can I do this outside of the draw?
B) Subclass a view within the dialog layout (e.g. LinearLayout) and override it's draw and draw the chart?
C) Other? I've read that one approach would be to draw to a bitmap and then blt (or equivalent) to the canvas. This sounds closer to what I want to do, as once I create the chart, I have no need to alter it (no direct user interaction).
I haven't yet found any good sample code that deals with custom drawing in a dialog, so if I'm missing something, an example would be great.
Thanks much,
Rich

Solved.
My solution was a hybrid of B/C above. Since I needed access to the view's draw() method, I created my own subclass of an ImageView (e.g., MyView).
From within the draw(), I can get the dynamic size of the ImageView as it appears in the dialog. Given the size, I can now perform draws scaled to the custom ImageView size within the dialog.
I had to remember to use the proper custom view XML syntax in the dialog layout (e.g. "com.avaliant.dialogtest.MyView" to replace "ImageView"). And, of course, in my dialog class, I had to set to view to the proper view class:
MyView test = (MyView)dialogView.findViewById(R.id.test);
Quite easy once I understood WHY I had to do what I had to do :).
Rich

Related

Drawing a tournament bracket in android?

I have an app that will take a list of players and set them up so that you can go through and create a bracket dynamically. The number of players will change and the names as well.
Instead of showing a whole bracket, my point will be to go in columns. I.e. instead of the left side of the arrow, it show the right side:
My questions is how do I best draw this?
My initial thought is to do multiple layouts where I hard code textviews with custom drawables and lines for each column of 16/8/4/2 with just one border, but then I have to constantly change the layout setup.
My second one would be to dynamically draw it on a canvas based on this answer here, but it didn't give a lot of ideas of implementations.
My Questions: If I choose method 1, will I be able to dynamically change the layout of my activity?
If I choose method 2, how could I go about this? I'm still pretty new to android.
As for question 1, yes you can absolutely change the layout dynamically. You can create several layout files and do one of two things: The first, is you can use your activity's setContentView(int) method to completely reset the activity's layout to a new view. The second would be to identify the parent view of the bracket portion, cast it to a ViewGroup and use addView(View) and removeView(View).
As for question 2, you could go about using a canvas, but it would be quite difficult to use only a canvas, but possibly a combination of a canvas and a set of text views could work. While this isn't directly answering your question, this tutorial is one I have used in the past, and gives you an excellent sense of how to work with the Canvas class in android.

How many views are required as I have a view showing drawing required, do I still need surfaceview

I have already created a circle with the use of a View and have not used SurfaceView at all. I want to create buttons which when clicked on show images from the drawables. But I have read on the net that a SurfaceView is required to allow UI elements to be placed on top. Is this true, can someone please help me, as I am confused on this.
Thanks.
It's not very clear what you want to do, but if you want to place UI elements on top of each other without using SurfaceView you can you a RelativeLayout, this layout allows you to have views on top of each other, do you can have an ImageView with a drawable appearing over a button for example.
If you just want to change the background/src images of a button when clicked (for example to create a 3d effect of clicking), you can check out selectors, these allows you to specify different drawables for pressed/normal states.
If you want to create buttons on a SurfaceView, I suggest you render Bitmaps that will represent buttons. You will have to programmatically check if the touch coordinates are in the bounds of that bitmap tough, to register a button click.
I hope this helps.

Using a FrameLayout as a Live Wallpaper

I'm trying to use a FrameLayout as the content of a Live Wallpaper. According to this answer by Romain Guy, it should be possible, but I didn't have much luck calling .measure() and .layout() on the view. Could anyone give me a brief example of how to use a regular layout inside a Live Wallpaper?
It's a wallpaper that you can mess around with, right? If you are going above the usual views, you may have to implement some custom logic, especially if you are drawing images. A FrameLayout has background and foreground drawable functions. You can use XML or Java to add regular views into a FrameLayout
You also can create a custom Drawable object to do a lot of fun things with regards to custom painting. The Drawable object simply gets sent the view's canvas.
If you want to measure views, you are going to have to do it after the view runs through its measure and layout codes. This means doing this in onCreate() returns 0. Lots of people have had better luck doing it in a callback like onWindowFocusedChanged

Transparent view overlay for drawing

I have app consisting of RelativeLayout, TableLayout and some buttons. Now I would like to draw a circle under point where user pressed on screen. One way to go is to extend View and add all the functionality inside, but I am wondering if there is any more elegant (general) solution?
I was thinking in way of transparent overlay, which you add to layout and it captures MotionEvent, draw circle on point of touch and passes event to underlying View component (so buttons are still clickable).
That way solution would be more reusable, as you can easily plug it in any project, but unfortunately I have no idea if it is doable that way. Any hints?
I'm pretty sure you can achieve what you want using GestureOverlayView.
Resources and examples are available:
http://developer.android.com/reference/android/gesture/GestureOverlayView.html
http://developer.android.com/resources/articles/gestures.html
http://www.vogella.de/articles/AndroidGestures/article.html#example
I dont know if my suggestion is the best solution,but I would do the following:
Create a new class that extends Drawable.
Override the onDraw method and in there draw your circles on the canvas.
Add a. addCircle(int x, int y); method to the drawable class which you will call each time you want to add a circle.
Then in you activity create a new objectof the above mentioned class and set it with context.getWindow().setBackgroundDrawable(your object) as your WindowBackground. Then where you handle your motionevent call this objects .addCircle method and your circle will be added.
Like this in your activity will be only several lines of code and it will be reusable.

android custom view how to draw button on top of the view

I am creating a android app using LunarLander as a example.
Now I need to create a few buttons which are drawn over the view.
I do not want them as a seperate layout above or below the view but in the custom view.
Is this possible or am I going to have to programmatically show the button images then detect the touch. The buttons I create using new never show on the app. I assume this is because I have overwritten the onDraw and the buttons are never drawn even though I call
super.onDraw(canvas);
I think you could use FrameLayout to show two layers - first would be your surface from lunar, and second is the layout with buttons etc. You could define everything in layout.xml file. Probably that is enough.
Regards!
If your view extends Linear/Relative/TableLayout, you can use view.bringChildToFront(child).
Use AbsoluteLayout & then in your ui thread set button.bringTioFront() & you are done!
watch this post Problem when using more elements in the ListView

Categories

Resources