How to show webview data in a vertical center

I want the following formula to display the vertical center in the layout. It is horizontally centered. But it is displayed at the top, not in the center.

This is where I want to reach enter image description here

But here's how it is shown above: enter image description here

Here is my XML code:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/title"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#EFEFEF"
   android:paddingBottom="0dp"
   android:paddingLeft="0dp"
   android:paddingRight="0dp"
   android:paddingTop="0dp" >

<LinearLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ffd0e3e5"
    android:gravity="center"
    android:paddingBottom="6dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#ff33203d"
        android:textSize="18sp" />
</LinearLayout>

<WebView
    android:id="@+id/txtQuestion"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/topLayout"
    android:layout_weight="1" >




</WebView>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtQuestion"
    android:paddingLeft="16dp"
    android:paddingRight="14dp" >

    <LinearLayout
        android:id="@+id/listViewQuestion"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
   </ScrollView> 


 </RelativeLayout>

      

and my Java code:

 public class MainActivity extends Activity implements View.OnClickListener{

 private WebView webViewEquationDisplay,txtQuestion;
 private String mathML;

    private String doubleEscapeTeX(String s) {
    String t="";
    for (int i=0; i < s.length(); i++) {
        if (s.charAt(i) == '\'') t += '\\';
        if (s.charAt(i) != '\n') t += s.charAt(i);
        if (s.charAt(i) == '\\') t += "\\";
    }
    return t;
}

    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.examination_fragment_layout);
    txtQuestion = (WebView) findViewById(R.id.txtQuestion);
    txtQuestion.getSettings().setJavaScriptEnabled(true);

    try {
        InputStream raw = getApplicationContext().getAssets().open(
                "multipale_choose.xml");
        QuizzesParser parser = new QuizzesParser(this);
        parser.startParsing(raw);
        answerList=parser.getpList();
        initiateWebView(answerList.get(1).getAns_body(),txtQuestion);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 private void initiateWebView( String formulaML,WebView formulaWebview){

    mathML="<p style='  height: 1000px;vertical-align: middle;text-align:center;'>";
    mathML=mathML+formulaML;

    mathML=mathML+"</p>";   

    webViewEquationDisplay=formulaWebview;
    webViewEquationDisplay.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

                 webViewEquationDisplay.evaluateJavascript("javascript:document.getElementById('math').innerHTML='" +
                     doubleEscapeTeX(mathML)
                                  +"';",null);


            }
            else{
                webViewEquationDisplay.loadUrl("javascript:document.getElementById('math').innerHTML='" +
                        doubleEscapeTeX(mathML)+"';");
                        /*
                        "<font color=\"black\">`"+"<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
        +"<mstyle displaystyle=\"true\">"
                  +doubleEscapeTeX(e.getText().toString())
                  +"</mstyle></math>';"+"`</font>';");
                  */
            }

            webViewEquationDisplay.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
        }
    });

    final String mathJaxOfflineUrl = "file:///android_asset/MathJax/MathJax.js";            
    webViewEquationDisplay.loadDataWithBaseURL("http://bar/", "<script type='text/x-mathjax-config'>"
            +"MathJax.Hub.Config({ " 
                +"showMathMenu: false, "
                +"jax: ['input/MathML','output/HTML-CSS'], " 

                         +"extensions: ['mml2jax.js'], " 
                          +"TeX: { extensions: ['noErrors.js','noUndefined.js'] }, "

              +"});</script>"
            +"<script type='text/javascript' "
              +"src='"+mathJaxOfflineUrl+"'"
              +"></script><span id='math'></span>","text/html","utf-8","");

  }

 }

      

I follow this example.

0


source to share


1 answer


You are using the style line-height

:



mathML = "<p style='line-height:400px; vertical-align: middle; text-align: center;'>";

      

+2


source







All Articles