Admob integration failing thread to work properly on AndEngine based app - java

I'm building an android game powered by AndEngine game framework.
I'm using the following code to inegrate with Admob:
#Override
protected void onSetContentView() {
mRenderSurfaceView = new RenderSurfaceView(this, mEngine);
mRenderSurfaceView.applyRenderer();
setContentView(R.layout.main);
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
AdView adView = new AdView(this, AdSize.BANNER, "XXXXXXX");
adView.refreshDrawableState();
adView.setVisibility(AdView.VISIBLE);
final FrameLayout.LayoutParams adViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice(Secure.ANDROID_ID);
adView.loadAd(adRequest);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
in the game, when a ball is created it makes a fading-in animation
I made with a thread:
new Thread(new Runnable() {
public void run() {
mBody.setType(BodyType.StaticBody);
mSprite.setAlpha(0.0f);
try {
while(mSprite.getAlpha() < 1.0f) {
mSprite.setAlpha(mSprite.getAlpha() + 0.01f);
Thread.sleep(3);
}
mBody.setType(BodyType.DynamicBody);
mBody.setLinearVelocity(new Vector2(0, 10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
The problem is:
the animation works perfectly, BUT
when I'm adding the Admob code the Sprite appears for a second
and then, just disappearing.
it seems to me there is a problem between this two "chunks" of code.
but I can't put my finger on the solution or even what causing this problem to occur.
I only know that the animation is not working when the Admob code is combined in my app.
I'd like to know why and how to solve it.
thank you guys

This could be completely unrelated, but I had a problem with randomly disappearing Sprites recently, though not connected with Admob. The problem went away when I added the following line to the place where I set the Engine up:
engineOptions.getRenderOptions().disableExtensionVertexBufferObjects();

I recommend you to use an XML file to put your admin. Its cleaner, easy to use and according to my own tests it is faster than overriding onsetcontentview. In order to achieve that you need to extend LayoutGameActivity (there is also a simple version of that class. SimpleLayoutActivity if I'm not wrong)
I'll improve the answer when I get a computer.

Related

Crash when using Handler/addView in Android Studio

so basically i have 2 problems.. 1st problem is the handler and runnable..
i have an image and i want to make it move..
so i wrote this
ImageView a = findViewById(R.id.os);
final Handler handler = new Handler();
final Runnable run = new Runnable() {
#Override
public void run() {
float y = a.getY();
if(y > layout.getHeight())
y=0;
else
y+=7;
handler.postDelayed(this,3000);
a.setY(y);
}
};
handler.post(run);
when i run it, the app crashes...my layout is ConstraintLayout.
and i wrote this code in a method "onClick" so after clicking the button the image starts moving.
..
2nd Problem : i want to add alot of objects (enemies etc to make a game) but the problem if I do that using XML it will take a long time. imagine adding 200 objects and what if I wanted to repeat adding them after they get removed or changing their attributes ? i will need to define an id for all them and create objects in reference to their id's all of them ...so I needed to figure out how to add objects programmatically to the ConstraintLayout..so i Wrote this:
ArrayList<ImageView> bosses=new ArrayList<>();
ConstraintLayout d = findViewById(R.id.layout);
for(int i =0;i<200;i++) {
bosses.add(new ImageView(this));
ConstraintLayout.LayoutParams lay =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lay.startToStart= d.getId(); lay.endToStart= d.getId();
lay.topToTop = d.getId(); lay.bottomToTop=d.getId();
bosses.get(i).setLayoutParams(lay);
d.addView(bosses.get(i));
}
and it crashes too....I'm trying to learn how to move objects and how to add them programmatically so I can create games but i keep facing these crashes. if anyone could help I'll be thankful.

How to reverse recyclerView layout like a chat app with FirebaseUI FirestoreRecyclerAdapter?

I am using FirestoreRecyclerAdapter to make a chat app in java. When adding a message it works fine but I want messages to show at bottom not on top. I tried
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
Now it reverse it's position which is good but it's not automatically scrolling to bottom position. And when I add a new message I need to scroll manually to see it.
Recyclerview automatically scroll to bottom after adding below lines
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
recyclerView.smoothScrollToPosition(0);
}
});
}
}, 1000);
above solution will not work if parent layout is horizontal scroll.
Other solution is
recyclerView.scrollToPosition(0);
Both line of code work for me. you can use any of the solution
The best way to do it is use the following piece snippet whenever there is a new message. You can use a snapshot listener for that
y = (Here you need to try 2 things. My coding laptop is not with me so I cannot test and tell but either 0 should work or the chatlist.size()-1);
layoutManager.scrollToPosition(y);
OR
layoutManager.smoothScrollToPosition(y);// I do not recommend this for chat apps since it will take time to go down if there are a lot of texts below the user
This is what I used:
#Override
public void onStart() {
super.onStart ();
adapter.startListening();
recyclerView.scrollToPosition (0);
}
Also use
#Override
public void onDataChanged() {
super.onDataChanged ();
recyclerView1.smoothScrollToPosition (0);
}
in adapter
I was using
recyclerView.scrollToPosition (0);
in onCreate and adapter was starting in onStart. Now it's working

Unity android plugin - ads won't go on the bottom of screen

I am trying to make a plugin so I can use Amazon Mobile Ads in Unity, but for some reason I can not get it to stick to the bottom of the screen. It always goes to the top of the screen. The app is in Landscape mode.
Here is the plugin code.
public class AmazonAds {
public AmazonAds(){
}
public static void displayAd(final boolean test){
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run(){
AdRegistration.setAppKey("APPKEY");
AdRegistration.enableTesting(test);
AdLayout adView = new AdLayout(UnityPlayer.currentActivity);
LinearLayout layout = new LinearLayout(UnityPlayer.currentActivity.getApplicationContext());
layout.setGravity(Gravity.BOTTOM);
UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.addView(adView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
adView.loadAd(new AdTargetingOptions());
}
});
}
}
What should I try to see if it fixes it?
Thanks!
Sweet I fixed it! In case you want to know the answer, this is how you fix it.
AdRegistration.setAppKey("APPKEY");
AdRegistration.enableTesting(test);
adView = new AdLayout(UnityPlayer.currentActivity);
LinearLayout layout = new LinearLayout(UnityPlayer.currentActivity.getApplicationContext());
layout.setGravity(Gravity.BOTTOM);
UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.addView(adView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL));

Creating buttons dynamically on android

i'm trying to create buttons dynamically though code using a linear layout to set one button after the other, my code runs doesn't give any errors, nor throwing any exceptions or anything, but all i get is an empty screen. Thanks in advance.
private void runThreadCreateButton(final List<Stop> stops) {
new Thread() {
public void run() {
int i=0;
while (i++ < 1) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
for(int j=0;j<stops.size();j++)
{
Button myButton = new Button(getApplicationContext());
myButton.setText(stops.get(j).getName());
LinearLayout ll = new LinearLayout(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}
});
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}.start();
}
you create a new Layout, but you Activity is not set to show your layout. Call setContentView() on your Main Activity with your new layout.
LinearLayout ll = new LinearLayout(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
//add something like this
yourMainActivity.setContentView(ll);
Well, if you didn't add your layout to your activity this was your main problem.
However you should learn about composing your layouts in xml. This makes your code a lot more readable and maintainable. Also you don't need to worry about switching Threads all the time.
If you need to populate your views with runtime data, you should use list views and list adapters. ListViews and ListAdapters are essentials to almost every android app, so you you should really learn about them. If you want your list items to hold more complex layout you can do so by implementing your own custom list adapters.
There are also lots of performance tweaks you can do, when using list views.
For most use cases generating and managing your layout from code is not a good approach.

Robotium with libGDX finding imageButton index

I've been trying to get the Robotium tester to work for the Android project in libGDX, but got some problems.
First I want to test my games buttons (that are imageButtons), but for that I need the imageButton index in Android.
How can I get that?
My button is in the main project:
ImageButtonStyle style = new ImageButtonStyle();
style.imageUp = buttonsImagesUp;
style.imageDown = buttonsImagesDown;
ImageButton newgameButton = new ImageButton(style);
newgameButton.setPosition(425,330);
This is the button I would like to test in JUnit.
My JUnit code:
public class ButtonTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
#SuppressWarnings("deprecation")
public ButtonTest() {
super("com.me.myGame", MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void testMenu(){
solo.assertCurrentActivity("First menu selected", MainActivity.class);
solo.clickOnImageButton(index); //need index
}
}
My game is set up like this:
First there is the main Game class that handles the resources and sets the first screen to the main Menu:
#Override
public void create() {
playSound = userPref.getBoolean("playSounds");
setScreen(mainMenuScreen);
}
And after this it goes to the MainMenuScreen.js and thats were the buttons are that I want to test with JUnit
Robotium's idea of an ImageButton is very, very different from Libgdx. Robotium is expecting an image button that is defined as part of the standard Android UI infrastructure (see Android ImageButton). The Libgdx ImageButton is a Java object with an OpenGL texture reference (see Libgdx ImageButton). From the Android UI infrastructure point of view, a Libgdx app is just a full-screen OpenGL view, and has very little meta-data for Robotium to use.
You may be able to use the raw "clickOnScreen" infrastructure, but you will have to manually compute and track the location of your buttons on the screen. Additionally, I would be surprised if Robotium did not contain other dependencies on the Android UI infrastructure that are not there in a Libgdx app.

Categories