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");
Related
I'm trying to add this WebView to onClick function of a button but,
WebView is always there before even click the button. I want WebView
appear only when clicked . can anyone tell which code I need to put to
get the WebView work when the button clicked .
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
xml code
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
protected Button mButton;
private WebView webview;
ViewPager mHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_home);
Button mButton = (Button) findViewById(R.id.infobutton);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
String chtml = "<html>"+
"<style> div{height:50%; width:70%; background-color:#aaa;} body div:nth-child(2){height:20%; margin:20px; width:30%; background-color:#222;} h1{ color:red; font-size:24px;}</style>" +
"<body><h1>hello, webview</h1> <div></div> <div></div> </body></html>";
webview.loadData(chtml, "text/html", "UTF-8");
}
});
java code
For that you can set the visibility to GONE in the xml and then on the button click you need to set visibility of the webview to VISIBLE.
<WebView
android:id="#+id/web"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
make this us layout don't forget to add
android:visibility="invisible"
in java
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fgfg);
Button bt = (Button)findViewById(R.id.button);
web =(WebView)findViewById(R.id.web);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
web.setVisibility(View.VISIBLE);
startWebView("http://www.youtube.com");
}
});
}
inside button click
finally found way to add WebView to api data
create button
protected WebView eventDesctibtion;
assign button to view
eventDesctibtion = (WebView) findViewById(R.id.corporateDescription);
WebSettings settings= eventDesctibtion.getSettings();
settings.setDefaultFontSize(11);
load data
eventDesctibtion.loadData(object.getString("description"), "text/html; charset=utf-8", "UTF-8");
I am developing an app and there is webview tab, on it i give zoom in out from .getSettings().setSupportZoom(true);. However when i zoom out (the view gets smaller and smaller until minus button(zoom out)disabled itself)the webview showing white background that i don't even set a background on the layout. how to make webview on zoom out have some resolution which will fit the height of the screen? and I am curious if we can make the zoom controls always seen?(it only appear whenever we touch the screen and make some move on it(tap, hold and move))
here is my java code from webview
public class WebViewActivity extends Activity {
private WebView webView;
//private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
#Override
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.somewebsitename.com");
}
}
and here is my xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
thanks in advance
Try this
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
I am trying to stream a video stored on dropbox on android's webview but only a blank space is
displayed, my code is below :
activity_web_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/webView"
android:text="Description" >
</TextView>
</RelativeLayout>
public class WebViewA extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.dropbox.com/s/XXXX/video.mp4?dl=0?client_id=XXXX");
}
}
I have also tried the following Urls on webView.loadUrl(), but none
of them work, only a blank space appears :
"https://dl.dropbox.com/s/XXXX/video1.mp4?dl=0?client_id=XXXX"
"https://dl.dropbox.com/s/XXXX/video2.mp4?dl=1?client_id=XXXX"
"https://dl.dropboxusercontent.com/s/XXXX/video1.mp4"
"https://www.dropbox.com/s/XXXX/video1.mp4?dl=0"
if I try http://www.google.com it works, I have also seen some tutorials
with dropbox addresses that have an 'u' instead of an 's' like
"https://www.dropbox.com/u/..."
does WebView only works with dropbox urls with an 'u', if so how can I
get that type of address.
I am new to android development and i'm doing a an app where i need to embed an iframe in android's webview..
iframe sample is
<iframe src="//e.infogr.am/chart-61108188168" width="550" height="680" scrolling="no" frameborder="0" style="border:none;"></iframe>
Can you just set up a simple webview pointing to the iFrames url like so?
Create a webview in the xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then in your code set it up as:
public class WebActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webcontent);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://websitesby.projectcaruso.com/");
}
}
I am trying to make a simple app that starts the webbrowser on the right page. Simply enougth I found the code to do it right here on stackoverflow.
But there is a problem, if you open the website threw the app, then click home button and open the app again then nothing happens and the browser does not open.
Does anybody know away to force the browser in the foreground?
Edit: Extend question:
Here is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" />
</RelativeLayout>
And this is the MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
oWebView = (WebView) findViewById(R.id.webview);
oWebView.setWebViewClient(new WebViewClient());
oWebView.getSettings().setJavaScriptEnabled(true);
String url = "http://google.com";
oWebView.loadUrl(url);
}
My problem is with the fullscreen, the Webview does not extend to the hole screen. See the top right:
It doesn't really matter on this websites but on other it messes up the views.
Try this :
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
WebView oWebView = (WebView)findViewById(R.id.webView1);
oWebView.setWebViewClient(new WebViewClient());
oWebView.getSettings().setJavaScriptEnabled(true);
oWebView.loadUrl(your_url);
xml: android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//or in code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
WebView oWebView = (WebView)findViewById(R.id.webView1);
oWebView.setWebViewClient(new WebViewClient());
oWebView.getSettings().setJavaScriptEnabled(true);
oWebView.loadUrl(your_url);
onResume(){
oWebView.reload();
}