I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at mainactivity it goes to second activity but it shows nothing but blank.
here is my code ..
MainActivity:-
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra(link,1);
startActivity(intent);
}
});
}
}
Second activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("1");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
No offence but you must have read the documentation first .
intent.putExtra(key,value);
Key should be constant in case of getting value from the same key in other component.So the answer is your OnClick shold be like below.
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(YourActivity.this,Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
Getting url from intent in other activity.
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;// Use this url to open in web view
Related
html page doesn't open after clicking the button. I need html page to be opened in web view on button click in another activity. Pasting both the java files:
prerequisites.java:
public class Prerequisites extends AppCompatActivity implements View.OnClickListener{
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prerequisites);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.bowls)
{
Intent intent = new Intent(this, bowls.class);
Button btbowls=(Button)findViewById(R.id.bowls);
startActivity(intent);
finish();
}
}
}
bowls.java:
public class bowls extends AppCompatActivity {
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowls);
wv = (WebView) findViewById(R.id.bowlswebview);
wv.loadUrl("file:///android_asset/bowls.html");
}
}
#Elackya
if you add your web view in activity_prerequisites then the below code will work.
public class MainActivity extends AppCompatActivity {
public WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button2);
final String webLink = getIntent().getStringExtra("weblink");
wv = (WebView) findViewById(R.id.webView);
wv.loadUrl(webLink);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setAction("weblink");
intent.putExtra("weblink", "file:///android_asset/hello.html");
startActivity(intent);
}
});
}
}
If you add your web view in another activity, then the source code of the second activity is like below
public class bowls extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
WebView ww = findViewById(R.id.webView2);
Intent web = getIntent();
String url = web.getStringExtra("weblink");
ww.loadUrl(url);
}
}
Your main activity class should be like
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),bowls.class);
intent.setAction("weblink");
intent.putExtra("weblink", "file:///android_asset/hello.html");
startActivity(intent);
}
});
}
}
And AndroidManifest.xml should include the activity bowls
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".bowls"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar">
</activity>
It... it's just not how it work, putExtra is used to pass info to another activity, not to do what you're trying to do, it crashed because your intent make no sense, and intent is used as a package to send to another activity, there is also no intent to get
anyway, here's the fixed code
public class Prerequisites extends AppCompatActivity implements View.OnClickListener{
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prerequisites);
wv = (WebView) findViewById(R.id.bowlswebview);
}
#Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.bowls:
wv.loadUrl("file:///android_asset/bowls.html");
break;
default:
break;
}
}
}
I have two ImageButton with two different url. When I click the image, it returns an white screen.I don't know what is the problem. Any edit or suggestions are welcome.
Thanks!
main activity
public class main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1);
ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2);
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.amazon.com"));
WebView webview = new WebView(main.this);
setContentView(webview);
startActivity(intent);
}
});
flipkart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.flipkart.in"));
startActivity(intent);
}
});
}
my webview activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView) findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("");
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Do you want to load Url in your app or want to open via External Browser. anaway I'm given the snipped code for the open the url in webview as given below in your application as:
WebView mynews;
ProgressBar pb;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsreadscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pb= (ProgressBar) findViewById(R.id.progress_bar);
mynews= (WebView) findViewById(R.id.mynews);
mynews.getSettings().setJavaScriptEnabled(true);
mynews.getSettings().setDefaultFontSize(17);
mynews.getSettings().setDisplayZoomControls(true);
mynews.getSettings().setDomStorageEnabled(true);
mynews.getSettings().setLoadsImagesAutomatically(true);
mynews.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pb.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
pb.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pb.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
mynews.loadUrl("https://www.google.co.in");
this is the code I have Edited
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.putExtra("url","http://www.amazon.com");
startActivity(intent);
}
});
get the value on the MainActivity.Class onCreateMethod like below
String url=getIntent().getStringExtra("url");
mynews.loadUrl(url);
I am trying to implement basic webview. However, when I enter the url and click on a button to load it, I get a popup asking me to choose from a list of applications to load that webpage.The webpage then loads perfectly in the browser.I have added the INTERNET permission in the Manifest file. Please help me to spot the error or missing logic.
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
Please set setWebViewClient listener:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
view.loadUrl(url);
return false;
}
});
now replace this code with your code...
public class MainActivity extends Activity {
private static final String TAG = "WebViewActivity";
Button button1;
EditText et1;
String address;
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
wv1=(WebView)findViewById(R.id.webView1);
wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setJavaScriptEnabled(true);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
address=et1.getText().toString();
Log.d(TAG,"url has been stored in address");
wv1.loadUrl(address);
Log.d(TAG,"url should now be loaded in webview");
}
});
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
i have this problem : java.lang.NullPointerException
My Codes and LogCat pictures :
WebviewActivity.class
public class WebviewActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
Bundle alinan = getIntent().getExtras();
String alinmis = alinan.getString("veri");
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(alinmis);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient() {
});
MenutrActivity.class
public class MenutrActivity extends Activity{
Bundle bnd;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menutr);
bnd = new Bundle();
Button mekanlar = (Button)findViewById(R.id.mekanlar);
final Intent i = new Intent(getApplicationContext(), WebviewActivity.class);
mekanlar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("android.intent.action.WEBK"));
String myString = "http://www.google.com.tr";
bnd.putString("veri", myString);
i.putExtras(bnd);
startActivity(i);
}
}); }}
application fails when I press the button.
LogCat error : java.lang.NullPointerException in WebviewActivity.class
First of all change "StartActivity(new Intent...." to "Intent i = new Intent(...". You call the activity before putting extra.
Next time just copy your code and paste as a code. This way it is very hard to see...
i have a problem with loading a webview from a string url.
here is the code of an activity with only WebView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Intent intent=getIntent();
String url=intent.getStringExtra("url");
//EditText edit=(EditText)findViewById(R.id.editText1);
//String url=edit.getText().toString();
WebView webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient());
}`
and code of the calling activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGo(View v)
{
EditText edit=(EditText)findViewById(R.id.editText1);
Intent intent=new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url", edit.getText().toString());
startActivity(intent);
}
i have even added the android.permissions.INTERNET in the manifest
but still i cannot view the page with the click of the button in MainActivity.
From the comment
yes the real device has the same error with "www.google.com"
Use
http://www.google.com
Also make sure you have internet permission mentioned in manifest file
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}