show pdf file in pop up window [duplicate] - android

This question already has answers here:
open pdf in popup android
(2 answers)
Closed 9 years ago.
Iam developing an android app.I need to show my pdf file(which contains only one page) in popup window.here Iam starting new activity from popup.i have changed its theme in manifest.xml as:
<activity
android:name="com.example.myapp.Label"
android:label="#string/title_activity_label"
android:theme="#android:style/Theme.Dialog" >
</activity>
code of my Label.java is:
public class Label extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b=getIntent().getExtras();
String pdfurl=b.getString("url");
Boolean dilg=b.getBoolean("isDialog");
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(Label.this);
// mWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(PluginState.ON);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + pdfurl));
setContentView(mWebView);
}
}
My android version is:4.2
It is opening new activity in pop-up but not opening PDF.Is there any mistake in my code?

You need to display a custom dialog with webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<WebView
android:id="#+id/webview"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
Display a dialog.
Dialog dialog = new Dialog(Activity.this);
dialog.setContentView(R.layout.web_dialog)
WebView wb = (WebView) dialog.findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = wv.getSettings();
webSettings.setPluginState(PluginState.ON);
wb.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(image_urlpdf);
return false; // then it is not handled by default action
}
});
wb.loadUrl((googleDocsUrl + image_urlpdf));
dialog.setCancelable(true);
dialog.setTitle("WebView");
dialog.show();

Related

Am trying to Use button to Load html file from asset folder

Am trying to Use button to Load html file from asset folder
but after clicking the button the html file refuse to load
Activity_main.xml
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="open"
android:text="lawal"
android:textSize="20dp" />
<WebView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="#+id/webView"
/>
Main.java
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view) {
webView = (WebView)findViewById(R.id.webView);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///Android_asset/1.html");
setContentView(view);
}
}
android_asset should be in lower case.Change your button onClick() code as below. here test.html should be replaced by your file name.
you need to create assets folder inside main folder.
WebView webView = view.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/test.html");
Initialize the webview in oncreate after setting the content view
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
and then load your url in open function
webView.loadUrl("file:///android_asset/1.html");

webView in android open the website in web instead of webview

when I run this code, "google.com" open in web application (for examaple google chrome) and not in the webView in the dialog.
It's does work OK with some urls.
Why?
final Dialog dialog=new Dialog(this.activity,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.full_page_ad_between_chapters);
WebView webView = (WebView) dialog.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://google.com");
R.layout.full_page_ad_between_chapters :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="1000dp"
android:layout_margin="5dp"
android:orientation="vertical" >
<Button
android:id="#+id/fullPageAdBetweenChaptersClose"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="סגור" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
You need to override the method shouldOverrideUrlLoading() on your WebViewClient.
WebView webView = (WebView) dialog.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check if we want to open the url into the WebView
if (iWantToOpenTheUrlIntoTheWebView(url)) {
return false;
}
// let the system handle the url outside of the app
return true;
}
});

Webview in Android 4.4 logs nativeOnDraw failed

This question has been asked many times, but none of the answers works for me.
I'm using webview playing video url = "http://youtu.be/gm4wlE04hbI" , the webview does not work and turn blank, it logs
01-30 00:00:31.640: W/AwContents(16221): nativeOnDraw failed; clearing to background color.
My layout is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/layout_base_top_bar" />
<WebView
android:id="#+id/web_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</WebView>
</LinearLayout>`
And the code
webView = (WebView) findViewById(R.id.web_content);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(PluginState.ON);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
L.e(newProgress + "");
}
});
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
String url = getIntent().getStringExtra("url");
webView.loadUrl(url);
And close the hardware
android:hardwareAccelerated="false"
I hope some could tell me what's wrong with it ....

website is not opening in webview

I am trying to open a url in my webview but the problem is that the url is not loading in my webview, its showing pop-up for the selection of browser and as soon as i click on an option it opens the url in that browser. Can somebody help me to solve this, i want the url to open in the webview itself not in the browser.
my xml for webview is :-
<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="fill_parent"
android:background="#drawable/bg_new" >
<RelativeLayout
android:id="#+id/upBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/wbview_bckbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back_btn_chng" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/upBar" >
<WebView
android:id="#+id/linkwebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbars="none" />
</RelativeLayout>
</RelativeLayout>
and here's my code for webview :-
public class FbTwtrLink extends Activity {
WebView web;
ImageView img,bckimg,share_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fb_twtr_link);
img = (ImageView)findViewById(R.id.header);
bckimg = (ImageView) findViewById(R.id.wbview_bckbtn);
web=(WebView)findViewById(R.id.linkwebview);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_fb_twtr_link, menu);
return true;
}
}
and i m calling the webview like that :-
fbicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(context,FbTwtrLink.class);
//intent.putExtra("fb_link", 1);
startActivity(intent);
}
});
You should override WebViewClient and its method shouldOverrideUrlLoading
web.setWebViewClient(new MyWebClient());
web.loadUrl("http://www.google.com");
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//1 option
// getApplicationContext().startActivity(
// new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// 2 option
//view.loadUrl(url);
return true;
}
}
Add this line may help:
webView.getSettings().setPluginState(PluginState.ON);
also add internet permission in your manifest
<uses-permission android:name="android.permission.INTERNET" />
Also if using API lever greater equal to 11 then you can use this in you manifest at application level
android:hardwareAccelerated="true"
You can add the below statement to open in webview:
mweb.setWebViewClient(new WebViewClient());
You can use Direct this >
fbicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (android.content.Intent.ACTION_VIEW, Uri.parse ("http://www,google.com")); // Prepare intent
startActivity(intent);
}
});

How do I get a progress dialog to show up when any link is clicked in a Webview

I have a header with some buttons in it and a webview below it. I have a working progress dialog for when the page originally opens and when a button is clicked. The problem is when I click any links inside of the webview a progress dialog doesn't show. Does anyone know the best method to make this happen? I included below the code for my WebViewClient and one of the buttons.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "","Loading...", true);
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
final Button btnCategory = (Button) findViewById(R.id.btnCategory);
btnCategory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "","Loading...", true);
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
mWebView.loadUrl("http://mywebsite.com/page");
}
});
Instead of trying to show a dialog you could just toggle the visibility of a progress bar.
I haven't tried it myself but it could look like this:
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible" >
</ProgressBar>
you could use a relativelayout to include the webview and the progressbar (which is centered in the relativelayout).

Categories

Resources