I am new to java and android development. I am working on an application that contains a simple graphical interface for accessing and reading fundamental C and Java programs for students.
I am trying to populate webview with a text file (in this case, a C program) that is preloaded into a specific column of my database.
I decided not to use scrollview because scrolling and scaling at the same time is very difficult to implement. Scaling requires creating custom implementations that are hard for me to understand.
Thus, I decided to use webview, as it scrolls easily and requires only 1 line of code to implement text scaling.
I have 2 problems:
1) webview cannot handle new line using my current implementation. ie tries to display all text on the same line, rather than skip to the next line
2) it does not display "stdio.h", "stdlib.h", etc .... basically all words with the extension .h, which is part of almost all programs in C. It is important to display this.
I tried the following 2 implementations (maybe wrong):
1) loadData strong> (String data, String mimeType, String encoding)
2) loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
My code is as follows:
package com.example.uopengineering; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.webkit.WebView; public class viewProgramActivity<x> extends Activity { private SQLiteDatabase database; ExternalDbOpenHelper dbOpenHelper; private WebView webView; private static final String DB_NAME = "umangdb.db"; private String rowid; private String programtext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewprogram); dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME); database = dbOpenHelper.openDataBase(); Intent intent=getIntent(); rowid=intent.getStringExtra("myextra"); String query="select * from engg_table where _id="+rowid; Cursor myCursor = database.rawQuery(query,null); myCursor.moveToFirst(); programtext=myCursor.getString(4); myCursor.close(); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setJavaScriptEnabled(true);
Corresponding XML file (for layout docking):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/article" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines = "22" android:scrollbars = "vertical" /> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> </RelativeLayout>
I went through other forums that have similar questions, but could not find suitable solutions. One of them suggested using javascript to solve this problem . Everything I know about javascripts does web form validation for my college project. Is it possible? If so, how do you suggest me to learn javascript and how exactly should I implement it.
In addition, How does replaceAll ("\ +", "") work , how exactly does it remove unwanted characters? Also I am not very familiar with character encoding standards