4

I use MoPubView for ad in my app. But when I open keyboard this banner rises and closes widgets.

enter image description here

This is code from layout:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/material_bg">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_horizontal_margin">

            ...Content here
        </LinearLayout>



    </ScrollView>

    <com.mopub.mobileads.MoPubView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/mopub_height"
        android:layout_gravity="bottom"/>
</FrameLayout>

This scheme of this layout: enter image description here

How to hide this banner or fix it at the bottom?

Artem
  • 4,569
  • 12
  • 44
  • 86

3 Answers3

2

Create a class to handle the Keyboard detection,

import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

import java.util.LinkedList;
import java.util.List;

public class SoftKeyboardStateWatcher implements ViewTreeObserver.OnGlobalLayoutListener {

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }

    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
    private final View activityRootView;
    private int        lastSoftKeyboardHeightInPx;
    private boolean    isSoftKeyboardOpened;

    public SoftKeyboardStateWatcher(View activityRootView) {
        this(activityRootView, false);
    }

    public SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) {
        this.activityRootView     = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);

        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < 100) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }

    /**
     * Default value is zero {@code 0}.
     *
     * @return last saved keyboard height in px
     */
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }

    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

In your AdActivity's onCreate method, insert these lines:

final SoftKeyboardStateWatcher softKeyboardStateWatcher = new SoftKeyboardStateWatcher(findViewById(R.id.container);
        // Add listener
        softKeyboardStateWatcher.addSoftKeyboardStateListener(new SoftKeyboardStateWatcher.SoftKeyboardStateListener() {
                @Override
                public void onSoftKeyboardOpened(int keyboardHeightInPx) {

                }

                @Override
                public void onSoftKeyboardClosed() {

                }
            });
        // then just handle callbacks
Artem
  • 4,569
  • 12
  • 44
  • 86
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
  • @Asal Imam - hi man, thanks, but - http://joxi.ru/ZrJVlnVh4jwkrj - wich id i must set here? If I have many widgets (EditText) for input - its need one keyboardlistener for each edittext? – Artem Sep 10 '15 at 09:49
  • You are not required to set SoftKeyboardStateWatcher on every widget...! You just have to set your layout's id only....Do you need example...? – Arsal Imam Sep 10 '15 at 09:55
  • 1
    @ArtemShevchenko ....! I updated the answer, now you don't need to change the id, Just use it as same..... – Arsal Imam Sep 10 '15 at 09:59
  • 1
    Did you get the desired results bro...? – Arsal Imam Sep 10 '15 at 10:20
  • I test this. Dont work - when I start Activity ad set visibility GONE even if I dont open keyboard.((( – Artem Sep 10 '15 at 10:23
  • I try this - dont work (I try use all id's - but dont work). When activity started I see toast OPEN (I add Toast.makeText(...)) in onSoftKeyboardOpened() - but then not happens ( – Artem Sep 14 '15 at 12:09
2

Resolved this problem by adding

android:windowSoftInputMode="adjustPan"

in AndroidManifest.xml

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:theme="@style/AppTheme.NoActionBar"> 
</activity>
1

Use the below code when you dont want to show the ad

 adView.destroy();
 adView.setVisibility(View.GONE);

Edit: Try using this:

  android:windowSoftInputMode="adjustPan";

in your Manifest for that Activity

Ashish Shukla
  • 1,027
  • 16
  • 36