Gestures with WebView - java

What I'm trying to do:
I want the user to be able to custom gestures and name them "up","down"..etc. Then when the user runs my application, it loads up a WebView with a particular url like http://google.com. Then the user can draw the gesture he created earlier. So if he draws the "up" gesture, then another webview opens.
My problem:
When I try to draw the gesture over the webview, it doesn't draw. Rather it scrolls the webview.
What have I tried:
Already created a GestureOverlayView and put the WebView within that. Unfortunately, the GestureOverlayView is unable to detect the gestures as the WebView gets scrolled.
My Code:
public class WebViewCenter extends Activity implements OnGesturePerformedListener {
/** Called when the activity is first created. */
GestureLibrary mLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//WebView webview = (WebView)findViewById(R.id.WebView01);
//webview.loadUrl("http://www.google.com");
//Button btnStart = (Button)findViewById(R.id.Start);
//btnStart.setOnClickListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures_overlay);
gestures.addOnGesturePerformedListener(this);
WebView webview = (WebView)findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://live.com");
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
mLibrary.load();
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
Log.i("Default Marker", predictions.get(0).toString());
if (predictions.size() > 0) {
String action = predictions.get(0).name;
Log.i("Gesture", action);
if ("up".equals(action)) {
Log.i("Up_gesture", "Up gesture detected");
Intent int_up = new Intent(WebViewCenter.this, WebViewUp.class);
WebViewCenter.this.startActivity(int_up);
//return true;
//Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
}
/*else
{
Log.i("Marker 2", "god knows what ur upto");
}*/
}
}
Any advice as to how can I separate the WebView scrolling from the gesture building?
Thanks,
Pawan

Related

Webview activity not showing anything on second load

I have this weird problem when showing an activity which consists (only) out of a webview.
The first time I start this Activity B by clicking on something in activity A, B is properly loaded and functional. I press the return button, go back to A, then click on the same button to go to B and my webview doesn't show anything. Debugged it tons of times and nothing seems out of the ordinary. There are also no crashes in the stack trace log... This problem by the wway occurs always in debug mode and only sometimes in run mode.
Here is the code for the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin);
myItem = (Item) getIntent().getSerializableExtra("Item");
initView();
initHandlers();
}
private void initView()
{
webView = findViewById(R.id.webview_spin);
//TODO : webViewClient vs webChromeClient ?
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String json = new Gson().toJson(myItem);
webView.loadUrl("file:///android_asset/item.html?item="+json);
}
private void initHandlers()
{
OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
#Override
public void onOrientationChanged(int orientation) {
webView.reload();
}
};
orientationEventListener.enable();
}
FYI this problem occured before setting the orientationlistener.
That looks like somehow the render process of the WebView is failing. You could store the WebViewClient you created on a variable and check the result of its method onRenderProcessGone() to check if it is rendering the second time you load the activity. Here you have all the WebViewClient methods: https://developer.android.com/reference/android/webkit/WebViewClient

How to create a wrapper object for a webview

so I have a working webview that is simple, but most of the work is done in the main activity. I am trying to break it out to its own little class so I can load other classes into the main activity on the fly. I am stuck on how to do this. I have tried this a few times and been debugging it but I just dont know if I am using the right findByValue(R.id.webView); thing here. it keeps giving me a
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
issue. I have tried passing the reference in as a parameter and also hard coded it. I am missing something but dont know what. There is a WebView in my activity_main that is called wevView that is what I am trying to find out where i'm did mistake.
Thanks for any help with this.
So this is my webview class:
public class NewWebView {
//variables need for webview
private final String myPage = "http://www.google.com";
WebView myWebView = null;
//consturctor
public NewWebView(){}
//This method creates the WebView
public void createWebView(WebView webView)
{
//WebView code
//Find the Webview in the Activity.xml file
//myWebView = webView;
myWebView = (WebView)myWebView.findViewById(R.id.webview);
// myWebView = (android.webkit.WebView)findViewById(R.id.webview);
//Use the custom WebViewClient for internal app browsing
myWebView.setWebViewClient(new MyWebViewClient());
//now for enabling the settings
WebSettings webSettings = myWebView.getSettings();
//Enable JavaScript
webSettings.setJavaScriptEnabled(true);
//Enable andriod zoom features
webSettings.setBuiltInZoomControls(true);
//Loading the Url
myWebView.loadUrl(myPage);
}
//Methods Used for WebViewer
//custom WebViewClent needed for internal app navigatiion
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverRideUrlLoading(android.webkit.WebView view, String url)
{
if(Uri.parse(url).getHost().equals(myPage))
{
//this is Apivita remain in the APP
return false;
}
/*
//if it is not in my site redirect it to mobile browers
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
*/
return true;
}
}
//Enable the use of the system back button for navigation
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if the back button was pushed
if((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
//if not
return false;
}
}
and here is the main activity
public class MainActivity extends AppCompatActivity {
WebView myWebView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the webview
myWebView = (android.webkit.WebView)findViewById(R.id.webview);
NewWebView apWebView = new NewWebView();
apWebView.createWebView(myWebView);
}
}
It looks like you are finding your webview in the main activity and then passing it into your class. In your custom class, you're then trying to find another view, which you technically already have. So its trying to find a webview, inside of the webview. It obviously can't find this, so it's null.
In theory, you should be able to just use myWebView = webView;, however, you need to make sure that it is finding it in the first place.

ANDROID: Facebook and Browser Open inside my app, not in their own

this will be a bit annoying to explain, but bear with me:
So i have a simple android app with buttons, one opens a facebook page (first checks if the app is installed, and will open in that, else will open same page in browser), the other opens a website url in the browser
The problem i'm having is that, instead of opening them as seperate applications, it seems to be opening the browser/fb app inside my original app.
So if i open the fb page through my app, and click the back button, it brings me back to my app's home screen. If i click the fb button, and minimize the app, and go into the ACTUAL fb app, it hasn't changed anything there.
So my app is not actually using the standalone fb app, just calling it up inside itself.
(hope that makes sense...) here is the code i used;
//just defined button variables
button facebook, shop;
//now for the onCreate method
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook = (Button) findViewById(R.id.facebook);
shop = (Button) findViewById(R.id.shop);
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{ //left out the profile id on purpose...
Intent facebook = new Intent(Intent.ACTON_VIEW, Uri.parse("fb://profile/xxxx"));
startActivity(facebook);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.facebook.com/example")));
}
}
});
The shop button is set up the same, only without the try/catch, it will only open in the browser.
How do i make it so that it sends the request to the actual fb app/browser, instead of what it's currently doing? Any help/tips are gratefully welcome :)
Create a new layout with layout/a.xml
Open the layout file & then add a WebView, assign an ID so you can access the widget later.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Insert Permission in your manifest file as you are accessing INTERNET in your application
<uses-permission android:name="android.permission.INTERNET" />
Inside the new Activity -->WebActivity.java
public class WebActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.facebook.com");
}
}
If you want to handle navigation inside the webView on your own
mywebview.setWebViewClient(new WebViewClient())
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
Now to redirect from your main activity to these new activity ::
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
boolean isFacebookInstalled = true;
}
catch( PackageManager.NameNotFoundException e ){
isFacebookInstalled=false;
}
if(isFacebookInstalled)
{
//start the facebook app
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);
}
else
{
Intent facebook = new Intent(getApplicationContext(),WebActivity.class);
startActivity(facebook);
}
});
Same way you can also insert for your shop button

Creating An In-Game Web Browser on Android

In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".

Adding a progress bar to webview

I have been trying to put a back button progress bar in a webview and keep the url loading within my app instead of using the android default web browser.
If I manage to keep to browsing within the app and keep the back button the progress bar never shows up if I manage to get the progress bar to show up the code at the bottom for shouldoverideurl come up never read and the default browser launches, I tried all the google tutorials and solution but none of them work. I am currently using google.. Can anyone help??
public class livebrad extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.brows);
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
// Get Web view
mWebView = (WebView) findViewById( R.id.webView ); //This is the id you gave
//to the WebView in the main.xml
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this
//if ROM supports Multi-Touch
mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
// Load URL
mWebView.loadUrl("http://www.bbc.co.uk");
// Sets the Chrome Client, and defines the onProgressChanged
// This makes the Progress bar be updated.
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
});
}//End of Method onCreate
}
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SandbarinFacebook extends Activity {
WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fb);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
true);
mWebView = (WebView) findViewById(R.id.webkitWebView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
mWebView.loadUrl("http://m.facebook.com/sandbarathens");
}
}
If you want show progress bar each time user clicks links, add code to show progressbar in your shouldOverrideUrlLoading() method.
You just missed this statement.
mWebView.setWebViewClient(..)

Categories