How to create AdMob Smart Banner programmatically

Asked on September 21, 2015
I am integrating AdMob smart banner in my android application. I am using XML as follows
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
I need to achieve same by code. How to do it.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
I need to achieve same by code. How to do it.

Replied on September 21, 2015
You can do as follows.
AdView mAdView = new AdView(activity);
RelativeLayout.LayoutParams params = new RelativeLayout
.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mAdView.setLayoutParams(params);
mAdView.setId(R.id.adView);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(activity.getString(R.string.banner_ad_unit_id));
adLayout.removeView(activity.findViewById(R.id.adView));
adLayout.addView(mAdView);;
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Find the link for more detail.

Replied on September 21, 2015
Great. Thanks.