Android & phonegap pass parameters to html page - android

How can I pass a json variable to my html page in Android, using PhoneGap?
My code:
public class Activity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String json = readFile();
// I would pass json to menu.html
super.loadUrl("file:///android_asset/www/menu.html");
}
};
Thanks a lot.

Maybe you can use the localStorage to pass your json.
window.localstorage.setItem("Json", json);
and when you want to get this json
string json = window.localstorage.getItem("Json");

You should only be doing this if you are writing a Cordova (PhoneGap) plugin. If this is the case, you should be extending CordovaPlugin, not DroidGap. Then you will be able to return data to the JavaScript via a PluginResult object or the convenience method callbackContext.success(json) Have a look at the Plugin Development Guide, specifically the page on developing for Android.

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
myclass = new MyClass(this, appView);
appView.addJavascriptInterface(this, "MyTeam");
super.loadUrl("file:///android_asset/www/menu.html");
}
and
public class MyClass {
private WebView mAppView;
private DroidGap mGap;
public MyClass(DroidGap gap, WebView view){
mAppView = view;
mGap = gap;
}
}

Related

How to attach webView with another activity so that it opens the webPage related to different posts in Android studio

I am using webView in my application and have another activity with different posts, I want to attach my webView to those posts so that if I click on post 1 the webPage which is linked to it opens up and if I click on post 2 the webPage linked to that post opens up in same webView, is there any way to do so. Also I am using firebase as a database and fetching my web URL from the database. Please help.
I am using Android Studio
webView class file:
public class webViewNews extends AppCompatActivity {
private WebView webviewthis;
private DatabaseReference mdataRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_page);
mdataRef = FirebaseDatabase.getInstance().getReference().child("webing");
webviewthis = (WebView) findViewById(R.id.webView_news);
webviewthis.setWebViewClient(new WebViewClient());
webviewthis.getSettings().setJavaScriptEnabled(true);
webviewthis.getSettings().setLoadsImagesAutomatically(true);
mdataRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
post2web webView = dataSnapshot.getValue(post2web.class);
webviewthis.loadUrl(webView.getWebViewPost());
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
Fetch the url of that post and pass it with intent to webview activity.
Instead of using activity for webview, it is better to open webview in fragment.
It would be faster and easier
try this create one method like this
public static void callWebview(Context context, String url) {
Intent i = new Intent(context, webViewNews.class);
i.putExtra("url", url);
context.startActivity(i);
}
when you need to start webview activity than just call this methos like this
callWebview(this,"https://stackoverflow.com");

How can I display a pdf document into a TextView?

I want to read pdf files and display contents on TextView. is it possible ? or just show pdf into WebView or pdfViewer?
i want to do like it,
public class MainActivity extends Activity {
private TextView showText;
String url="http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showText= (TextView)this.findViewById(R.id.showtext);
showText.setText( ++ convertPdfTotext ++);
}
public void convertPdfTotext(View view)
{
}
}
If you want to use it in text view you can convert it with itext as string,but you'll loose formatting:
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);
Use WebView instead and you can enable even zoom.
WebView mWebView=new WebView(MyPdfViewActivity.this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);

android phonegap webview

hello all i am very new to phonegap.. i want to webview into my application how can i add this? i've created same app. using android but how it can devleope using PHONEGAP?
private WebView mWebView;
//bla bla bla..
#Override
public void onCreate(Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webviewHelp);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyjavascriptInterface(), "HTMLOUT");
mWebView.loadUrl(strURL);
mWebView.setWebViewClient(new HelloWebViewClient());
}
public class MyjavascriptInterface {
public void showHTML(String html)
{
bla bla bla...
}
}
public class HelloWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
bla bla bla...
}
}
}
thanks in advance :Pragna
For your most basic PhoneGap app you should extend your main activity from DroidGap.
import com.phonegap.DroidGap;
public class Main extends DroidGap {
/** Called when the activity is first created. */
//private Button m_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
I would suggest checking out this link for a series of tutorials using Phonegap within the Eclipse development environment. Once you are setup on eclipse, you only need to overide a few lines of code in the Android Activity class to call your web pages. Phonegap takes care of the rest in terms of opening up a webview class and rendering your html within it. You will no longer to code that yourself (as you've done above). The tutorials spell it out quite clearly. You can also add your own custom JavaScript interface methods as well. Again that's described in the tutorials. Hope this helps.
In your case there is a need to Embedding Cordova WebView on Android here is link for it
Modify your main Activity as
public class MainActivity extends Activity implements CordovaInterface {
CordovaWebView cwv;
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
cwv.loadUrl("file:///android_asset/www/index.html");
}
And in your layout replace your webview with
<org.apache.cordova.CordovaWebView
android:id="#+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Android App with PhoneGap: InvocationTarget Exception by calling addJavascriptInterface

I am using the Android SDK and PhoneGap to create a native Android App. Now I want to use Java methods in the view, calling by JS methods.
In the Main Class I called the "addJavascriptInterface" method to bind a java class with the view.
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}
The problem is I get an InvocationTarget Exception when the programm executes the line "appView.addJavascript..." and the program crashes on the device.
Any solutions here?
Thanks!
Calling Java methods from JavaScript is exactly what PhoneGap plugins enable. Checkout several examples here
Using addJavascriptInterface before super.init(); can make the application crash.
You should try the following :
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}

Need help with android WebView

I am new to Android and java development. I create a WebView object in the OnCreate function. Now I need to be able to pass this same object to other functions in the code, such as the onOptionsItemSelected function. I currently have it where I just create a new WebView object in each function where I need it, but this slows down the code since it has to recreate it and such.
Something as simple as this?
public class MyActivity extends Activity {
WebView wv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv = new WebView(this);
setContentView(wv);
#Override
public boolean onOptionsItemSelected(MenuItem item){
wv.doSomething();
}

Categories

Resources