0

In my app I have a AdView object and Float button in a LinearLayout (Vertical). And I want when the user buys the in-app-purchase "Remove ads" to remove the AdView object from the xml so the FAB button take its place. Like this:

ads

no ads:

enter image description here

How can I do that ? That is my xml code:

<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>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true"
    android:background="?android:selectableItemBackground"
    android:id="@+id/recyclerView"
    android:layout_below="@+id/tool_bar"
    />

<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"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:layout_alignParentBottom="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_weight="50"
        android:layout_gravity="center"
        fab:show_animation="@anim/fab_roll_from_down"
        fab:hide_animation="@anim/fab_roll_to_down"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

This is my code that removes the adview:

mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds) {
                Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }else{

                if(mAdView != null) {
                    mAdView.setVisibility(View.GONE);
                }
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }

enter image description here

Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126

3 Answers3

2

To remove any view you can use, where ll is your parent linear layout

ll.removeView(view)// to remove particular view
ll.removeViewAt(position);// to remove view from particular position

Alternatively you can also set visibility of your adView to GONE which will in result give its place to the button.

adButton.setVisibility(View.GONE);  

EDIT:
After looking at your XML I think its weight which is creating the issue.
You should set floatingButton layout_weight to 100 where you are hiding the adView

floatingButton.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 100f));

EDIT:
I would suggest not to use weights as you don't need them.

Salmaan
  • 3,543
  • 8
  • 33
  • 59
  • I tried this but when I installed the app on a device that already had purchased the "remove ads" feature the adview was gone, but there is still space bellow the Float button. – Georgi Koemdzhiev Jun 25 '15 at 05:03
  • 1
    I think its problem with the layout_weight, you should also set the weight of floatingButton to 100 while removing adView - see http://stackoverflow.com/a/3224365/3828908 for weight – Salmaan Jun 25 '15 at 05:07
  • I still have problem, setting the layout_weight of my button to 100 :/ – Georgi Koemdzhiev Jun 25 '15 at 05:17
  • Whats the problem, code doesn't executes or what? Tried using 100.0f? – Salmaan Jun 25 '15 at 05:21
  • No, it just does not work, the space is there.Maybe I am importing the wrong class for the LayoutParams, because android studio is offering me many options. I chose android.view.ViewGroup.LayoutParams – Georgi Koemdzhiev Jun 25 '15 at 05:23
  • Goto Mobile Settings -> Developer Options -> Show layout bounds. This will draw blue rectangles on every UI component for all applications. Post your app screenshot after that. http://tysmith.me/post/27035355999/layoutbounds – Salmaan Jun 25 '15 at 05:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81486/discussion-between-salmaan-and-georgi-koemdzhiev). – Salmaan Jun 25 '15 at 05:28
  • I was able to fix the problem based on the suggestions that you made. Thanks a lot! :) – Georgi Koemdzhiev Jun 25 '15 at 15:57
1

It is not practical to add the view in xml if you want to dynamically determine whether or not to display it. If you still want to add to add to xml you can just make it invisible or detach/remove the view. Else, don't add the AdView in Xml. I handle this scenario as follows:

        adView = new MoPubView(this.getApplicationContext());
        adView.setAdUnitId(getString(R.string.mopub_banner_adunitid));
        adView.refreshDrawableState();
        adView.setBannerAdListener(this);
        if(!Prefs.getUserPreferenceBooleanValue(Prefs.Key.REMOVE_ADS_PAID, this)) {
              frameLayout.addView(adView, bannerViewLayoutParams);      
              adView.loadAd();

        }

Add the adview programatically in your code. Store a flag in shared preferences and set it as required to display or remove ads. when the activity loads, read the flag and determine whether or not to add the adview to the layout.

For the size of the button you can sent it to be relative to the available space or resize it based on the value of flag.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • That is interesting approach, I will be definitely checking it out. Thanks! – Georgi Koemdzhiev Jun 25 '15 at 05:05
  • 1
    to add to the above, You could even choose to not instantiate the object at all by putting the entire code in the if block. This will use a tiny tiny bit less memory. Not a major difference but better. I have some other reason for doing it this way. – Viral Patel Jun 25 '15 at 05:10
1

lets say youViewName is the view you want to remove. if its a LinearLayout use below code, otherwise if its a RelativeLayout replace LinearLayout in the below code by RelativeLayout

((LinearLayout) youViewName.getParent()).removeView(youViewName);

In your case the line below should work

((LinearLayout) mAdView.getParent()).removeView(mAdView);
mushahid
  • 504
  • 5
  • 21