AdView No longer has setGravity? - java

Just updated to the new Android SDK Rev 22.3 with Android 4.4 support. After that I see AdView doesn't have a setGravity method anymore.
I need this as i'm making a Unity3D plugin and can't use XML markup.
EDIT: I'm trying to align the AdView to either the BottomLeft, BottomCenter, TopLeft, ect, ect... So any other way to do it would be helpful as Java on Android isn't my typical environment I use.

I found the solution for that. I previously relied on the setGravity method. To get the same thing I do it in this way:
/// Create a AdView
AdView myAdView = new AdView(MY_ACTIVITY);
myAdView.setAdUnitId(<ADMOB ID>);
myAdView.setAdSize(AdSize.SMART_BANNER);
/// Set the initial frame
FrameLayout.LayoutParams initialFrm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
MY_ACTIVITY.addContentView(myAdView, initialFrm);
/// ....
/// Now whenever I want to position it on the bottom I do this:
myAdView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
/// To put it back on the top, do this:
myAdView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP));

Related

Overlay of type "TYPE_ACCESSIBILITY_OVERLAY" hides navigation buttons

I'm adding a view to windows manager using these layout parameters:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
The expected result is an overlay window that covers the entire screen except the system ui (navigation bar and status bar) and doesn't cover the soft keyboard.
The above parameters do that but the problem is that the navigation bar buttons are not visible while the soft keyboard is open.
Current result:
Expected result:
I've tried a couple of flags like:
WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR
and a combination of:
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
But none of them worked.
I've also gone through the documentation for system ui but most option there that involve WindowManager.LayoutParams flags are for hiding the decoration and the rest of them require a reference to the window which I don't think I can get from an accessibility service.
The problem seems to be the:
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
flag, if I remove it, the navigation buttons are visible but the overlay will cover the keyboard, it will do that even if I use something like this:
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
My question:
Is there any way to have an overlay that doesn't cover the keyboard and doesn't hide the navigation bar buttons ?
Did you try switching from PixelFormat.TRANSLUCENT to PixelFormat.TRANSPARENT? Also:
.apply {
gravity = Gravity.BOTTOM //CENTER or whatever
x = 0
y = 0
screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

Admob NativeExpressAdView only java

Any way to use NativeExpressAdView from admob without using a layout file (xml).
I use only java file to create views and am wondering on how to create a view from java file for NativeExpressAdView ?
Edit:
I tried this
NativeExpressAdView adView = new NativeExpressAdView(activity);
AdSize adSize = new AdSize(280, 80);
adView.setAdSize(adSize);
adView.setAdUnitId(getString(R.string.ad_unit_id));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, adSize.getHeightInPixels(activity));
adView.setLayoutParams(layoutParams);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
linear_layout.addView(adView);
this code give me a black empty view width:280dp, height: 80dp
Something like this should work.... First "reserve" an id for the view by creating a file /res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="nativeExpressAdId" type="id"/>
</resources>
On API versions of Android > 17 you can generate the ID on the fly, but then you lose backward compatibility.
Next add an entry to '/res/values/strings.xml`:
<string name="adUnitId">ca-app-pub-12345678901234567890etc</string>
Then in code:
NativeExpressAdView adView = new NativeExpressAdView(this);
adView.setId(R.id.nativeExpressAdId);
int height = 80; // or whatever is appropriate - make sure its >= ad minimum
// set the size to the width of the screen
adView.setAdSize(new AdSize((int) (AdSize.FULL_WIDTH, height));
adView.setAdUnitId(getString(R.string.adUnitId));
// assuming this goes in a linearlayout... you can also addRules to lp
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
// set margins manually (`.setMargins()`) or add rules with (`.addRule()`)
// or .setVisibility() etc... to lp
adView.setLayoutParams(lp);
Then use parent.addView(adView) to insert this to the parent container. This isn't exactly the code I've used (I was using a RelativeLayout), but it's pretty close, so should get you started.
This sort of code is kind of useful if you need to generate a new ad when rotating, since you can't resize an existing one. You can "borrow" the previous ID, visibility, layout, etc. from the previous one and just apply it to the new one. See here for a similar example.
Tip: Make sure to stop any running animations before removing a nativeexpressadview as this may cause crashes in older webviews like those found in kitkat.
This is pretty much simlar to other but the width and height matters also you need a AdRequest
NativeExpressAdView mNativeExpressAdView = new NativeExpressAdView(this;
mNativeExpressAdView.setLayoutParams(new NativeExpressAdView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mNativeExpressAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH, 132)); // here 132 is the medium size
mNativeExpressAdView.setAdUnitId("YOUR AD_UNIT ID");
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
//adRequestBuilder.addTestDevice("28776EC697A5120CBA87CB573E26544A"); //if needed
<parent view>.adcontainer.addView(mNativeExpressAdView);
mNativeExpressAdView.loadAd(adRequestBuilder.build());

Android Studio overlays

I want to start creating long running applications. For instance, you choose some settings and then they are used while you browsing or playing a game. For example shortcut apps. You click somewhere in the corner, and something pop ups, with list of applications you want to go to.
My problem: I don't know how to create buttons, which would be on phone screen, like triggers, when you touch, something happens. I heard it's called overlays, but I couldn't manage to find tutorials about it.
Application example: Pie Control on Google Play. https://play.google.com/store/apps/details?id=jun.ace.piecontrol
Thanks for helping me out.
use system alert window .
example
View mView = new View(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);

how can I right-align activity titles?

I'm new to android development and I need proper instructions to right-align activity titles in a min-sdk:8 android application. I can do so by enabling rtl support for my app, but that only takes effect when the user has chosen an rtl language on his/her device. is there a way to force rtl even when the device language is ltr? or any other instructions to right-align titles in ltr mode? thx.
You can set a custom view in your Toolbar like this:
(copied from How to align center the title or label in activity?)
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.RIGHT );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
You can do that^. If you were do that multiple times in your app, you can create a customToolbar class that extends Toolbar and overwrite this method and add all that code there^ (and use that custom toolbar in your xml as well)
https://developer.android.com/reference/android/widget/Toolbar.html#generateLayoutParams(android.util.AttributeSet)

How to create a system overlay in Android which allows interaction with the windows below it?

As seen in this app, I want to create an app which dims the screen by creating a shaded overlay.
The window is created, and it's partially transparent, however, I cannot get the applications below it to launch. I can click them, and see the button presses, but other apps cannot launch while mine is running.
Suggestions?
I enclosed my code below, and an example of an app which is already doing this.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_black, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
https://play.google.com/store/apps/details?id=com.haxor
This is an easy way:
Dialog dialog = new Dialog(this,android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.getWindow().setDimAmount(0);
dialog.setContentView(new EditText(this));
dialog.show();
Modified the Activity to be settings, and moved the blacking component to a service, which worked fine.

Categories