I did a web to show different videos in HTML5,and the web works.
Now I would like to do a google tv app and show this web and work with it.
I try with a webView component but the part with javascript doesnt work and show like a plain text($function...) and the rest appear without any format.
This is my Activity:
public class Test extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vid);
mWebView = (WebView) findViewById(R.id.web);
mWebView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl("http://192.168.2.103:8080/VirtualSTBJSF/vid.xhtml");
mWebView.setWebViewClient(new verMiWeb());
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
public void onStop() {
super.onStop();
mWebView.stopLoading();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
}
return super.onKeyDown(keyCode, event);
}
}
class verMiWeb extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
and the layout:
<?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" >
<WebView
android:id="#+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Any idea?
Thanks for the answers.
Related
I'm new to Android Studio and learning and help me to short out my issues. I'm unable to add onBackpress method in my webview fragment:
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate ( R.layout.fragment_home, container, false);
WebView myWebView = myView.findViewById ( R.id.home_webView );
myWebView.loadUrl ( "https://yahoo.com" );
WebSettings webSettings = myWebView.getSettings ();
webSettings.setJavaScriptEnabled ( true );
myWebView.setWebViewClient(new WebViewClient());
// Inflate the layout for this fragment
return myView;
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else super.onBackPressed ();
}
}
}
This code is not working and my paystack payment gateway is not showing, though it displays the spinner but will not show the form for card input.
Please I need a complete code that will help me get my job done:
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(WebpageActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setSupportZoom(true);
newWebView.getSettings().setBuiltInZoomControls(true);
newWebView.getSettings().setPluginState(PluginState.ON);
newWebView.getSettings().setSupportMultipleWindows(true);
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
return true;
}
}
});
Well, this code should work when pasted on your MainActivity file of your android webview project.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.net.Uri;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
public class MainActivity extends Activity {
private WebView webView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.setInitialScale(1);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://www.your_url.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("your_url") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
}
Now, after pasting the above code you need to use the PHP paystack integration this time and not the javascript paystack integration initialization and callback functions.
At this point you only need to worry about your sessions.
Tested And Confirmed.
Hello guys im trying to do Tablayout in my project on android studio by following this https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout (doing it up to the "Add Icons to TabLayout") Recently, but somehow my icon wont appear, already added style but still nothing appear, anyone know why?
Thanks
*note : i also try to create new project and copy the code exactly but the result is the same
on activity :
public class SlidingTabsActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sliding_tabs);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
//set icon to tablayout
tabLayout.getTabAt(0).setIcon(R.drawable.clock);
tabLayout.getTabAt(1).setIcon(R.drawable.clock);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
private class CustomAdapter extends FragmentPagerAdapter {
private String viewfragment [] = {"slidingtab1","slidingtab2"};
private Context Mycontext;
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
this.Mycontext = applicationContext;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SlidingTab1_Activity();
case 1:
return new SlidingTab2_Activity();
default:
return null;
}
}
#Override
public int getCount() {
return viewfragment.length;
}
}
}
On The xml :
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
android:background="#cdc36e"
app:tabMode="fixed"
app:tabGravity="fill"
style="#style/MyCustomTabLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Style
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
Some type of Urls are not loading in my app in Webview but it can be loaded in device browser. Following is the example Url which is not working in my code-
"http://apps.takeyourapp.com/testApp/staging/index.html"
package com.example.webviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
WebView webViewPlaceholder;
String URL = "http://apps.takeyourapp.com/testApp/staging/index.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webViewPlaceholder = (WebView) findViewById(R.id.webholder);
webViewPlaceholder.getSettings().setJavaScriptEnabled(true);
webViewPlaceholder
.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webViewPlaceholder.setScrollbarFadingEnabled(true);
webViewPlaceholder.getSettings().setLoadsImagesAutomatically(true);
webViewPlaceholder.getSettings().setUseWideViewPort(true);
webViewPlaceholder.loadUrl(URL);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create WebViewClient and load url like
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
And setWebViewClient like:
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
And also add INTERNET permission into manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Try this solution.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
setContentView(mWebview );
}
}
I have an Android app, running a WebView that loads a certain page, and also part of the app
I want to generate one of button onclick() event inside the WebView page
How to load JavaScript file to WebView page which is inside the Android assets?
Thanks.
Finally I found the answer...
webView.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
here is the complete source code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Huddle extends Activity {
private WebView wv;
EditText editText;
TextView textView;
Button button,buttonClick;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.titlebar);
textView = (TextView)findViewById(R.id.txt_html);
button = (Button)findViewById(R.id.button);
buttonClick = (Button)findViewById(R.id.buttonClick);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
wv.setWebChromeClient(new MyChromeClient());
wv.loadUrl("file:///android_asset/www/index.html");
wv.requestFocus();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String txt = editText.getText().toString();
wv.loadUrl("javascript:sayHelloFromJS('"+txt+"')");
}
});
buttonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
wv.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
}
});
}
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void showHTML(final String html)
{
runOnUiThread(new Runnable() {
#Override
public void run() {
Huddle.this.textView.setText(html);
}
});
}
}
class MyChromeClient extends WebChromeClient{
}
class MyWebClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
}
index.html
add this index.html to assets\www\index.html
<html>
<script language="JavaScript">
function sayHelloFromJS(value) {
document.getElementById("namefromAndroid").value = value;
}
function setHtml(){
var HTML = ""+document.getElementById("namefromjs").value;
window.HTMLOUT.showHTML(HTML);
}
function doAlert() {
alert("JavaScript says: Hello Android !!! How are you?");
}
</script>
</head>
<body>
<p> Enter your name here <input type="text" id="namefromjs" />
<p> <input type="button" onclick= "setHtml()" value="Set Name in Android">
<p> Name from Android is <input type="text" id="namefromAndroid" />
<p> Button Click <input type="button" value="Click me" id="buttonClick" onclick= "doAlert()" />
</body>
</html>
res\layouts\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="184dp"
android:layout_height="22dp"
android:id="#+id/txt_html" android:layout_gravity="left|center_vertical"/>
<EditText
android:layout_width="161dp"
android:layout_height="wrap_content"
android:id="#+id/titlebar" android:layout_gravity="left|center_vertical"/>
<Button
android:layout_width="129dp"
android:layout_height="wrap_content"
android:text="Add text to web"
android:id="#+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Event"
android:id="#+id/buttonClick" android:layout_gravity="left|center_vertical"/>
<WebView android:id="#+id/webview" android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
this is for kotlin. its working for java add (;) line end
wv.loadUrl("javascript:(function() {document.querySelectorAll('.class')[0].click();})()")