Android WebView density issue - android

Got some problems with the WebView class. I've got a WebView inside a RelativeLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00"
android:orientation="vertical" >
<WebView
android:id="#+id/mWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#0000FF"/>
<Button
android:id="#+id/adminBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Admin" />
</RelativeLayout>
The WebView is contained in a Fragment, code is this
public class WebViewFragment extends Fragment {
private WebView webView;
private Button adminBtn;
private String url = "http://www.my-local.guide"
#SuppressLint("SetJavaScriptEnabled")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.webview_fragment_layout,
container, false);
webView = (WebView) rootView.findViewById(R.id.mWebView);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setInitialScale(120);
webView.setWebViewClient(new WebViewClient() {
});
webView.loadUrl(url);
adminBtn = (Button) rootView.findViewById(R.id.adminBtn);
adminBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCheckPasswordDialog();
}
});
return rootView;
}
public static WebViewFragment get() {
WebViewFragment fr = new WebViewFragment();
return fr;
}
private void showCheckPasswordDialog() {
CheckPasswordDialog checkPasswordDialog = CheckPasswordDialog.get();
checkPasswordDialog.show(getFragmentManager(), SHOW_PASSWORD_DIALOG);
}
}
It seems like the WebView scales his width to 960px. I write a js script on the page and it tells me that page's width is 960px.
I need to set a larger page's width to use Bootstrap as framework in my embedded websites.
Using google.com as url anyway I can get all the screen's width, so I'm guessing if I have to use specific css rules for 960ps width or if I can modify the WebView's width
thank you for your time

Finally I fix it, I was adding to many functions that was overriding themselves
webView = (WebView) rootView.findViewById(R.id.mWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setInitialScale(100);
this code works

Related

Nest ScrollView

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;
}
}

android webView zoom in out problems

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);

how can i open a webpage in a webview from a fragment with buttons?

I read the answer of https://stackoverflow.com/users/1116216/michele-la-ferla (michele la ferla) here: how can i open a webpage in a webview from a fragment?
but when you have more buttons for more URL in "layout.xml" file,
how modify you the "fragment class" file?
this is my code for the first button, where is the error?:
private Dialog WebDialog1;
private WebView URL1;
private Button button0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragmentsite, container, false);
button0 = (Button) rootView.findViewById(R.id.button0);
button0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
WebDialog1 = new Dialog(getActivity());
WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
WebDialog1.setContentView(R.layout.sitef);
WebDialog1.setCancelable(true);
URL1 = (WebView) rootView.findViewById(R.id.webview);
URL1.setWebViewClient(new WebViewClient());
URL1.setScrollbarFadingEnabled(true);
URL1.setHorizontalScrollBarEnabled(false);
URL1.getSettings().setJavaScriptEnabled(true);
URL1.loadUrl("http://www.dhfhfhfh.com/Home.php");
WebDialog1.show();
}
});return rootView;}}'
If I understood your question well, your scenario is composed of the following:
A layout file with more than one button.
Each button loads a different url in a webview.
Taking these into consideration, I would implement it using an ActionListener on each button, and sending the parameters to load the url in a webview in the ActionEvent.
Fragment event:
public class EventFragment extends Fragment {
private Dialog WebDialog1, WebDialog2;
private WebView URL1, URL2;
private Button btnURL1, btnURL2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_layout, container, false);
btnURL1 = (Button) rootView.findViewById(R.id.btnURL1);
btnURL1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
WebDialog1 = new Dialog(getActivity());
WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
WebDialog1.setContentView(R.layout.web_layout);
WebDialog1.setCancelable(true);
URL1 = (WebView) WebDialog.findViewById(R.id.url1);
URL1.setWebViewClient(new WebViewClient());
URL1.setScrollbarFadingEnabled(true);
URL1.setHorizontalScrollBarEnabled(false);
URL1.getSettings().setJavaScriptEnabled(true);
URL1.getSettings().setUserAgentString("First Webview");
URL1.loadUrl("//the first url goes here");
WebDialog1.show();
}
btnURL2 = (Button) rootView.findViewById(R.id.btnURL2);
btnURL2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
WebDialog2 = new Dialog(getActivity());
WebDialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
WebDialog2.setContentView(R.layout.web_layout);
WebDialog2.setCancelable(true);
URL2 = (WebView) WebDialog.findViewById(R.id.url2);
URL2.setWebViewClient(new WebViewClient());
URL2.setScrollbarFadingEnabled(true);
URL2.setHorizontalScrollBarEnabled(false);
URL2.getSettings().setJavaScriptEnabled(true);
URL2.getSettings().setUserAgentString("Second Webview");
URL2.loadUrl("//the second url goes here");
WebDialog2.show();
}
});
return rootView;
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnURL1"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
<Button
android:id="#+id/btnURL2"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
</RelativeLayout>
web_layout.xml
<?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" >
<RelativeLayout
android:id="#+id/rl_relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="10dip"
android:layout_weight="0.82" >
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="Buy your Tickets:" />
<WebView
android:id="#+id/ticketline"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/Title"
android:layout_marginTop="5dip" />
</RelativeLayout>
</LinearLayout>
While at it, I would take a look at the official Android documentation for buttons and ActionListeners too.

webview is not displaying correct

I am trying to open url under webview. I have the code below:
public class FragmentWebView extends Fragment {
TextView webview_bar_title;
ImageButton menu;
WebView wv;
public FragmentWebView() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container,
false);
menu = (ImageButton) view.findViewById(R.id.webview_menu);
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity) getActivity()).onBackPressed();
}
});
webview_bar_title = (TextView) view.findViewById(R.id.webview_share);
webview_bar_title.setText("Share");
wv = (WebView) view.findViewById(R.id.fragment_webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://www.google.com");
return view;
}
}
And here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/webview_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/webview_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_left" />
<TextView
android:id="#+id/webview_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</RelativeLayout>
<WebView
android:id="#+id/fragment_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/webview_actionbar" />
</RelativeLayout>
I am trying to load google page in my webview. But it's not working and gave me errors like The website is not available. The webpage google might be temporarily down or it might have moved permanently to a new address. And suggestion? Thanks in advance.
Make sure you're setting a WebViewClient to your WebView and that you declare the INTERNET permission in the manifest.
To set the client:
wv.setWebViewClient(new WebViewClient());
To declare the permission in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
Those are the only things that I see missing in your code.
Also, it's much more common practice to subclass WebViewClient to handle different page events instead of using the default client object.

WebView size always 0

I can't seem to get my WebView to display. I suspect it's because its size is 0. I'm not sure how to un-zero this WebView!
This is my 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/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and this is my onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.twitterfeed, null, false);
WebView webView = (WebView) webViewLayout.findViewById(R.id.webView1);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
String summary = "http://www.example.com";
webView.loadUrl(summary);
System.out.println("webView: " + webView.getWidth());
System.out.println("webView: " + webView.getHeight());
}
You're inflating your layout but never displaying it by attaching it to the app window with Activity.setContentView(). Use either
View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.twitterfeed, null, false);
setContentView(webViewLayout);
WebView webView = (WebView) webViewLayout.findViewById(R.id.webView1);
or more simply just let the framework do the inflation for you:
setContentView(R.layout.twitterfeed);
WebView webView = (WebView) findViewById(R.id.webView1);
you need to use ViewTreeObserver to measure the webview layout width and height.
ViewTreeObserver trigers it's global layout listener, and has it's own thread to measure the layout dimensions.
webView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
System.out.println("webView: " + webView.getWidth());
System.out.println("webView: " + webView.getHeight());
}
});

Categories

Resources