Setting up text view text in java file onLoadResource Android Studio? - android

I am trying to set text in java file.
this is my xml code
.xml file
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
this is my java code
java file
public class account extends AppCompatActivity {
TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
textview = (TextView)findViewById(R.id.text);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public void onLoadResource(WebView view, String url) {
textview.setText("Register");
}
});
}}
issue is if i set title in oncreate it's work fine but if set in onLoadResource then it's not working.

Related

Android Webview doen't work correctly

This site works well in android browsers but it doesn't work in android webview.
I set internet permission and did all things.
Who can teach me?
public class MainActivity extends AppCompatActivity {
WebView web_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.webView);
getSupportActionBar().hide();
web_view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
web_view.getSettings().setDomStorageEnabled(true);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebViewClient(mWebViewClient);
web_view.addJavascriptInterface(new MyJavaScriptInterface(), "android");
web_view.loadUrl("http://182.72.55.182/pms/employee");
}
WebViewClient mWebViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
#JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
}
make sure that your regarding website becoming live in everywhere!
webview doesn't display any local host website
Try to put WebViewClient part inside the onCreate.

Error while loading html in webview from Assets folder in android while used multiple activities

I have two activities in my android project. Both contents WebView and load same html(which include some javascript code also) file from assets folder.
When I load WebView in first activity it works fine. But after navigating to second activity, same html file doesn't load in WebView.
Activity 1:
public class Activity_1 extends AppCompatActivity {
WebView webView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__1);
webView = (WebView) findViewById(R.id.webview1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity_1.this, Activity_2.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl("file:///android_asset/sample.html");
}
#Override
protected void onPause() {
super.onPause();
}
}
Activity 2:
public class Activity_2 extends AppCompatActivity {
WebView webView;
ProgressDialog progressDialog ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
webView = (WebView) findViewById(R.id.webview2);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
}
#Override
protected void onResume() {
super.onResume();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= 19)
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
else
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressDialog.setProgress(newProgress);
}
});
webView.loadUrl("file:///android_asset/sample.html");
progressDialog.show();
}
}
activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
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="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next"
android:textSize="22sp"/>
</LinearLayout>
activity_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
When I open second activity, I get stuck as,
I don't understand that same html is loading properly in first Activity, but not in the second.
I also tried with clear WebView cache in first activity and destroying WebView in onDestroy of activity 1.
To load html, I tried with following methods:
webView.loadUrl("file:///android_res/raw/sample.html");
OR
Access html from storing as string resource
webView.loadDataWithBaseURL(null, getResources().getString(R.string.html), "text/html", "UTF-8", null);
Edit:
I tried with uninstalling webview updates and its working fine.
Is there any problem with webview updates in android 5.0?
Please let me know what I am doing wrong?
I've done a similar project to try your code. It's working for me :S
Maybe you can try to place you file in src/main/assets and use this line
webView.loadUrl("file:///android_asset/sample.html");
But in my case is working with two options. ¿Can you provide more info about html file please?

how to surf internet from web view in android

Hey guys I want to surf internet from webview in android and i am getting the page for which i have provided url in edittext.But when click on a link inside webview then it is unable to find that page.I have provided internet permission in manifest file and taken web view in layout.
This is my main activity which needs to be corrected somewhere.Thanks in advance and help done will be greatly appreciated.
public class MainActivity extends Activity {
EditText et1;
Button btn;
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.et1);
btn=(Button) findViewById(R.id.btn);
wv=(WebView) findViewById(R.id.wv);
wv.setWebViewClient(new mybrowser());
btn.setOnClickListener(new OnClickListener() {
#SuppressLint("SetJavaScriptEnabled") #Override
public void onClick(View v)
{
String url = et1.getText().toString();
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("https://"+url);
wv.requestFocus();
}
});
}
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url1=et1.getText().toString();
view.loadUrl(url1);
return true;
}
}
}
Try to do like
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
and remove String url1=et1.getText().toString();

Android Webview shows error in emulator

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

A simple HTML file wouldn't show on Android full screen

I have tried calling
zoomIn();
setBuiltInZoomControls(true);
supportZoom(true);
etc but it just wouldn't work.
Just a hint... when I double-click on the text it does fit the screen but no wrap.
Any lead given will be deeply appreciated.
Have you added a Viewport Meta tag?
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
If the HTML body is set to 100%, the text should automatically fill the screen.
Hi try the following code :
public class Terms extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.terms);
this.mWebView = ((WebView)findViewById(R.id.mWebView));
this.mWebView.getSettings().setBuiltInZoomControls(false);
this.mWebView.getSettings().setLoadWithOverviewMode(false);
this.mWebView.getSettings().setUseWideViewPort(false);
this.mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
this.mWebView.loadUrl("file:///android_asset/Terms.html");
Button back_btn = (Button)findViewById(R.id.back);
back_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
finish();
}
});
}
#SuppressWarnings("unused")
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return(false);
}
}
}

Categories

Resources