How to control google play leaderboard log in - java

I followed a tutorial in connecting to google leaderboard but a lot of tutorials are using the old google play services library. I manage to sign in to google using this code but it appears upon starting the app. Where should I put the googleAPIclient code so that I can log in when I click a button inside the game. I use libgdx and if anyone can link a tutorial that uses a new google play services library It would help a lot. I seemed to be stuck with this one. Thank you!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new RBGame(this,this), false);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addApi(Games.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Games.SCOPE_GAMES)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Create and setup the AdMob view
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
// Add the libgdx view
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}

Easiest way to get it going is to use BaseGameUtils which you can download here (it is located in /BasicSamples/libraries/).
The Google Play Game Services documentation describes how to use it. You still don't have to use BaseGameUtils, you could just use it as a reference for coding your own. But using it seems to be the way Google wants it done - probably to provide some consistency across games :)
Personally, I don't like using the BaseGameActivity as a base for my own activities and allowing it to take over the sign-in flow completely. I just use the GameHelper class to perform the required functionality on demand.

Related

Are Native Express Ads Deprecated?

I tried implementing Native Express Ads, but for testing purposes i can't find the test ad id.
I found alot of posts which just used the test id for banner ads ca-app-pub-3940256099942544/6300978111and for some reason this does work.
Now my question is if Native Express Ads are still recommended to use as an ad type or are they deprecated and should i switch to Native Advanced Ads?
My code
LinearLayout adContainer = findViewById(R.id.adContainer);
mAdView = new NativeExpressAdView(getApplicationContext());
mAdView.setAdSize(new AdSize(320,50);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adContainer.addView(mAdView);
LinearLayout adContainer2 = findViewById(R.id.adContainer2);
mAdView2 = new NativeExpressAdView(getApplicationContext());
mAdView2.setAdSize(new AdSize(320,50);
mAdView2.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adContainer2.addView(mAdView2);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("4EF772AE9549741BE0E1FCA236C7382C").build();
mAdView.loadAd(adRequest);
mAdView2.loadAd(adRequest);
Yes you are right now google has replaced Native Express Ads with Native Advanced Ads.
Here is the official link of Google Admob documentation
https://developers.google.com/admob/android/native/advanced

Ressource conflict/not found with flavors in android studio

i am playing around with flavors in Android studio, i made one demo and one full flavor, the sole difference beeing the demo version having a copy of the activity_main.xml layout with an admod view in it,
the full version has not....
i read that having separate java class files is a bad idea, so i integrated the admob stuff in the main/.../src/java/.../MainActivity class:
if("free".equals(BuildConfig.FLAVOR))
{
Log.d(TAG, "flavor: " + BuildConfig.FLAVOR);
MobileAds.initialize(this, ADMOB_APP_ID);
AdView mAdView;
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("151A597D874BD0B8D69D5D5E5B18B0E8")
.build();
mAdView.loadAd(adRequest);
}
and building the demo version goes flawlessly....
but, since the admob view id doesn't exist in the full version, well it doesn't build anymore....
quitting with a
Error:(204, 34) error: cannot find symbol variable adView
so i am a bit at a loss how to solve that... all the examples about flavors i read through, adress the problem generally, but for solving specifically that sort of problem i couldn't find anything....
You must to use only one xml file (activity_main.xml) and use
AdView mAdView;
mAdView = findViewById(R.id.adView);
if("free".equals(BuildConfig.FLAVOR))
{
mAdView.setVisibility(View.VISIBLE);
mAdView.loadAd(adRequest);
}else{
mAdView.setVisibility(View.GONE);
}

Disabling AdMob Ads in certain parts of app

My App, (OpenGL ES 2.0) currently has 55 classes, and one activity single.
I have my own custom Scene Manager which I use to change between different Scenes (ie, different scenes would be: Main Menu, Level Select, Level 1, Level 2....Level 20, Game Over etc...)
So I set all of my AdMob stuff up in my Activity's onCreate(); method. So at the moment, the same ad runs throughout the whole app.
How it is possible to 'switch off' the ads on different Scenes?
Bear in mind that none of my classes have direct access to the Activity class itself.
Any suggestions would be appreciated as I'm finding using AdMob extremely challenging, it doesn't seem very intuitive so any AdMob experts out there, your opinions would be appreciated!
I should point out that I don't use XML, everything is done programmatically.
Thanks
Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// 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(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Work out values for resizing screen while keeping aspect ratio
width = (int) Math.min(width, height * 1.702127659574468);
height = (int) Math.min(height, width / 1.702127659574468);
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//Set the colour if we don't, the ad won't show (bug?)
adView.setBackgroundColor(Color.BLACK);
layout.addView(myView);
layout.addView(adView, adParams);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
setContentView(layout);
}
First of all you're causing yourself a lot of pain by doing your layout programmatically (I gave a talk on this just last night). You should really consider understanding and using the XML layout.
But to answer your question, you will need to find a way for the classes that want to control whether an ad is shown or not to have some method of sending a message to the Activity. I would suggest injecting them with an interface that is implemented by an inner classes in your Activity.
The interface is going to need to look something like:
- startShowingAds - this should start requesting ads and make the AdView visible.
- stopShowingAds - this should pause ad showing and hide the AdView.
To temporarily hide the ads you just need to hide the AdView, eventually calling pause() even though I think that hidden ad views are not refreshed (you can check in the logs).
About how to reach the AdView object from your code, it depends on what you have accessible from there. If you just have the View where you are drawing a solution could be to store the AdView object in the View's tag (see setTag() and getTag() here: https://developer.android.com/reference/android/view/View.html#getTag%28int%29).

android java layout + source code confusion

I'm a new to android programming. I'm having issues with the layout in my activity. My menu appears like this:
and I've done all the layout work directly through the source code:
enterNameTxt.setText("Enter User Name");
enterNameTxt.setY(200);
enterNameTxt.setX(-600);
userNameTxt.setY(300);
userNameTxt.setX(100);
userNameTxt.setWidth(200);
enterSpeedTxt.setText("Enter Speed");
enterSpeedTxt.setX(-500);
enterSpeedTxt.setY(100);
userSpeedTxt.setX(-400);
userSpeedTxt.setY(700);
userSpeedTxt.setWidth(200);
configButton.setWidth(400);
configButton.setText("Back to Game");
configButton.setY(1000);
and as you can see the speed option doesn't even show up on the screen. And I keep playing with setX, setY, setWidth options but it keeps getting messy.
Is it wrong to do the layout directly through the source code?
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
I just don't understand when I use the source code and when I should use the layout...
Thanks in advance
Is it wrong to do the layout directly through the source code?
No it's not. But you are strongly advised to use xml layout because it gives you a visual presentation of what you are trying before runtime.
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
Yes. You have to create an xml file for every activity in your app.
I just don't understand when I use the source code and when I should use the layout...
You should use xml as much as you can. You should only use Java code to set layout attributes when it has to change at runtime. E.g populating a ListView with text from an database or a web service.
Use this code in you java file
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams box_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(120, 40);
ll.setOrientation(LinearLayout.VERTICAL);
EditText et1 = new EditText(MainActivity.this);
et1.setHint("Enter User name");
EditText et2 = new EditText(MainActivity.this);
et2.setHint("Enter speed");
Button btn= new Button(MainActivity.this);
btn.setText("Back to Game");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
ll.addView(et1, box_params);
ll.addView(et2, box_params);
ll.addView(btn, btn_params);
ll.setGravity(Gravity.CENTER);
rl.addView(ll,layout_params );
}
}
You will get required output but it is prefer to use xml file until you don't need dynamic changes in your UI. It is easy to maintain code and design screens using xml files and if there is no need of large dynamic changes you should use xml files. Through Xml files you can check you code on different resolution through graphical representation of code. You can create a xml file in layout folder and can link it to your activity. It is easy, time saving and provide you more accurate designs... :) Please check screen shot for dynamic creation of your required design.
Happy Coding !!!

Android Admob showing blank RocketFuel ads often

I have a working AdMob AdView on top of my opengl game. Its working great but I notice that it keeps showing a particular blank ad. I thought it was a bug on my side but then I noticed that the ad is functioning partially. The ad appears to be a RocketFuel ad according to the provided info button in the top right of the AdView. Clicking within the blank AdView area does nothing unless the info button it hit. The images and code below help explain.
I thought RocketFuel was a separate ad service, is it possible to limit its appearance in Google AdMob using the Filter Settings > Category / Type Settings > Affiliate offers? Or perhaps one of the other categories? I am going to try adding a URL Block for rocketfuel.com under the Ad Filters. However I am unsure if they are serving ads directly from their domain.
Otherwise, is there any way to block, report, or limit these types of ads from being shown through my app? Has anyone had similar problems with such ads?
Here is how I slap the adview on top of my java based OpenGL ES 2 renderer:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mView = new nopgRenderer(this);
setContentView(mView);
adView = new AdView(this, AdSize.IAB_LEADERBOARD, "<AdMobAdAppID>");
RelativeLayout layout = new RelativeLayout(this);
layout.addView(adView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(params);
this.addContentView(layout, params);
adView.loadAd(new AdRequest());
}
public static void hideAd(){
adView.setVisibility(AdView.GONE);
}
public static void showAd(){
adView.setVisibility(AdView.VISIBLE);
}
The app on Google Play:
https://play.google.com/store/apps/details?id=com.dmtsource.nopg
Example of a normal ad:
Example of the blank ad:
Clicking on the info item in the blank ad:

Categories

Resources