I have the following code that works but doesn't use my layout webscreen.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webscreen);
String turl = getIntent().getStringExtra(URL);
Log.v(TAG, "Recipe url = "+turl);
WebView webview = new WebView(this);
setContentView(webview);
webview.clearCache(true);
webview.loadUrl(turl);
If I change the line setcontentview(webview) to setcontentview(R.layout.webscreen) my layout loads but the web page doesn't.
Sorry I am a newbie.
Kind Regards,
Mike
Try This,
public class Main extends Activity {
WebView webview1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.getSettings().setJavaScriptEnabled(true);
webview1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview1.getSettings().setBuiltInZoomControls(true);
webview1.setInitialScale(50);
webview1.loadUrl("http://www.mywebsite.com/");
}
}
xml file
<?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="fill_parent">
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="horizontal">
<WebView android:layout_marginTop="60sp" android:id="#+id/webview01" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</ScrollView>
</LinearLayout>
Related
there are two webviews in scrollview.
<ScrollView>
<LinearLayout>
<WebView></WebView>
<WebView></WebView>
</LinearLayout>
</ScrollView>
I want to make two webviews looks like one webpage. currently, my solution is make webview's height equal to webview's contentHeight. and disable scrollability of webview. but for very big html. it's not a good solution. webview has to render the whole html. I want to use nestedscrollview. make two webviews' height equals to nestedscrollview's height. but I dont known how to deal the events.
Try this to make two WebView looks like one webpage.
public class MainActivity extends Activity {
String url = "http://www.yahoo.com";
String url1 = "http://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "Activity one");
WebView webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewController());
webview.loadUrl(url);
WebView webview2 = (WebView) findViewById(R.id.webView2);
webview2.getSettings().setJavaScriptEnabled(true);
webview2.setWebViewClient(new WebViewController());
webview2.loadUrl(url1);
}
}
XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
WebView client
class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
So i have this button that I would like it to open a webviewer when the button is pressed.
Here is the xml where the button is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
</LinearLayout>
Here the class code for the click button
public class SignIn extends Activity {
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img = (ImageView) findViewById(R.id.signIn);
signIn();
}
public void signIn() {
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SignIn.this, WebViewActivity.class);
startActivity(intent);
}
});
}
}
here is where i have the webviwer.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
And here is the webviewer class
public class WebViewActivity extends Activity{
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
When click nothing happens, I also edited the manifest to have internet access
Make a small correction. In XML you are using "ImageButton" but in java file your are using "ImageView".
<ImageView
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
I think it is wrong to write in XML ImageButton and in java file to search for an ImageView by id.
Use an ImageView or an ImageButton!
if you want to use an imageView don forget to make it clicable.
WebView in my app shows as window, but I want it be fullscreen! Here is screenshort of it:
Here is my code:
public class DockViewerActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
mWebView.loadUrl("file:///android_asset/1.html");
Toast.makeText(this, getString(R.string.loading), Toast.LENGTH_LONG).show();
}
}
XML
set the height and width to match_parent like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/conrainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent "
android:layout_height="match_parent "/>
</RelativeLayout>
Activity::
to remove the status bar you have to use:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
to remove titlebar,you have to use:
requestWindowFeature(Window.FEATURE_NO_TITLE);
both of the above features you have to set before set your content view like below i am doing:
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.base_screen);
}
if you Create webView programtically then set the height, width to match_parent like:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mWebView .setLayoutParams(layoutParams);
and its done now!! #cheers!!
Here is my codes to achieve full screen webview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webview);
...
}
And XML Layout of this activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mains"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
...
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
/>
...
</RelativeLayout>
htmlp = URLEncoder.encode(desc,"utf-8").replaceAll("\\+"," ");
WebView wv=(WebView)findViewById(R.id.webView1);
wv.setInitialScale(70);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadData(htmlp,"text/html", "utf-8");
In htmlp contains HTML content(tags). Now I have to enable zooming control, but using above code it is not working. Is anything I have to enable in xml part of the webview.
Thanks in advance
This works good in my case.Try it for your case..
In manifest file..
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
webview.xml
<?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>
In activity..
public class Main extends Activity {
private WebView myWebView;
private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.webview);
this.myWebView = (WebView) this.findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
this.myWebView.loadUrl("http://www.facebook.com");
}
}
wv.getSettings().setBuiltInZoomControls(false);
change to false
At last I have solved this problem. The problem was in the design section. Just changed the layout param to this:
WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
xml
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
tabview xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Tabview java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, WebActivity.class);
spec = tabHost.newTabSpec("webview")
.setIndicator("webview", res.getDrawable(R.drawable.info))
.setContent(intent);
tabHost.addTab(spec);
// add other tabs
tabHost.setCurrentTab(0);
}
This will launch a webView in full screen.
Is it possible to display webview inside the tabview?
You can find the answer here: http://developer.android.com/guide/webapps/webview.html#HandlingNavigation
By default WebView launches the browser everytime you visit a new URL, so what happens is, it launches the browser.
To avoid this happening everytime you click something etc, you need to add a WebViewClient to your WebView:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
If you need to peform some particular action when a user clicks a link, implement your own WebViewClient:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean result = false;
/* ... */
// Return false to proceed loading page, true to interrupt loading
return result;
}
}
And use it:
myWebView.setWebViewClient(new MyWebViewClient());