I am trying to implement admob in my app, but I can't get it working.
I have a Class A, which is the main class, it extends activity.
I have a Class B, which is the class that is called when the application start. I have the following piece of code in class A to archive this:
B b = new B(this);
setContentView(B);
In class B I have a canvas with test and bitmaps. I want to put an ad on the canvas with admob, but I can't archive this. Class B:
private AdView adView;
int[] degree = { 90, 180, 270, 360 };// random graden eindposities pijl
// int width, height;
Random rand = new Random();
Typeface font;
Matrix matrix = new Matrix();// degree,x,yaxis
Region region;// region die klikbaar is om het pijl te bewegen
AlertDialog alertDialog;
LinearLayout layout;
public YesNo(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//do something
}
#Override
protected void onDraw(Canvas canvas) {
//Do something
invalidate();
}
i have tried to implement admob with this code(among other):
public void ads() {
adView = new AdView((Activity) getContext(), AdSize.BANNER,
"xxxxxxxxxxx");
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Create a linear layout
LinearLayout layout = new LinearLayout((Activity) getContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
layout.addView(adView, params);
}
I searched hours for a solution but I can't find any.
Can somebody help me in the right direction?
First of all, your AdView need to be a part of your content view. The LinearLayout you created in your ads() function. Second, you need to create an AdRequest and load the ad with that request:
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR); // get test ads on emulator.
adView.loadAd(request);
Check out the documentation for more information on how to set up your AdView.
In your ads function, you do not actually use the LinearLayout you create - you need to put that into another layout or directly into your activity, using setContentView
Related
I have a game on Libgdx and I have been trying to add AdMob banner to it.
I have 2 problems with it,
1) The banner takes like a minute to load and to be shown on the screen and it shouldn't
2) The banner is showing on every screen and I want it to show itself only on the Menu screen and not the Game Screen.
Here is the code I used:
public class AndroidLauncher extends AndroidApplication {
AdView adView ;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize(new GameMain(), config);
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 GameMain(),config);
layout.addView(gameView);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
adView.loadAd(adRequest);
setContentView(layout);
}
The banner really takes a few minutes to load, so you must to load the banner a little time before show it. About the second question, you should set false when you don't want to show the banner; and set true to show it when you want.
Concerning your first issue,
Loading an ad requires a network request which is quiet slow. You can try defining the ad in a chronological order, though I doubt this will provide a better experience:
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
// Hook it all up
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
Concerning your second issue...
There are multiple ways to solve that. The best solution is to use an interface that has the ability to show and hide ads.
Define an interface AdHandler.java in your game's core project:
public interface AdHandler {
public void showAds(boolean show);
}
Then, implement the interface in your android project.
Just follow the official libgdx guide on controlling admob ads.
Go to the topic:
" Control
This example will show you how to turn the AdMob View's visibility on and off from within your libgdx app. "
https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx .
It has all you need.
I read this and implemented it on the code but the ads aren't going away.
here is the android launcher:
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize(new GameMain(), config);
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 GameMain(),config);
layout.addView(gameView);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
adView.loadAd(adRequest);
setContentView(layout);
}
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
adView.setVisibility(View.GONE);
break;
}
}
}
};
#Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}}
and in the window i dont want the ads to show up i did this:
constructor:
public Main(Game game,AdHandler a)
{
this.game = game;
this.ad =a;
}
and the call to the function:
ad.showAds(false);
what is wrong with the code?
edit: i checked and the showads function in the androidLauncher isn't even been called i cant find the reason
I've found this solution. Please test this one:
Add this into LinearLayout
<com.google.ads.AdView
android:id="#+id/adMob1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="{AdID}"
ads:loadAdOnCreate="true"/>
Then add this into OnCreate
AdView adView = (AdView) findViewById(R.id.adMob1);
adView.loadAd(new AdRequest());
If it works, you can add your specific code.
For more information https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
I currently add images through xml with the whole R.id.x method with the following function:
public void Image(int ID, int x, int y){
ImageView iv = (ImageView) findViewById(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
}
I've written a new function to get these images on screen programmatically instead of parsing them in XML, with help from afore-mentioned topics/questions I searched and came up with this:
public void ImageRAW(int ID, int x, int y){
ImageView iv = new ImageView(c);
iv.setImageResource(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
rl.addView(iv);
}
But it did not work. I also tried adding the following line, to no avail: iv.setVisibility(View.VISIBLE);
And in regards to the variables rl and c:
private Context c;
private RelativeLayout rl;
public void SetUtilContext(Context context){
c = context;
rl = new RelativeLayout(context);
}
The above function is called in every Activity's onCreate() function and sets the UtilLib's current Context and RelativeLayout for drawing accordingly.
The function ImageRAW() is something I would like to use to replace the old Image() function, to make things easier for me. How would/could I get this working?
Try add this before your SetUtilContext():
RelativeLayout menu = findViewById(R.layout.menu);
And this at the end of your ImageRAW() method:
menu.addChild(rl);
As per Incredible's request, my onCreate function where I'm testing the functions:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
SetUtilContext(this);
Image(R.id.logo, 0, 0);
GetScreenSize();
ImageButton(R.id.start_btn
, (screenWidth/2)-(GetImageWidth(R.id.start_btn)/2)
, 48+GetImageHeight(R.id.logo));
ImageButton(R.id.options_btn
, (screenWidth/2)-(GetImageWidth(R.id.options_btn)/2)
, 96+GetImageHeight(R.id.logo)+GetImageHeight(R.id.start_btn));
ImageButton(R.id.about_btn
, (screenWidth/2)-(GetImageWidth(R.id.about_btn)/2)
, 144+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*2));
ImageButton(R.id.exit_btn
, (screenWidth/2)-(GetImageWidth(R.id.exit_btn)/2)
, 192+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*3));
PlayAudio(R.raw.theme, true);
ImageRAW(R.drawable.head, 0, GetImageHeight(R.id.logo));
}
I have followed an instruction for making new AdMob plugin for Unity. Ads show up correctly, but I have problem with bottom position. They show up at top of the screen. I have set gravity to bottom (for FrameLayout) but banner ads again show up at the top of screen.
I dont have any .xml file with LinearLayout/FrameLayout tags.
Here is all code:
public class playads {
private String adUnitID = "ca-app-pub-9578188175087140/5483276313";
private Activity activity; //Store the android main activity
private AdView adView; //The AdView we will display to the user
private LinearLayout layout; //The layout the AdView will sit on
public playads () {
activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable() {
public void run(){
adView = new AdView(activity);
adView.setAdUnitId(adUnitID);
adView.setAdSize(AdSize.BANNER);
AdRequest request = new AdRequest.Builder().build();
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
if(layout == null)
Log.d("null", "null");
{
activity.runOnUiThread(new Runnable() {
public void run(){
layout = new LinearLayout(activity);
layout.addView(adView);
//FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//p.gravity=Gravity.BOTTOM;
activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((FrameLayout.LayoutParams)layout.getLayoutParams()).gravity = Gravity.BOTTOM;
}
});
}
}
}
);
adView.loadAd(request);
}
});
}
}
I really dont know what the problem is. I spent all day to fint it, but without any solution :(
Remember that gravity sets the positioning of a view's children, while layout_gravity sets the positioning of a view within its parent. So in your case, you want to set the gravity of the LinearLayout, which can be done via member methods. You should also set the orientation.
Your run() method should look something like:
public void run(){
layout = new LinearLayout(activity);
layout.setGravity(Gravity.BOTTOM);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
activity.addContentView(layout, lllp);
}
Use setGravity in LinearLayout
Hello I use this code below to show an Ad in my game and it works fine, but I created this banner (setContentView()) in GameActivity.java. And I have gameScene.java. I wanna hide this banner sometimes. So how can I reach "adView" variable (which is in GameActivity.java) from gameScene.java?
I have one activity GameActivity.java and others scenes (gameScene, menuScene...).
I want to show the banner in my menu scene,but I also want to hide it in game scene.
#Override
#SuppressLint("NewApi")
protected void onSetContentView() {
super.onSetContentView();
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.FILL);
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP);
adView = new AdView(this);
adView.setAdUnitId("XXXXXXXXXXXXXX");
adView.setAdSize(AdSize.BANNER);
adView.setVisibility(AdView.VISIBLE);
adView.refreshDrawableState();
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, this);
final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
surfaceViewLayoutParams.gravity = Gravity.CENTER;
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
If you want to hide it, simply call adView.setVisiblity(View.GONE). If you want to show it again, call adView.setVisiblity(View.VISIBLE).
Do as Alesandro suggests.
Pass a reference to the Activity into GameScene and use that reference to call findViewById to get the AdView.
SkinWalker, I readed your posts in the AndEngine Forum and here, I had the same problem but Androidacct solved it for me. Read this post: Hide/Show AdView from Scene
I hope It helps you!
EDIT:
This is how it help me:
In your BaseGameActivity add this code:
public void showAds() {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
adView.setVisibility(View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
}
});
}
public void hideAds() {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
adView.setVisibility(View.INVISIBLE);
}
});
}
And you must to call it from your Scene with this code:
ResourcesManager.getInstance().activity.hideAds();
And I recommend you to read this tutorial: Full game tutorial - part 3. Resources Manager
Best Regards!
I have a class Drawview:
public class DrawView extends View {
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private int balID = 0; // variable to know what ball is being dragged
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3);
}
}
And my current class is:
public class Quiz1 extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.e);
AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
DrawView d=new DrawView(this);
l.addView(d);
}
}
I am trying to add that DrawView class but its not getting added as a child view of Absolutelayout of my current class view.its executing without any error but i am not able to see Drawview class objects.
And when i am doing this:
public class Quiz1 extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.e);
AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
DrawView d=new DrawView(this);
l.addView(d);
}
}
I am getting NullPointerException it means its not renderring the Drawview View.So bottom line is how to add a class extending a View to current view.
Please help me..thanx
The view probably is getting added, but from the code that you've posted it wouldn't lay out in a way where anything would be visible. By default, when you add a view to a layout in Java code like you have done, without explicitly setting any LayoutParams, it will set your view to be laid out using wrap_content for both height and width. Since (as far as we can see) you are not overriding any of View's measurement methods to tell the layout system how big the "content" inside your custom view is, the view will be added to the hierarchy with a height and width of zero.
Before adding your custom view to the layout, you should add a line to set the layout parameters to fill it's container (the parent layout), like so:
AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
DrawView d = new DrawView(this);
LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0, 0);
d.setLayoutParams(lp);
l.addView(d);
Another option is to add your custom view directly to the XML layout R.layout.e, where you can set all these parameters directly in the XML and not worry about doing it in Java code.
Final side note: AbsoluteLayout has been deprecated for a long time now, and should not be used in new applications. You should use a FrameLayout or RelativeLayout for your application, which provide just as much flexibility.
HTH