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 ();
}
}
}
Related
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 am calling javascript function from an activity but I am getting Uncaught ReferenceError: myFunction is not defined at null:1 error. Here is my files
MainActivity.java
package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView)this.findViewById(R.id.webView1);
WebSettings webSettings = webview.getSettings();
// Enable Javascript for interaction
webSettings.setJavaScriptEnabled(true);
// Make the zoom controls visible
webSettings.setBuiltInZoomControls(true);
// Allow for touching selecting/deselecting data series
webview.requestFocusFromTouch();
// Set the client
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
//webview.loadUrl("file:///android_asset/test.html");
/*String str = "<books>" +
"<book title=\"A Tale of Two Cities\"/>" +
"<book title=\"1984\"/>" +
"</books>";*/
webview.loadUrl("file:///android_asset/test.html");
webview.loadUrl("javascript:myFunction()");
//webview.loadUrl("javascript:myFunction("+str+")");
}
#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;
}
}
test.html
<html>
<body>
<script type="text/javascript" src="marknote.js"></script>
<script type="text/javascript">
function myFunction()
{
var str = '<books><book></book></books>';
var parser = new marknote.Parser();
var doc = parser.parse(str);
var bookEls = doc.getRootElement().getChildElements();
for (var i=0; i<bookEls.length; i++) {
var bookEl = bookEls[i];
alert("Element name is " + bookEl.getName());
}
}
</script>
</body>
</html>
I have seen this error occurred when I am using webview.loadUrl("javascript:myFunction()");. I want to pass a xml string from java and parse it into javascript. Please help me to find a solution.
You should execute the javascript when the page is loaded
webview.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webview.loadUrl("javascript:myFunction()");
}
});
The code will be executed and will find your javascript function. The way you are doing now does not wait.
This one worked just need to change parameter set up sequence
package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webview = (WebView)this.findViewById(R.id.webView1);
WebSettings webSettings = webview.getSettings();
// Enable Javascript for interaction
webSettings.setJavaScriptEnabled(true);
// Make the zoom controls visible
webSettings.setBuiltInZoomControls(true);
// Allow for touching selecting/deselecting data series
webview.requestFocusFromTouch();
// Set the client
// webview.setWebViewClient(new WebViewClient());
// webview.setWebChromeClient(new WebChromeClient());
final String str = "<books>" +
"<book title=\"A Tale of Two Cities\"/>" +
"<book title=\"1984\"/>" +
"</books>";
// webview.loadUrl("file:///android_asset/test.html");
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:myFunction('"+str+"')");
}
}
);
webview.loadUrl("file:///android_asset/test.html");
}
#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;
}
}
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.