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));
Related
I am using an library called CircularFloating to show menu in my homepage in one fragment but when i click and go to the next fragmetn it still appears to be in next fragment also. how can I remove or set visibility off when I move to next fragments.
here is my code.
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());
ImageView itemIcon1 = new ImageView(getActivity());
itemIcon1.setImageDrawable(getResources().getDrawable(R.drawable.camera_button));
SubActionButton button1 = itemBuilder.setContentView(itemIcon1).build();
ImageView itemIcon2 = new ImageView(getActivity());
itemIcon2.setImageDrawable(getResources().getDrawable(R.drawable.button_action_dark_touch));
SubActionButton button2 = itemBuilder.setContentView(itemIcon2).build();
final FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(getActivity())
.addSubActionView(button1)
.addSubActionView(button2)
.attachTo(actionButton)
.build();
itemIcon2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Fragment abt = new Feedback_Fragment();
((AppCompatActivity)getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, abt).addToBackStack(null).commit();
actionMenu.close(true);
}
});
Library I have used is
implementation 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
You can remove the visibility with
actionMenu.setVisibility(View.INVISIBLE)
or
actionMenu.setVisibility(View.GONE)
And set the visibility to visible again with
actionMenu.setVisibility(View.VISIBLE)
Without knowing exactly how this library is built, you could try playing around with the attribute
app:elevation="0". Try to set this attribute to the layout that is currently appearing on top of all fragments, if this attribute is available for that layout. If not, try setting it for the FloatingActionButtons. Hope this helps.
I'm trying to do implement a floating widget button(like the Facebook Messenger) which is displayed over the other app. The button should be able to interfere with the displayed view of the other app and capture the whole smartphone screen underneath.
The floating button is already implemented and works fine on every app. Getting the root view and printing out the bitmap of this floating view will just return the floating button image. I think the floating window cant detect the view of the apps underneath.
One possible solution could be to start the other application as a separate activity and get its view, but I am not sure how to do that. Maybe there is some easier way to achieve this?
//Creating the floating window and set its Layout parameters
mFloatViewLayoutParams = new WindowManager.LayoutParams();
mFloatViewLayoutParams.format = PixelFormat.TRANSLUCENT;
mFloatViewLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED;
mFloatViewLayoutParams.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
mFloatViewLayoutParams.gravity = Gravity.START;
mFloatViewLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatViewLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// adding the floatview to the WindowManager
LayoutInflater inflater = LayoutInflater.from(activity);
mFloatView = inflater.inflate(R.layout.float_view_layout, null);
mWindowManager.addView(mFloatView, mFloatViewLayoutParams);
...
// some clickListener to start the screenshot
ImageButton imageButton = mFloatView.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// here I want to get the view of the whole screen to
screenshot it
}
});
}
});
What I don't want: I don't want to screenshot the view of my main activity which starts the floating button.
I am really out of ideas about how to handle this challenge. Hopefully, someone has a guess!
Cheers!
I'm making this converter app and have spent some time trying to figure out how to work this scrollTo() function to scroll to the button I desire it to be focused in a Horizontal Scroll View.
I've implemented the following to my onCreate:
final HorizontalScrollView HscrollView1 = (HorizontalScrollView)findViewById(R.id.hsView1);
final HorizontalScrollView HscrollView2 = (HorizontalScrollView)findViewById(R.id.hsView2);
final Button cmBtn = (Button)findViewById(R.id.cm_id);
final Button KmBtn = (Button)findViewById(R.id.km_id);
final Button mmBtn = (Button)findViewById(R.id.mm_id);
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
After running APK, it won't scroll to mm and cm button when the app starts (unlike what I wrote in the code)
It seems so frustrating to me with this scrollview and it would be awesome if someone can help me here at 5am (est)
Thank you!
replace
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
with
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
}
}, 500);
and run your code.
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.
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.