I want to set the following properties for an Android View, but in code.
This is the AdView I am trying to replicate:
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000"
android:paddingTop="5dp"
app:adSize="SMART_BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
This is the code I have so far:
mAdView = new AdView(m_Context);
// Sizes BANNER, SMART_BANNER, LARGE_BANNER
mAdView.setAdSize(AdSize.SMART_BANNER);
//mAdView.setAdUnitId(adIdBanner);
mAdView.setBackgroundColor(0xFF000000); // First byte alpha
mAdView.setPadding(0, 5, 0, 0);
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mAdView.setLayoutParams(lp);
myLayout.addView(mAdView);
// OR SHOULD IT BE THIS????
//LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.MATCH_PARENT);
//myLayout.addView(mAdView, params);
Is this correct?
What about CenterInParent?
Have I done the width and height right so the width is matchparent and the height is wrapcontent?
Try this:
int paddingTop = 5;
float density = m_Context.getResources().getDisplayMetrics().density
int padding = (int)(paddingTop * density);
mAdView = new AdView(m_Context);
// Sizes BANNER, SMART_BANNER, LARGE_BANNER
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(adIdBanner);
mAdView.setBackgroundColor(Color.parseColor("#000000"));
mAdView.setPadding(0, padding , 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mAdView.setLayoutParams(params);
myLayout.addView(mAdView);