3

I want to create special characters for math, for an android application.
And I wonder if it's is possible to overlay one character on top of another, or if there is some fine control on how text is rendered (and how do you go about doing that).

ilomambo
  • 8,290
  • 12
  • 57
  • 106
  • 1
    can you provide some example for this? – Pratik Sharma Dec 12 '12 at 11:47
  • @PratikSharma Yes, one example of many, I want the subscript and superscript to be aligned on the same horizontal start point. Or I want to create a special inner multiplication symbol by rendering a circle and a star together. – ilomambo Dec 12 '12 at 11:53
  • see the solution I have posted for that. Let me know if you need more help from me. – Pratik Sharma Dec 12 '12 at 12:06
  • Text of what sort? If you mean text rendered into a graphic display element, then certainly. Text rendered as the content of a text area might be more complicated, though you may be able to stack two text areas on top of each other with some clever use of transparency. – Chris Stratton Dec 19 '12 at 22:36
  • Just to clarify, are you asking 1) how to show mathematical symbols (beyond what's found on keyboard keys) in a TextView? and a second question 2) How to overlay text with precision, e.g. write say a yellow H exactly on top of a blue H? – Gunnar Karlsson Dec 19 '12 at 23:47
  • @GunnarKarlsson The answer to 1) Yes. The answer to 2) Not exactly, I want to be able to write a'+' character on top of an '0' character. Or to be able to write A with superscripts and subscipts on top of it.:::: Basically to write any character on top of any other, multiple times also, and be able to shift with pixel/points precision to the right left, top, bottom the written character. – ilomambo Dec 20 '12 at 00:18

4 Answers4

8

If you need a general method to display math equations, have a look at jqMath. I think you can render it using a WebView.

Many math symbols have Unicode code points. Specifically, the Unicode code point for the symbol of a '+' inside a circle is U+2295. They can be rendered directly if the font supports it. For a list of some math symbols with corresponding Unicode code points check this Wikipedia article.

Also have a look at this question for resources using MathML in Java.

Community
  • 1
  • 1
Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
1

I would extend the View class and then use the drawText method of the Canvas object you receive in the onDraw method. It sounds like you need fine control over coordinates of where text is being painted and extending View would give you just that. Take a look at Canvas.drawText and you can use the x and y coordinates to overlay text as you require.

Ameen
  • 2,576
  • 1
  • 14
  • 17
  • Thank you very much for the input. I was thinking of doing something like that, but Dheeraj's answer gave me a library that can render Mathtml, which is a standard and that solution is way better (easier) than drawing myself. All math I need to write is supposed have a code in Mathtml. – ilomambo Dec 26 '12 at 12:21
0

Try this:

This way you can add Superscript text :

TextView out_unit2 = (TextView) findViewById(R.id.out_unit2);
out_unit2.setText((Html.fromHtml("meter<sup><small>2</small></sup>")));

Subscript text :

TextView out_unit2 = (TextView) findViewById(R.id.out_unit2);
out_unit2.setText((Html.fromHtml("meter<sub><small>2</small></sub>")));

You can use this to add as many to your code.

Hope it helps you.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • I am currently not at my develop computer. Can you give an example of both superscript and subscript on the same text, and both aligned? Also, how does it solve the general question of overlay text over text? The examples I gave are just 2 out of a lot, I need a general solution. – ilomambo Dec 12 '12 at 12:17
  • This doesn't answer the general question, rather it provides a possible alternative for two specific cases. – Chris Stratton Dec 19 '12 at 22:35
0

It could get really messy really quickly, but you would be able to accomplish it with a FrameLayout and several TextViews inside. For example, XML for a "0" overlapped with a "+", superscript "+" and subscript "-":

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="@dimen/title_bar_font_size" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="@dimen/title_bar_font_size" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="12sp"
        android:layout_marginLeft="12sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="12sp"
        android:layout_marginLeft="12sp"
        android:layout_gravity="bottom" />
</FrameLayout>

Resulting in:

Output

Cameron
  • 3,098
  • 1
  • 22
  • 40
  • This look really complicated. I cannot imagine having to define each math synbol/glyph in XML. And moreover, I need it to be dynamic, as the user will be typing math. It is not a static string I have to display. – ilomambo Dec 22 '12 at 17:06