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());
Related
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.
I am creating a dynamic number of editTexts and want to eventually pull the ID for each to call .getText() on the editText.
However, I noticed that it is difficult to programmatically set the ID, so I am using the .setTag() method instead:
private void createAnswerChoice(int answerNumber) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_questions_answer_layout);
EditText editText = new EditText(getActivity());
editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber));
editText.setTag(editTextID);
Toast.makeText(getActivity().getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
How would I get the value of each editText if I know the tag and not the ID? Also, what is the purpose of the .setTag() method (how does it relate to .setID()?)
create resource file(id.xml) in res/values/id.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="edittext_hello" />
</resources>
and then set,
editText.setId(R.id.edittext_hello);
findViewById() and findViewWithTag() are methods intended to obtain a reference to a View that was inflated from XML.
If you are creating the Views, you have them already then there's no need to use those methods. Keep the reference to the View somewhere.
I do not recommend using tags to get views from your hierarchy but if you must this SO post describes how to do it: https://stackoverflow.com/a/16262479/6526330.
Tags should be used when you need to cache some data in a view to grab out later when you have the view again (yes I know very generic). Some examples would be if you are using a holder in a list view or if you have a lot of views on a screen and want a global click listener vs listeners for each view. I stole both of these examples from the answers on this post which do a hell of a better job at describing the use cases than I can.
How can I set the alignParentEnd property programmatically?
I searched all over the Android Developer website, but I couldn't find any reference to setting properties like alignParentEnd in code. It is only explained how to set them in XML. Where could I find documentation for something like this in the future?
LayoutParams are used to set layout properties in code. alignParentEnd is a property of RelativeLayout so you have to use RelativeLayout.LayoutParams. You can find the documentation here.
So to set the property in code you can use the addRule() method of the RelativeLayout.LayoutParams. For example you can do this:
// Create the LayoutParams
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add all the rules you need
params.addRule(RelativeLayout.ALIGN_PARENT_END);
...
// Once you are done set the LayoutParams to the layout
relativeLayout.setLayoutParams(params);
You can find a list of all rules which you can set with addRule() here.
Parameter are added in View and ViewGroup using LayoutParam.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) my_child_view.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_END);
my_child_view.setLayoutParams(params);
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.