Im writing an app. I create my own class that extends View, then setContentView(new MyView());. Sometimes I want to draw popup with options. Now I draw it with shapes. I was wonder is there a way to make another View or View-like-class and use it as popup?
Another question: Can I use EditText in my own View, also without XML?
Yes, you can use addView method and put new EditText, but be sure about layout params.
Related
I am new to android and trying to make something like the fingerlock security system of the android. The problem is that I have found a lot of reference on drawing onto a plain background (that sets different content view and allows drawing onto that), but not regarding drawing onto the same activity layout.
To draw manually on a View, you need to override its onDraw method. For this you need a custom View (often extending from an existing View type).
If you are using an xml to set the Activity's layout in setContentView() then you need to add the above custom view to this xml. Then, you can draw onto the same activity layout.
setContentView is noting but the activity view. once the view is set in main activity (max in oncreate method) it can update from multiple classes according to your logics.. i think before trying this you have to understand the view and its hierarchic properties properly
In my application I need to have something like image below. I have experience of creating custom components and I know how to do it. However, I have no idea how to create this kind of component. Do I need to do something like when we are designing custom rating bar?
Anyway, their so many way you can achieve this.
One way i am seeing using radio group and radio button, vertical lines and text view you can easily achieve this. Put everything inside a layout of your choice and then just inflate the layout where ever you need. After that you just need to set the radio button like this radio1.setChecked(true).This is a simple solution but may not a be a optimal one.
Alternatively you can create a custom view by extending view class or any of its subclass, whatever you feel suitable for your work then override its onDraw method do the entire thing manually.
Is it possible to add a button to a class that extends View? ""public class josh extends View "" If not how can I merge a button onto the view when it is referenced in my main activity? uhg please help....
Two ways:
Have both the button and your custom view as children of a FrameLayout, by which the button can be drawn over your custom view.
Don't have a real button. Instead, draw something that looks like a button, treat touch events specially if they're within its dimensions, etc. This is more appropriate for something like a game.
I believe you can do #2 but have a subclass of Button do the drawing, so that it performs exactly like a normal button and is just positioned/sized by your custom view, but I don't know how to do this.
If are wanting to add a button to a view at runtime check this preivoius question... How to add a button control to an android xml view at runtime?
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
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