I'm a beginner on this kinda stuff.
I have a button that when I click on it, it loads a page on webview.
The code is the following:
Right now when I'm clicking on the button, the page doesn't load.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web2);
String url = "http://giae.aeourem.pt";
WebView web = (WebView) findViewById(R.id.webview2);
web.loadUrl(url);
}
}
Related
I have my webpage (CYNX.ML/U1.html) which displays perfectly in all other browsers, However I am using WebView to display it to the users which seems to shown it broken.
This is how it appears on WebView- http://prntscr.com/irhosm
Any way to fix this? It seems to be displayed perfectly on all mobile web browsers- Including Chrome, Dolphin Web browser, UC web browser
*CODE of MainActivity.java-
package com.sirseni.simpleandroidwebviewexample;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("http://cynx.ml/U1.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
}
// Use When the user clicks a link from a web page in your WebView
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.centerend.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
**UPDATE:**Had to inform that I feel WebView is having slight problems with the page's css styling, however Google seems to show it as 100% mobile compatible. Any ideas?
in my case when i try to set the button enable using BUTTON1.setEnable(false);
so the emulator does not start the apk...
i want to create a game level menu with some buttons Enable and rest of the buttons Disable .as each button is a game level .. so if my integer flag has value 5 so first five levels buttons should be active and rest of the buttons be disable.... but i could not even set a single button disable ... please help am i missing some concept with buttons ... where to change their state in OnResume() or in OnCreate();
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b1=(Button)findViewById(R.id.button);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setEnabled(false);
}
}
You should add b1 = (Button) findViewById(R.id.button) into your onCreate() method.
Try this:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b1.setEnabled(false);
}
}
I don't have an android development environment at hand right now, but I could imagine, that it has somethin to do, with the view (and therefore the button) not beining available at the time of the initiation of the attribute b1. Could you try the following:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getButton1().setEnabled(false);
}
private Button getButton1() {
return (Button)findViewById(R.id.button);
}
}
so in order to set buttons Enable/Disable one have to declare the buttons into OnCreate ...otherwise it wont find the view of the button and show errors ...
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b1.setEnabled(false);
}
}
is the right way .............thanks everone
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new CustomWebViewClient());
mWebView.loadUrl("http://groupedirectouest.com");
}
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
when I use ua (userAgent) string, webview shows desktop version of website.
here is the ua string i have used :
mWebView.getSettings().setUserAgentString("AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
here is the website that i am trying to open in my
http://groupedirectouest.com/
it works fine with chrome but webview.
Now there are custom chrome tabs for you!
The WebView is good solution if you are hosting your own content inside your app. If your app directs people to URLs outside your domain,it is now recommended that you use Chrome custom tabs for various benefits.Follow the link below:
https://developer.chrome.com/multidevice/android/customtabs
I built a web app, and I am making a wrapper to put it on the app store. I have a main page, or start page for the app, and the submit button successfully loads my webview page, and the app works well.
I wanted to add a small webview above the submit button where I could display updated news before users enter the app, such as new terms of use.
I followed examples to get the webview to load on the main page, and it works - the content is displayed, but when I add the webview, the submit button doesn't work any longer.
Eclipse throws an error "Duplicate Method OnCreate(Bundle) in type TOS, so I tried changing OnCreate to OnStart or OnCreateView for the button. It renders from the layout xml, but there is no functionality. I'm certain it is a novice syntax error, so I've just posted the Tos.java code:
package com.packagename.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tos extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
WebView TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl(someURL");
};
};
So you need only one onCreate method for each Activity not object, which is where you're thinking(I assume) that you need multiple onCreate methods.
So.. just merge the two you have...
public class Tos extends Activity {
//UI ELEMENTS
private Button button;
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl("http://www.barglance.com/assets/tos/tos.php");
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
I hope this helps. For more information about activities and what onCreate actually does and when it's called, refer to Android Docs - Activity
I would like to add a share button in an action bar of my current activity.
I don't know how to do it.
Is it possible to do this in activity ?
Here is the code of my activity :
package com.rss.utils;
import com.rss.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebBrowserViewActivity extends Activity {
WebView webview;
ProgressBar progressB = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_browser_view);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
Log.d("WebBrowserViewActivity", "URL to load : " +url);
progressB = (ProgressBar) findViewById(R.id.progressBar1);
webview = (WebView) findViewById(R.id.webViewArticle);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
// webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress < 100 && progressB.getVisibility() == ProgressBar.GONE){
progressB.setVisibility(ProgressBar.VISIBLE);
}
progressB.setProgress(progress);
if(progress == 100) {
progressB.setVisibility(ProgressBar.GONE);
}
}
});
webview.loadUrl(url);
}
}
Thanks a lot for your help.
++
I've never done that before but I did some research for you and I'm pretty sure this is what you're looking for.
This article will tell you what to do :)
In addition to what #Matthew said, this answer on question How to track the Share button clicked on the ActionBar? provides step wise details to do the same. Some of other links that may help are: How to activate "Share" button in android app? and Adding share action in ActionBar. Hope this helps.