How to have a circular TextView - android

I have been trying to make a circular TextView. Its a circle in which I want to accomodate whole space above a circular bubble as shown in image below.
Kindly see attached image.
In this image, we have a circular bubble with circular text in it.
I have already tried setting oval shape .xml as background of TextView but still no luck.
Edit:
As text length increase. It must reduces in size to fit inside the circle. This is the hardest part to think about.

You need to create a custom view, extending from TextView probably, setting the circle as background image, and calculate the text width / break the lines manually according to the width of the text.
To calculate the width of a string, see How to calculate string font width in pixels?
Some math and calculations is required of course to measure the available space per line; but I think that's the only way, as there's no standard component out there to do it.
To place the text onto the view, use drawText of the Canvas class.

Related

how to scale image between 2 views with given coordinates in android

In my application I have a requirement to animate an image (this image view has an arrow set as source).
I am unable to figure this out how I can achieve this.To solve this i got x and y coordinates of second view that is rectangle after getting coordintes i am setting
scaleX() of image view that is purple line but I am not geeting desire out put because it stretches to the whole screen along x-axis
here is the code what i tried is
int x = (int) imageView.getX();
imageView2.setScaleX(x);
here imageview is rectangular box and imageview 2 is the purple line
Why you dont try with scenes? It's exactly what you need.
http://developer.android.com/training/transitions/scenes.html
I think you are getting the correct output when using the above code. Scale is defined against initial dimensions. And you would need to use something like this
dist = box.getX() - circle.getX()
and your scale would be
imageView2.setScale(dist/distInitial)
where distInitial you compute it at creation time using the dist formula.
You might need to change the position because it is scaled around the center of it (i.e. a smaller scale shrink margins to center of image)
You might want to perform this operations using a Canvas. It might be more efficient.

Find radius of background circle drawable

I want to have 3 TextViews that are surrounded with circles as below:
The text views are centered in the circles.
I was planning on using TextView's with backgrounds set to a drawable containing a circle.
However, to position the circle properly I need the radius of each circle.
I tried to use Drawable.GetBounds() and then find the hypotenuse of the boundary rectangle, but this doesn't yield the values I want.
How do I find the radius. Or is there a better way to go about this in general?
Am I stuck just using a custom View and overriding onDraw?
This was a really dumb question. The radius is set to whatever the width or height of the View is.

Place an imageview on customdialog screen depending on variable

I'm trying to place a small image on the top of a big one in a custom dialog.
I want the small one to be placed in a different place depending on a variable, which I called "pos". It contains a int value from 0 to 100, meaning percentage of the dialog width.
If pos = 0, I want to place the image in the left margin of the big one, if pos = 30 it should be placed in the 30% of the screen, starting from the left.
If pos = 50 I want it to able placed right in the middle, being 100 the maximum value for it and placing it in the right margin.
I include a draft to explain myself a bit better.
I tried with RelativeLayout.LayoutParams but I never get expected output.
Perhaps a simpler strategy is to extend ImageView, then create a custom image using Canvas commands in the onDraw method that draw the bitmap appropriately on the x axis with a background set to match your layout.
You can determine the width of the custom dialog in the the onMeasure method of your custom ImageView, so the returned bitmap (including empty background) can be the correct width, and the position of the smaller bitmap can be correctly apportioned.
Once you've created the class, which will take just a few minutes, you can add it to your XML layout with something like:
<com.domain.yourapp.YourCustomerImageView
android:id="#+id/bitmap_graph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"/>

How to align the text with its compound drawables in TextView?

I can't figure out how to set the alignment of text and its compound drawable (smaller than text) in a TextView. It seems the default alignment is center, but I need to align them by the edge.
PS: The android:gravity works great if compound drawable is larger than text, but not if smaller.
It sounds like you're using an image with fixed dimensions for android:drawableLeft.
Try using a nine-patch or defining a drawable through an XML resource file. In this way your drawable's dimensions will stretch to align in the TextView the way that you want it to.
I had a similar problem in that I wanted to use setCompoundDrawables to set a top drawable for a floating hint, however TextView (EditText) sets the top drawable to center, irrespective of what its width is.
I am using a custom Drawable, and inside its draw method, I am grabbing the clipBounds, and translating on its top,left to get back to the TextView's 0,0 position.
The other way to do it easily is to getMatrix and mapPoints from 0,0 then take the negative of them and translate back to 0,0 that way.

How to find the text size for a given rectangle?

I have a doubt. How to find the max text size to a text for a given rectangle. Consider the rectangle width and height is 100,50 respectively. how to find a text size which almost fits the rectangle.
Thanks in advance.
Can't you just add a Grid to the rectangle. and Add a TextBlock in the Grid so the any text within the TextBlock would size with the rectangle.
<Rectangle>
<Grid><TextBlock></TextBlock></Grid>
</Rectangle>
If you create an android.text.Layout.StaticLayout specifying the font and width, and check the height. You can use this to do a binary search for the text size that best fits your rectangle.
You might want to also check that no individual line has too much padding on one end (think right-to-left language support if you add this check).

Categories

Resources