I added adview to my app programatically and I want it to be added/removed (if user make a in-app purchase) bellow my recycler view. I intentionally put mu Recycler view and adview in a Linear layout (container), which I am referencing in code and adding/removing the adview. However, when I tried it, my adview is half visible in the screen and there is space on the right of it like this:
This is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/myMainRelativeLayout"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:layout_centerInParent="true"
android:id="@+id/myAdContainer"
android:layout_below="@+id/tool_bar">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:focusable="true"
android:layout_weight="50"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:id="@+id/recyclerView"
/>
</LinearLayout>
<TextView android:id="@+id/list_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Shopping Items yet"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<com.software.shell.fab.ActionButton
android:id="@+id/buttonFloat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
fab:show_animation="@anim/fab_roll_from_down"
fab:hide_animation="@anim/fab_roll_to_down"/>
This is the code that adds the adview:
// does the user have the premium upgrade?
mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
if(!mIsRemoveAdds) {
//play ads
mAdView = new AdView(MainActivity.this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
adContainer = (LinearLayout)findViewById(R.id.myAdContainer);
//check if the adview is already added (count ==2) otherwise added
int i = adContainer.getChildCount();
if(! (i > 1)) {
adContainer.addView(mAdView, params);
}
//Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
}else{
//remove ads
if(mAdView != null) {
adContainer.removeView(mAdView);
}
//Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
}
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:layout_centerInParent="true"
android:id="@+id/myAdContainer"
android:layout_below="@+id/tool_bar">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:focusable="true"
android:layout_weight="50"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:id="@+id/recyclerView"
/>
</LinearLayout>
Remove weight and weightSum.