Android , Admob / Adview takes|steals focus from edittext , How to prevent?
Asked Answered
F

3

6

I am creating views and add these views to a linearlayout , These input is taken from edittext at the bottom of the screen like a messanger application. Whenever i push the 'done' button it triggers and add that message to that message linearlayout.

The problem :

When i want to put adview between those messages e.g. between every 10 message. Edittext is losing focus and that causes whole layout to scroll down.

What i want :

Edittext should not lose the focus and everytime it should be active waiting for input with keyboard open.

What i tried and did not work :

if (messageCounter % 10 == 0) {

        LinearLayout advertisedMessageLayout = new LinearLayout(this);
        advertisedMessageLayout.setOrientation(LinearLayout.VERTICAL);
        advertisedMessageLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        AdView av = new AdView(this, AdSize.BANNER, MBConstants.ADVIEW_ID);
        //remove focus for every child of adview
        for (int i = 0; i < av.getChildCount(); i++) {
            av.getChildAt(i).setFocusable(false);
            av.getChildAt(i).setFocusableInTouchMode(false);
            av.getChildAt(i).setClickable(false);

        }
        av.setFocusable(false);
        av.setFocusableInTouchMode(false);
        av.setClickable(false);
        av.setEnabled(false);

        AdRequest request = new AdRequest();
        av.loadAd(request);

        advertisedMessageLayout.addView(messageRow);
        advertisedMessageLayout.addView(av);


        return advertisedMessageLayout;

    }

Is there any possible way to prevent adview to take focus and behave like a normal view ?

Thanks.

Farnesol answered 3/2, 2013 at 9:53 Comment(2)
try to call EditText.requestFocus() after adding messages on Button clickBluetongue
sorry, i forget to mention that, i have also tried requesting focus to that edittext , but adview seems so greedy to take the focus every time !Hurleigh
H
0

To solve that problem you need to set your AdView layout params in onAdLoaded event.

@Override
public void onAdLoaded() {
    super.onAdLoaded();
    LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    adView.setLayoutParams(lay);
}
Hysterical answered 16/2, 2014 at 13:38 Comment(0)
S
0

On onCreate() set the visibility of adview to GONE

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        .........

        adView.setVisibility(View.GONE)

       ..........

    }

After that when ad is loaded set its visibility to VISIBLE

adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();

            adView.setVisibility(View.VISIBLE)
        }
    });

Hope this helps ;)

Sofiasofie answered 28/9, 2016 at 6:58 Comment(0)
G
0

Admob WebView gains focus in situations like that. To remove focus from banner view, clear focus from activity or close soft keyboard

    etAddNote.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {           
                activity.getCurrentFocus().clearFocus();

                return true;
            }
            return false;
        }
    });

or close soft keyboard

etAddNote.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                View view = activity.getCurrentFocus();
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                return true;
            }
            return false;
        }
    });
Guberniya answered 29/11, 2016 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.