My app default language is Spanish (es). I have introduced a webview in XML. Prior to this change, the language is Spanish. After adding the webview, it's automatically showing English. How can I fix this issue or problem?
Thanks for helping in advance.
I already used below code too.
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString(String.valueOf(Locale.SPANISH));
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="-5dp"
tools:ignore="WebViewLayout" />
You can dynamically add web view and initialize the language again after that.
llDynemic=(LinearLayout)findViewById(R.id.test);
WebView webView = new WebView(getContext());// webview in mainactivity
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webView.setBackgroundColor(Color.TRANSPARENT);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;} a {color: #337ab7;}</style>" + newBody, "text/html", "UTF-8", null);
llDynemic.addView(webView);
// initialize the language here
and it will work
in activity have webView
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init here language again then
setContentView(R.layout.activity_terms_of_use);
After a lot of testing and following the advices from the other answers, I've finally fixed the issue.
Issues with WebView:
WebView have to be loaded dynamically in the activity. Loading from XML file via Activity#setContentView() won't work for the first time and localisation of the entire activity will break. However, subsequent launches or Activity#recreate() will work as expected.
Even if WebView is loaded dynamically, it won't work for the first time. Again, subsequent launches or Activity#recreate() will work properly.
Considering the issues above, the solution involves loading WebView dynamically and then, the immediate recreation of the activity.
ActivityWithWebView.java
public class ActivityWithWebView extends AppCompatActivity {
private WebView webView;
private static boolean firstTime = true;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_with_web_view);
setSupportActionBar(findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.setTitle(R.string.instructions);
// WebView has to be loaded dynamically to prevent in-app localisation issue.
webView = new WebView(this);
if (firstTime) {
// Recreate if loaded for the first time to prevent localisation issue.
recreate();
firstTime = false;
return;
}
LinearLayoutCompat webviewWrapper = findViewById(R.id.webview_wrapper);
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webviewWrapper.addView(webView);
// Do other works.
}
}
activity_with_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:colorBackground" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/webview_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Related
I am using video networking library - https://github.com/vimeo/vimeo-networking-java
But unable to play video in my android app. I don't know HTML iframe properly
In the official link, it shows-
Video video = ...; // obtain a video in a manner described in the Requests section
String html = video.embed != null ? video.embed.html : null;
if(html != null) {
// html is in the form "<iframe .... ></iframe>"
// display the html however you wish
}
What code I need to place here. I am unable to understand. If you know?
You donĀ“t need any library to use Vimeo player in Iframe.
Here is an example:
vimeoPlayer.java file:
public class vimeoPlayer extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vimeo_player);
myWebView=(WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL("https://vimeo.com/47412289","<iframe src=\"https://player.vimeo.com/video/47412289\" width=\"100%\" height=\"100%\" frameborder=\"0\"></iframe>","text/html", "utf-8",null);
}
}
activity_vimeo_player.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".vimeoPlayer">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml file
...
<uses-permission android:name="android.permission.INTERNET" />
...
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;
}
}
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");
}
I am creating an application in which I have to use webView and display a HTML file saved in application in assets folder. This is my mainActivity code.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
WebSettings webSettings = browser.getSettings();
browser.getSettings().setJavaScriptEnabled(true);
setContentView(browser);
}
}
and this is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Application"
android:gravity="center_horizontal">
</TextView>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/wv1"
android:layout_marginTop="3dp">
</WebView>
whin I am trying to run this application , it goes crash..
can any one please help me to find out what is the mistake i am doing.
You cannot call setContentView(browser) because browser is already a child of another view -- the LinearLayout.
If you don't need the other views, just change the layout xml file and remove them, leaving only the WebView. Otherwise, remove the setContentView(browser) line.
You're using setContentView() twice and supplying the view that already has a parent. That is why it is giving you the error saying call removeView() on the child's parent first. So remove setContentView(browser).
Your code should look like this.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
browser.getSettings().setJavaScriptEnabled(true);
}
}
I have a webpage in ASP.NET and now I am creating an Android application using Xamarin through which user can access this webpage. I am using WebView.
Problem I am facing is that the content of webpage is not fitting in WebView, probably it seems a duplicate question but I did not got a proper answer in previous posts.
My layout code is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
and MainActivity code is
namespace DsclQuery
{
[Activity (Label = "DsclQuery", MainLauncher = true, Theme = "#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
WebView webview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
webview = FindViewById<WebView> (Resource.Id.webview1);
webview.LoadUrl ("http://www.dsclsugar.com/QuerySecure");
webview.SetWebViewClient (new dsclQueryWebViewClient());
webview.Settings.JavaScriptEnabled = true;
webview.Settings.UseWideViewPort = true;
webview.Settings.BuiltInZoomControls = true;
}
}
How can I do this ?
Try This code:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("your url");
int scale = (int) (100 * webview.getScale());
webview.setInitialScale(scale);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setBuiltInZoomControls(true);
Its android native code, I think you can understand !
I hope it will useful. Thank you.