Android, admob / adview takes focus with edittext, how to prevent?

I create views and add these views to linearlayout. This data is taken from the edittext at the bottom of the screen, as a messenger application. Whenever I click the done button, it starts and adds this message to this linearlayout message.

Problem:

When I want to advertise between these posts, for example. between every 10 posts. Edittext loses focus, and this causes the entire layout to scroll down.

What I want:

Edittext should not lose focus, and each time it should be active, waiting for input with the 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 the ad from watching and behave like a normal view?

Thanks.

+6
source share
3 answers

To solve this problem, you need to set the AdView layout options in the 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); } 
0
source

In onCreate () set the visibility of the ad to GONE

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ......... adView.setVisibility(View.GONE) .......... } 

After that, when the 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;)

0
source

Admob WebView gets focus in such situations. To remove focus from the banner screen, clear focus from activity or close the 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 the 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; } }); 
0
source

All Articles