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");
Related
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");
I has activity with webView. Here layout xml:
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here activity code:
public class TestActivity extends AppCompatActivity {
public static final String HTML_TEXT = "HTML_TEXT";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
String htmlText = getIntent().getExtras().getString(HTML_TEXT);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String resultHTML = "<html><head><meta charset=utf-8></head><body>" + htmlText + "</body></html>";
webview.loadDataWithBaseURL("fake://", resultHTML, "text/html", StringUtil.encodingUTF8, null);
}
}
And here result:
And as you can see on right side success show scrollbar when scrolling.
This work on Android 4.0, 5.0, 6.0, 7.0. OK.
But on Android 8.0 (Emulator) when scrolling to item "Test many posts 7/15" (about half screen) scrollbar is hide. I'm scrolling to bottom of screen but scrollbar not show.
Here result:
and scrolling to bottom of screen (scrollbar is hide):
How fix this?
in your onCreate() method try this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_web_view_layout);
browser = (WebView)findViewById(R.id.your_webview);
WebSettings mWebSettings = browser.getSettings();
mWebSettings.setBuiltInZoomControls(true);
browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
browser.setScrollbarFadingEnabled(false);
browser.loadUrl("file:///android_asset/index.html");
}
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.
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
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>