Draw custom View on a specific x\y coordinates - android

I would like to create a custom View (that will be inflated from XML) which will be drawn at given x\y coordinates.
I know I can create a custom View which will implement onDraw(Canvas canvas), but I want this View to be inflated from XML.
On Canvas I can only draw lines, rectangles and such.. But I want to inflate a whole XML layout..
How can I do that??

You can use a FrameLayout as a main layout.
Inflate the layout you want.
Add it to the FrameLayout.
Then add padding / margin with LayoutParams.

I've ended up using RelativeLayout as mentioned here:
Set the absolute position of a view
Thanks for the answers!

Related

The better way of dealing with RelativeLayout

I would like to know which is more profficient way of placing children in RelativeLayout. There are two approaches of doing this:
1) Place the main view with absolute position (like layout_centerInParent or set margins/paddings correspondent to parent view) After that you add other views and set them attributes like android:layout_above="#id/relative_view_id" and place them below relative view. It is not good way because your views hierarchy in xml does not match to what you see in preview.
2) You assign to children of RelativeLayout attributes with absolute id android:layout_above="#+id/relative_view_id" (+ appeared). It provides the correct views order in xml. BUT when you looking for declaration of view with relative_view_id from java code (by pressing cmd+B) Android Studio suggests all the views where you declared #+id. In our case View with attribute android:layout_above="#+id/relative_view_id" will also appear in search results.
What is your way of placing Views in RelativeLayout?
android:layout_above
Positions the bottom edge of this view above the given anchor view ID.
Accommodates bottom margin of this view and top margin of anchor view.
For your question I would prefer No 1 way .
android:layout_above="#id/relative_view_id"
Its refer the already generated id (relative_view_id) .

Add views in a circle (Android)

I would like to add some views in a layout.
How can we put these views in relative or linear layout or any other layout in a circle same as in this image. I would like to do it using code.

Add draggable shape on a picture in Android

Can anyone explain how can I add shape (for example a rectangle or an arrow) on to a image (in ImageView)? Once added, the shape will need to be draggable to anywhere in the image. And finally need to save the image edited (with the shape on top of that).
What informations do you really need?
This can be some steps to do:
Maybe create a new object to handle this:
Create a new class and extend from ImageView or use LayerDrawable for that.
Write setters/getters for the main-background-image/bitmap.
Add your own Vector shape (create a new class with definable color,thickness... or hard-code it)
Do all of your drawings in onDraw-Methods
Implement onTouch interfaces to handle your selection and dragging.
Create new method (render to jpeg/png/..) to save your resulting image
Create a Custom view from View that completely overlaps your ImageView. This view will have a transparent background and contain the draggging view. Initially set visibility to invisible.
In Custom view class override the onTouch event where you update the dragging views margins.
On save, get drawing cache of the parent layout(that contains both the ImageView and Custom view) and use as needed.
Sample xml code:
Relative layout parent whose drawing cache has to be grabbed using rel.getDrawingCache()
<RelativeLayout .......>
<ImageView ......./>
<CustomView ......./>
</RelativeLayout>

Grid-lines on a GridView

How do I define grid-lines for a GridView?
Is there an attribute or do I have to draw my own background with them in it?
There is no such attribute. The easiest thing is to create a subclass of GridView and override dispatchDraw() or onDraw() to do it yourself.
set the background to the view(inflate layout) inside the GridView so that gridview display the line automatically.
You can just set a padding for each cell of the grid view so that that looks like the grid line. Each cell say would be made up of a linear layout.
Another way of doing this would be to have the cell as a linearlayout and have a view inside that with width of say 0.5dp and height as mp and vice-versa for the horizontal line.

Adding text view to surface view

i use this a s surface view in my application and now i want to add text view to this programmatically. How can i do that.
"<com.csfcse.udrawer.UdrawerDrawingSurface
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/drawingSurface"
android:layout_gravity="left" />"
Thanks......
An option may be to have a FrameLayout with two children, first the SurfaceView, then whatever you want to overlay on the SurfaceView. In the sample below, the second View in the FrameLayout is a horozontil LinearLayout with a Button and a TextView. The FrameLayout displays all its children as piled in Z order with the first child at the bottom and all children positioned at the upper left corner. In this case the LinearLayout has Gravity.CENTER_VERTICAL (I think you could do the same thing with padding on the LinearLayout.
I have never found much (good) documentation about how SurfaceViews (or any of the screen drawing) works. There may be a problem with flickering or refresh, but I don't see much problem on my Froyo Evo. (This test app draws 'twirling' lines on the SurfaceView below the Button and TextView.
If the question is simply: How do you programmatically add a TextView to a Layout that was created by a XML inflated Layout, then, get a reference to the Layout instance and call addView() to it.
Layout lay = parentLayo.findViewById(R.id.drawingSurfaceParent);
TextView tv = new TextView(this);
tv.setText("New textview");
lay.addView(tv);
You'll need to use the Canvas and a corresponding text draw method:
drawText(String text, int index, int count, float x, float y, Paint paint).
This is not possible, a SurfaceView cannot contain other Views.

Categories

Resources