How can i set the adSize so that it matches the parent in width on all devices?
I tried setAdSize(AdSize.SMART_BANNER); but for some reason my ads don't show when i use this.
So for now i used mAdView.setAdSize(new AdSize(320,50); but not on all devices the width matches the full screen.
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);
EDIT
I tried mAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH, AdSize.AUTO_HEIGHT));
and it shows an ad with size 320*100, but there are black areas on the left and right side, so how can i set the adsize so that it matches the full screen?
Hi unfortunately we don't have that kind of customisation available for DFP ads.
They come in fixed size only i.e. 320x50, 320x100, 320x250 you can't add padding to them can't stretch them. If you do they'll not appear.
Related
I am making an app similar to facebook where i have a news feed which shows all the posts. However, the number of post in this feed varies with each user, depending on the amount of friends they have and how many posts they have made. I want to create a CardView for every post item which will show the name of the user who posted it, the time it was posted and the actual post itself. The layout will be split horizontally once with the post in the bottom half and the name of the person with the time it was posted on the top half.
Currently i can only find ways to do this by altering the xml text, but i want to create these CardViews as and when i need them, in my Java code.
I know i can alter TextViews by using such functions as setText() & setTextSize(). Is there a way for me to use similar functions on a CardView which can alter the base layout.
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
CardView TVcard = new CardView(Home.this);
TextView TVposts = new TextView(Home.this);
TextView test = new TextView(Home.this);
TVcard.addView(TVposts);
TVcard.addView(test);
test.setText("test");
test.setBackgroundColor(Color.GREEN);
TVposts.setText(snapshot.getValue().toString());
TVposts.setTextColor(Color.WHITE);
TVposts.setTextSize(25);
TVposts.setBackgroundColor(Color.BLACK);
TVposts.setHeight(200);
linearLayout.addView(TVcard);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) TVcard.getLayoutParams();
params.setMargins(0, 20, 0, 0);
TVcard.setLayoutParams(params);
}
Thanks
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());
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)
I'm developing an Android app that needs to support 2.3+. I'm using HoloEverywhere as a layout library. My app runs perfectly on smartphones and tablets, but for now I'm using the same layout for all devices, so, in tablets there is a lot of blank space in a lot of layouts. So, I thinking if is possible to show this layout as a popup (AlertDialog) just on large screens.
I searched the internet for a response but every response I look isn't applying to my case. I don't want to change my layouts files (and, of course, I don't want to create new layout files).
Can anyone give a direction to solve my problem, or the unique way is to create new layouts for large screens?
You can use your own layout file as the content view of a dialog. With a custom view you can create other/bigger dialogs for your tablets. It is also possible to create a dialog from an activity. See http://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog or http://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog
You can change the theme of activity to dialog in Manifest file. See:
<activity
android:name="Activity"
android:label="Home"
android:theme="#android:style/Theme.Holo.Dialog">
</activity>
You can use this code to change the height and width of my activity in onCreate()..It provides better flexibility than specifying dialog theme
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
android.view.WindowManager.LayoutParams params =
getWindow().getAttributes();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
params.height = LayoutParams.WRAP_CONTENT;
params.width = (int) (width / 1.2); // fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
getWindow().setAttributes((android.view.WindowManager.LayoutParams)
params);
The AdSize class has getWidthInPixels() and getHeightInPixels() methods to obtain size of the ad. Although, they work correctly for BANNER, they don't work SMART_BANNER. They always return -2.
Can you please suggest me a way to fetch AdView size during run-time?
If you're using the Google Play services library, you can simply use:
int widthPixels = AdSize.SMART_BANNER.getWidthInPixels(this);
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
In the old standalone Android AdMob SDK, you have to do it the hacky way:
Disclaimer: This is the incorrect way to create an AdSize. Do NOT pass this AdSize into the AdView constructor!
Hopefully the smart banner implementation will be fixed in future versions so you don't have to do this hacky workaround. But here is how it can be done:
// This testSize should not be passed to the AdView constructor.
// Always pass AdSize.SMART_BANNER instead.
AdSize testSize = AdSize.createAdSize(AdSize.SMART_BANNER, this);
int widthPixels = testSize.getWidthInPixels(this);
int heightPixels = testSize.getHeightInPixels(this);
// testSize should not be referenced past this point.