Skip to content

javafa/AndroidMathView

Repository files navigation

AndroidMathView

  • This Project's cloned from https://github.com/gregcockroft/AndroidMath
  • [17/DEC/2020] Added custom attributes for xml
  • [18/DEC/2021] Added methods
  • [07/JAN/2021] Added autoSize : If you want to use this attribute then set the layout_width. ex) layout_width="300dp"
  • [27/JAN/2021] Applied android jetpack
  • [29/JAN/2021] Fixed ndk errors on some cpu architectures on the release apk

Installation from the command line

git clone https://github.com/javafa/AndroidMathView.git

cd AndroidMathView

./gradlew installDebug

Installation for Android Studio

Clone this project, run CDep. CDep pulls in the freetype dependency.

git clone https://github.com/javafa/AndroidMathView.git

cd AndroidMathView/mathdisplaylib

./cdep 

Open the project in Android Studio

Using library in your app

This is using jitpack.io Add below lines to root's build.gradle

...

allprojects {
    repositories {
        ...

        maven { url 'https://jitpack.io' }
    }
}

Add below lines to apps's build.gradle

dependencies {
    ...
    
    implementation 'com.github.javafa:AndroidMathView:1.0.21'
}
	
<ConstraintLayout ...>

    <com.taeim.mathdisplay.AndroidMathView
        android:id="@+id/mathView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fontSize="20sp"
        app:fontType="LatinModernMath"
        app:latex="\log_b(x) = \frac{\log_a(x)}{\log_a(b)}"
        app:fontColor="@color/colorAccent"
        app:textAlignment="center" />

    <com.taeim.mathdisplay.AndroidMathView
        android:id="@+id/mathView2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        app:autoSize="true"
        app:fontSize="50sp"
        app:fontType="TeXGyreTermes"
        app:textAlignment="center" />

    <com.taeim.mathdisplay.AndroidMathView
        android:id="@+id/mathView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fontSize="10sp"
        app:fontType="XITSMath"
        app:textAlignment="center"
        />

...

</ConstraintLayout>

Attributes

Name Type Example
latex String \log_b(x) = \frac{\log_a(x)}{\log_a(b)}
fontColor Color @color/colorId
fontSize Dimmension 11sp
fontType Enum LatinModernMath, TeXGyreTermes, XITSMath
textAlignment Enum left, center, right
autoSize Boolean false
class MainActivity:AppCompatActivity() {
    
    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } 

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.mathView2.latex = "{n \\brace k} = \\frac{1}{k!}\\sum_{j=0}^k (-1)^{k-j}\\binom{k}{j}(k-j)^n"
        binding.mathView2.setColorResource(R.color.colorPrimary)
        binding.mathView2.setFontSize(33.5f)

        binding.mathView3.latex = "\\int_{-\\infty}^\\infty \\! e^{-x^2} dx = \\sqrt{\\pi}"
        binding.mathView3.setColorString("#5312f3")
    }

    ...	
}

Api

Name Parameter Return Example
setColorResource(@ColorRes colorId:Int) Int void mathView2.setColorResource(R.color.colorPrimary)
setColorString(color:String) String void mathView3.setColorString("#5312f3")
setFontSize(dp:Float) Float void mathView2.setFontSize(33.5f)

Credits:

  • iosMath This project is a Kotlin port for Android of the iosMath project
  • Freetype is used for rendering glyphs and font metrics.
  • Freetype jni was copied as a starting point to access the native freetype library.

Related Projects

For people looking for things beyond just rendering math, there are two related projects:

  • MathEditor: A WYSIWYG editor for math equations on iOS.
  • MathSolver: A library for solving math equations.

License

AndroidMathView is available under the MIT license. See the LICENSE file for more info.

Fonts

This distribution contains the following fonts. These fonts are licensed as follows: