Android Studio - HTML Buttons in MainActivity.java - java

So beforehand I made a project with Phonegap. In the Index.html file of the project I have this Button:
<button class="button" onclick="next()">Next Question</button>
Now i've imported this project into Android Studio to get it working with Admob to load interstitial ads like this:
MainActivity.java
import org.apache.cordova.*;
public class MainActivity extends CordovaActivity{
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)){
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
loadInterstitial();
}
public void loadInterstitial(){
//Interstitial Intergration Part
AdRequest adRequest = new AdRequest.Builder().build();
//Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
//Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded(){
//call displayInterstitial() function
displayInterstitial();
}
});
}
public void displayInterstitial(){
//If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()){
interstitial.show();
}
}}
So now I want to get the loadInterstitial(); function called when the button above is pressed. Is this possible when the button is already initiated in the HTML file? if so ( or not ), can I possibly get a step in the right direction?
Thanks!

Related

Avoid Splash Screen to launch when user open app from recent screen

I wanna know how I can avoid splash screen to launch when my app is in recents.
I want the app to open the activity where user was in it before when he/she clicks back button of device and opens
app from recent screen.
This is a splash screen activity :
public class MainActivity extends AppCompatActivity {
//variables
private static int SPLASH_SCREEN = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this , Main_Page.class);
startActivity(intent);
finish();
}
},SPLASH_SCREEN);
}
In the manifest, specify for the activity:
android:noHistory="true"
I found the solution to my problem.
This happens because when I click back button of device, according to
activity lifecycle , ondestroyview() will be called. so when
I returned from recents screen , app started again.
what I've done is that in the next activity which appears after Splash
Screen , I used onBackPressed like this :
#Override
public void onBackPressed() {
moveTaskToBack(true);
}

Change element's visibility when javascript calls a function in a WebView

I'm building a simple android app based on a WebView of a website that I personally built, so I'm just making the mobile app for it that shows the mobile version of the website. The app has an adMob Ad at the top of the page that I want to hide in certain cases after the user logs in(for example because he has a different account type and ads can't be shown when he logs in). So I tried using addJavascriptInterface to my WebView and calling a function from javascript when the Ad needs to be hidden but I can't make it work. I'm new to android programming so it may be I'm just missing something fundamental but I couldn't find an answer anywhere on internet. Here's my code:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdView = (AdView) findViewById(R.id.adView);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.addJavascriptInterface(new WebAppInterface(this, mAdView), "Android");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
absoluteUrl = "myWebSiteUrl";
mWebView.loadUrl(absoluteUrl);
mWebView.setWebViewClient(new MyAppWebViewClient(this, mAdView));
}
}
WebAppInterface:
public class WebAppInterface {
Context mContext;
AdView mAdView;
/** Instantiate the interface and set the context */
WebAppInterface(Context c, AdView mAdView) {
this.mContext = c;
this.mAdView = mAdView;
}
/** Show a toast from the web page and hide the Ad */
#JavascriptInterface
public void hideAd(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
mAdView.setVisibility(View.GONE);
}
}
Javascript code in my page
//..Some variables here
var accountType = 1;
$(document).ready(function() {
if (accountType == 1) {
Android.hideAd('The Ad is now hidden');
}
//Other js functions
It works when in hideAd() I only show the toast, but when I add
mAdView.setVisibility(View.GONE);
The Ad at the top doesn't hide and all the javascript code in the page no longer works, please help me understand what I'm doing wrong.

Android - after splash screen app stuck - can open ads - only on Android 4.4+

I've made app that has splash screen, and after the app is loading the ads, the splash screen is gone and the ads is open (full screen).
it's working on android 2.3 to 4.2 (made it for APK 8), but on 4.4+ its getting stuck - it's only showing the splash screen logo but isn't moving to the ad itself (checked on galaxy 5/4, Nexus 4/5).
by the way - I have the 4.4 jar:
splash_screen_logo.java
public class splash_screen_logo extends Activity {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_logo);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("*****"); // deleted the id
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
// Begin loading your interstitial.
displayInterstitial();
interstitial.setAdListener(new AdListener() {
public void onAdLoaded(){
finish();
displayInterstitial();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
Intent intent_openStartingPage = new Intent(splash_screen_logo.this, MainActivity.class);
startActivity(intent_openStartingPage);
}
});
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
why in the Android 4.4 it's getting stuck?
You haven't provided enough information to debug your question. but you should NOT be calling interstitial.show from OnAdLoaded as this results in a very poor user experience.
Calling finish from OnAdLoaded is also not a good idea. As this means your Actiivty will be terminated when you have received an ad, potentially before your Activity or your Ad has had a chance to be displayed.

Android Banner Ads Example; Unknown Package 'R'

So I am trying to get ads to work for my first Android ap, I am following the Google developers tutorial found here: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
Thus my code looks like what it looks like in the example:
package com.google.example.gms.ads.banner;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
/**
* A simple {#link Activity} that embeds an AdView.
*/
public class BannerSample extends Activity {
/** The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "TO_BE_DISCOVERED_SHORTLY";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
#Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
However, when I compile this I get the following errors:
BannerSample.java:25: error: package R does not exist
setContentView(R.layout.activity_main);
^
BannerSample.java:34: error: package R does not exist
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
^
2 errors
I suspect the solution to my answers may be incredibly simple, but I'm not sure where these Rs are suppose to come from and the tutorial doesn't say anything about it. Help?
I am compiling using this javac command:
javac -cp ".;android.jar;google-play-services.jar" BannerSample.java
Every time you compile your Android project (if you are working on eclipse at least) the R package gets deleted and then gets rebuild. This error is pretty common and it means that you have an error in your code that stopped the building process and so also the creation of the R package.
If it worked before then try identifying at which time did it stop working and analyze the code that caused it not to compile so to find the error. Normally the error is in the XML layout file that prevented the application from being built
It may be that your problem is caused by a different reason, but in every case that that I've seen that this problem is present, the cause is always a compilation error.
Happy hunting :)
edit:
Check out this answer, it may contain just the info you need

ANDROID: Facebook and Browser Open inside my app, not in their own

this will be a bit annoying to explain, but bear with me:
So i have a simple android app with buttons, one opens a facebook page (first checks if the app is installed, and will open in that, else will open same page in browser), the other opens a website url in the browser
The problem i'm having is that, instead of opening them as seperate applications, it seems to be opening the browser/fb app inside my original app.
So if i open the fb page through my app, and click the back button, it brings me back to my app's home screen. If i click the fb button, and minimize the app, and go into the ACTUAL fb app, it hasn't changed anything there.
So my app is not actually using the standalone fb app, just calling it up inside itself.
(hope that makes sense...) here is the code i used;
//just defined button variables
button facebook, shop;
//now for the onCreate method
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook = (Button) findViewById(R.id.facebook);
shop = (Button) findViewById(R.id.shop);
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{ //left out the profile id on purpose...
Intent facebook = new Intent(Intent.ACTON_VIEW, Uri.parse("fb://profile/xxxx"));
startActivity(facebook);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.facebook.com/example")));
}
}
});
The shop button is set up the same, only without the try/catch, it will only open in the browser.
How do i make it so that it sends the request to the actual fb app/browser, instead of what it's currently doing? Any help/tips are gratefully welcome :)
Create a new layout with layout/a.xml
Open the layout file & then add a WebView, assign an ID so you can access the widget later.
<?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">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Insert Permission in your manifest file as you are accessing INTERNET in your application
<uses-permission android:name="android.permission.INTERNET" />
Inside the new Activity -->WebActivity.java
public class WebActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.facebook.com");
}
}
If you want to handle navigation inside the webView on your own
mywebview.setWebViewClient(new WebViewClient())
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
Now to redirect from your main activity to these new activity ::
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
boolean isFacebookInstalled = true;
}
catch( PackageManager.NameNotFoundException e ){
isFacebookInstalled=false;
}
if(isFacebookInstalled)
{
//start the facebook app
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);
}
else
{
Intent facebook = new Intent(getApplicationContext(),WebActivity.class);
startActivity(facebook);
}
});
Same way you can also insert for your shop button

Categories