vendredi 26 février 2016

Android - Show webview in same tab with WebChromeClient

I'm currently working on an wbesite app to display a website in full screen + center the page, but as you can see Here the website is almost made with only HTML5 animations so I tried with the default WebClient (WebViewClient) but only the first page was able to be diplayed cause it dosen't have any kind of animations, so now I'm trying to do the same thing I did with the Default WebView but Using Chrome, but for some reason every time I click on something in my app, it open the chrome browser and set my app in the background, any idea how I can fix that and open the links I've clicked witout exiting my app ?

Thanks in advance for your help

(And Sry if my question may seems kinda stupid but I'm a newbie in Android programming so if you can explain with some details I would be really grateful ^^ )

Here is the BrowserApp.java :

 package fr.so_dupontrenoux.indigo;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;


public class BrowserApp extends Activity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See http://ift.tt/1Shh2Dk for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser_app);

        WebView webView= (WebView) findViewById(R.id.webView);

       webView.setWebChromeClient(new WebChromeClient());
        //webView.setWebViewClient(new WebViewClient());

        webView.getSettings().setJavaScriptEnabled(true);


        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);

        webView.getSettings().setUseWideViewPort(true);
        //Preserver la page ( cadrage 1:1 adaptatif )

//Test

//END TEST

        webView.loadUrl("http://ift.tt/1KPKXUy");


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See http://ift.tt/1Shh2Dk for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_browser_app, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See http://ift.tt/1Shh2Dk for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "BrowserApp Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://fr.so_dupontrenoux.indigo/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See http://ift.tt/1Shh2Dk for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "BrowserApp Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://fr.so_dupontrenoux.indigo/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

And the Manifest.xml :

<manifest xmlns:android="http://ift.tt/nIICcg"
    package="fr.so_dupontrenoux.indigo" >
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"

        android:hardwareAccelerated="true"

        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <!-- This line is important -->


        <activity
            android:name=".BrowserApp"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See http://ift.tt/1Shh2Dk for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>




Aucun commentaire:

Enregistrer un commentaire