How do i get my web view fit to screen? - android

I have implemented a web view to play a embedded flv video in my application. And it WORKS! but the problem is video frame is too small.. (video only takes about 1/8 of the screen) How can i make my video fit to screen?
Here's my code
MainActivity.java
package com.example.webvideo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.fortunagate.com/adaderana_apps/video_player.php?video=http://derana.lk/content/video/SriGauthamaSambuddha24-15thJune2014.flv");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(new WebChromeClient() {
private View mCustomView;
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/fullscreen_custom_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000"/>
<LinearLayout
android:id="#+id/linearlayout"
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>
</RelativeLayout>

I think you should have your WebView like this:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/fullscreen_custom_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"/>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
Then, when you want it to appear just change the visibility to visible and it will cover the entire screen.

Related

I want to hide Action Bar while scrolling the webview

Basically i have BottomNavigationView activity in that there are 3 fragments (tab1,tab2,tab3) which contain 3 webviews, what i actually wanna to do is while scrolling down the webview my actionbar should hide, likegetSupportActionBar().hide(); i know already there are answers in the stack overflow for the same question, but i don't know what to do in my case, because i am a beginner in android development
here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
This is my first fragment (fragment_tab1)
<LinearLayout 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"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#android:color/transparent"
android:foregroundGravity="top"
android:progressDrawable="#drawable/custom_progress"
android:progress="20"/>
</FrameLayout>
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
This is my java code for tab1
package com.hackerinside.jaisonjoseph.polysocial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
/**
* A simple {#link Fragment} subclass.
*/
public class tab1 extends Fragment {
public ProgressBar bar;
public FrameLayout frameLayout;
public tab1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab1, null);
frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
bar.setMax(100);
final WebView view=(WebView) rootView.findViewById(R.id.webview);
view.loadUrl("http://facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view1,int progress){
frameLayout.setVisibility(View.VISIBLE);
bar.setProgress(progress);
if (progress==100){
frameLayout.setVisibility(View.GONE);
}
super.onProgressChanged(view1,progress);
}
});
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar.setProgress(0);
return rootView;
}
}
Here is a sample idea of implementation.
boolean isShowing;
//oncraete code is here
WebView mScrollView = (WebView) findViewById(R.id.scrollView);
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
float mfloat = mScrollView.getScrollY();
if (mfloat >= tToolbar.getHeight() && isShowing) {
toolbarAnimateHide();
} else if (mfloat == 0 && !isShowing) {
toolbarAnimateShow(0);
}
}
});
private void toolbarAnimateShow(final int verticalOffset) {
isShowing = true;
//tToolbar is your view(any view)
// add getSupportActionBar().hide() this code here to hide actionbar
// remove below code or comment
tToolbar.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
toolbarSetElevation(verticalOffset == 0 ? 0 : TOOLBAR_ELEVATION);
}
});
}
private void toolbarAnimateHide() {
isShowing = false;
// add getSupportActionBar().show() this code here to showactionbar
// remove below code or comment
//tToolbar is your view(any view)
tToolbar.animate()
.translationY(-tToolbar.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
toolbarSetElevation(0);
}
});
}

Webview back button won't go back

Please Help
When you press the Back Button to get out of the application
I tried a lot of solutions, but did not succeed
I will put you code
MainActivity
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity{
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.alrofaiy.com/");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
setProgress(progress * 100);
}
});
webview.setWebViewClient(new InsideWebViewClient());
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
private class InsideWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#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_main, 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.
switch (item.getItemId()){
case R.id.item1:
try {
Intent abb = new Intent(Intent.ACTION_SEND);
abb.setType("text/palin");
abb.putExtra(Intent.EXTRA_SUBJECT , "APP");
String aliwi = "/n /n/n";
aliwi = aliwi + "https:// /n/n";
abb.putExtra(Intent.EXTRA_TEXT, aliwi);
startActivity(Intent.createChooser(abb,""));
}
catch (Exception o)
{o.toString();}
return true;
case R.id.item2:
Intent AAA = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:info#alrofaiy.com?subhect= "));
startActivity(AAA);
return true;
case R.id.link1:
webview.loadUrl("http://www.alrofaiy.com/login_in");
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:padding="0dp">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusableInTouchMode="false"
android:padding="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/text_call"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:autoLink="phone"
android:clickable="true"
android:linksClickable="false"
android:textColor="#fff"
android:background="#000"
android:paddingTop="2dp" />
</RelativeLayout>
What's the solution?
When you run the application and view the site does go out when you press the back button
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
Should be
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
} else {
super.onBackPressed();
}
}

How to add a progress bar for webview

I'm new to app building... I tried to add a progress bar or a loading screen to a practice app but i wasn't successful.
Could you please help and show me how to do it and where to add it in the code.
Tab2.java:
package com.dan.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class Tab3Fragment extends Fragment {
private WebView webView;
private Bundle webViewBundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab2,
container, false);
webView = (WebView) ll.findViewById(R.id.tab2);
webView.setWebViewClient(new WebViewClient());
*public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}*
if (webViewBundle == null) {
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/");
} else {
webView.restoreState(webViewBundle);
}
return ll;
}
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
*<ProgressBar android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:id="#+id/progressBar1"/>*
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Thanks in advance,
Dan.
This is how I have been using it in the past.
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
}
mProgressDialog.setProgress(progress)
if (progress == 100) {
mProgressDialog.dismiss();
}
}
});
Where mProgressDialog is a Dialog where I am showing the progress.

My web view video plays in samsung tab but not in phone

I have created a web view and used a embedded flv video link as for the video link. I was able to play it in my samsung tab but when i try to play it in my samsung phone all is shows is a BLUE LEGO WITH QUESTION MARKS.. What is the reason that i could play it in my tab but not in phone.
This is my code
MainActivity.java
package com.example.webvideo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
//mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.fortunagate.com/adaderana_apps/video_player.php?video=http://derana.lk/content/video/SriGauthamaSambuddha24-15thJune2014.flv");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(new WebChromeClient() {
private View mCustomView;
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearlayout"
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>
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webvideo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.webvideo.MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Is Flashplayer installed on your Phone?
I had to install an older Flash Version on my Device, as Adobe stopped Support for Android a while ago, FLV Playback in Webview doesn't work with newer Android Versions.

Getting admob and webview working together

I've built a webview app that works fine.
I've tried to add admob to the bottom on app, but it doesnt show.
I've figured out if I set the main activity to
setContentView(R.layout.activity_main);
I get ad, but no webview
If I set the main activity to
setContentView(mWebview );
I get the webview but no ad.
How can I get both to show together?
mainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
//setContentView(mWebview);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true"
>
</com.google.ads.AdView>
</LinearLayout>
Make a pointer to the webView1 ex:
mWebview = (WebView)findViewById(R.id.webView1);
try this:
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this will create a pointer to the webView1
mWebview = (WebView)findViewById(R.id.webView1);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</WebView>

Categories

Resources