Displaying text in multiple lines in Android - android

I have a graphical Android project and my primary trick for providing user interface functionality is to use drawrect and drawtext to draw a rectangle with a label on the screen. Then, I capture touch events and check to see if they occur in the rectangle -- when they do, voila, I have a button.
It may not be the most elegant method, but is seems to be working like a charm. However, not all of my labels look good all on one line, and I'd like to write them on two. I suppose I could write two separate lines and manually work out arranging the text to be nicely spaced and centered, but I'd like to avoid that if possible.
In Android, is there a simple way to split a text label and write it out in a single step??
Thanks,
R.

I don't think there is a easy way to achieve this, you will have to measure the text and draw it with an offset that is ButtonWidth - TextWidth / 2.

In an Android application, you can create a Layout and populate it with a variety of widgets. One of these potential widgets is the button and if you use it, it WILL display your button label on more than one line if the label's length requires that...
However, my application is a SurfaceRunner, and this view doesn't support buttons, as far as I can tell. I do use a button-ish functionality by drawing various rectangles and checking for screen touches inside their boundaries. This works fine for my purposes, but does not, in any way, automate any of the display tasks (like the button widget does). In order to display a long label on two lines, it's necessary to do that explicitly in the code. Oh well.
Fortunately, it's only one additional line:
canvas.drawRoundRect (Button, ROUNDOVER_SIZE-1, ROUNDOVER_SIZE-1, paint);
canvas.drawText (Text1, Button.centerX() - paint.measureText (Text1)/2, Button.centerY() - 10, paint);
canvas.drawText (Text2, Button.centerX() - paint.measureText (Text2)/2, Button.centerY() + 20, paint);
Obviously, it would have been cooler if I had used something a tad more automatic than "10" and "20", but this does get the job done.

Related

Lua - Zoom buttons - Help me

So I'm working on piano app in Corona SDK (I'd like to say that I'm still a newbie to it). Basically the app layout is 'static' as I call it (there's no any scrollview etc.). Keyboard is situated on top of the screen and it covers like 1/5 of whole background, so every key is tiny, unplayable. And I don't know how to make zoom function containing buttons feature. If out there would be 2 buttons; one with '+' and one with '-', situated in bottom's 2 corners. I want them to don't change their size and position while zooming. Then when it's zoomed, no matter how deep, app work would be easily scrollable. I'd love the both, zooming and scrolling to be smooth, so you were be able to individually select your playing setup in your own preference. Can you help me and give some code suggestions, please? (as I said before I'm new..)
Thank you
First of all I suggest you to look into group programming guide. As I understand you need to place Keyboard in separate group and use xScale/yScale properties to zoom it.
--setup keyboard
keyboardGroup = display.newGroup()
sceneGroup:insert(keyboardGroup) -- sceneGroup may have different name in your code
--insert keys example
keyboardGroup:insert(yourKeyObject)
Zoom function:
function setZoom(xValue, yValue)
keyboardGroup.xScale = xValue
keyboardGroup.yScale = yValue
end
Zoom buttons should be placed in another display group, so they will not be affected by scaling

How to implement multiple highlight line sliders in line chart

I want to implement single and multiple highlight line sliders in line chart. I am able to get only single highlight line slider with MPAndroidChart (https://github.com/PhilJay/MPAndroidChart) library. But I also need two highlight line sliders to allow the user to select range of values(min and max values) on the chart. Please refer the below image for more information:
Can we achieve the above requirement with any library or we need to draw a custom view?
Any help or guidance will be well appreciated.
In terms of shinobicharts, while the Crosshair could be easily customised to act like one of sliders, in order to have the two sliders you'd probably want to make use of the Annotations feature.
You can create Annotations with custom Views and add them at any X, Y point (in data terms). You can also convert between data values and pixel values via the Axes. It is therefore possible to make the Annotation follow the user's finger as they drag across the screen.
The trickiest part would be placing the circle on the LineSeries at the right Y-value. As I mentioned above you can convert pixel values (e.g. from a user gesture) to data values so you could easily get the X value but the API would leave quite a bit of work up to you in order to get the correct Y value.
This kind of feature is something that is requested quite often and is certainly something we are looking at providing in the future but for now, while possible, it isn't available out-of-the-box.
Disclaimer: I work for shinobicontrols

Android dynamic text drawing / touch detection

I want to display text (relatively large) and determine which character the user touches. I'd also like to embellish the text by drawing a circle (for example) over certain characters. How do I know which characters are at what location? Enforcing monospaced font would be fine if helpful programmatically.
Would it also be able to make this one line of text scroll left/right (using touch->slide) and still have touch work to determine the correct character?
A very broad question, and you've got a lot of work ahead, but here's the outline of one possible solution:
Create a custom view. ImageView is often a good base class to
inherit.
Override the onDraw() method of the view.
Use canvas.drawText() to place your text on screen.
Use paint.getTextBounds to meausure your text. See the discussion
linked below for a thorough understanding. Since you drew the text
on screen, and you measured it, you know exactly where each
character is.
Override the onTouch() method in your custom view and track the
events you need. There are many QAs here in SO on how to do this.
For scrolling the text, use canvas.translate() using the movement
you calculate in the onTouch(). Since you know how much you
scrolled (translated), you know how far your characters have moved
and can offset your touch detection to detect character touches.
Finally, since you now control all of the drawing and, your
character positions are abstract, only having meaning when compared
to a touch position, you can embellish them in any way choose.
Android Paint: .measureText() vs .getTextBounds()
Phew!
Good luck.

Android Custom EditText

I am looking to create a custom EditText, where each character entered should lie within its own cell (see image).
My best guess is that I need to create my own .png's for the various states of the EditText, which provide the rectangular outline, then extend EditText's onDraw method to draw the vertical lines that separate adjacent characters.
I've never made a custom view before, and I know little about manual drawing in Android, so some guidance is needed.
Am I on the right path here?
How can I determine how tall and at what location to draw the vertical lines?
What is the best way to eat an oreo?
This is gonna be a very difficult task. Just look at TextView.onDraw() (which you are thinking of override). If I were you, I'd immediately change my mind :)
Instead, I'd use a LinearLayout to hold an array of customized EditText, but I don't know what kind of interaction you are looking for
Finally, to measure text you use Paint.getTextBounds(). Where to draw vertical separators depends on your design. If you have a fixed number of fixed length cells, you know where, otherwise you need to measure text

How to produce these views? How Android screens go beyond the simple controls that have Android?

How Android screens go beyond the simple controls that have Android, look at this for example.
How come so that each key responds independently? Is it a single image, several or a drawing?
How to put each circle in the specific country? What if I want a country to be selected?
You can make your own controls by subclassing a view and overriding it's methods. The onDraw method gives you a canvas where you can place just about anything. Check the official android API or this post to get your started.
For the first image, my guess is that it is one image and the position of each click is translated to the corresponding key on the piano. The second image might also be a single image with separate overlays for each country and circle to set the desired color and place the numbers (that's what I would probably do, but it can be done differently).
If you require more flexibility than fixed images, take a look at creating 2D graphics.

Categories

Resources