Android - AdMob Set Ads to GONE? - android

I wanted to ask, how to implement an AdListner for Admob. I want the Ad to disapear if its clicked.
I tried this but it did not help.
final AdView ad1 = (AdView) findViewById(R.id.ad1);
ad1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ad1.setVisibility(View.GONE);
ad1.destroy(); }
});
thank you.

Depending on the device you are on, you may need to use that View's invalidate() method. (I would notice I needed to do this for some ad stuff on freewheel on 2.1 devices).
-- OR --
Try placing your ad1 view in a wrapper view (LinearLayout or something small) and then setting that wrapper view's visibility to View.GONE.

I think as AdView is RelativeLayout so you need to
* ad1.setClickable(true);
or
* can put entry in layout XML `android:clickable="true"`

If someone is still searching for this, here is the code I used:
public class myActivity extends Activity implements AdListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.ads2);
adView.setAdListener(this);
adView.loadAd(new AdRequest());
}
public void onDismissScreen(Ad arg0) {
RelativeLayout rellayout = (RelativeLayout) findViewById(R.id.rellayout);
AdView adView = (AdView)this.findViewById(R.id.ads2);
rellayout.removeView(adView);
}
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}

Related

Integrating of Analytics doesn´t work

I try to integrate google Analytics to my Application but it doesn´t work.
This is my Activity:
public class InhaltsverzeichnisActivity extends Activity {
private SharedPreferencesManager prefs; //added
private InterstitialAd Interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inhaltsverzeichnis);
prefs = new SharedPreferencesManager(this); //get SharedPreferencesManager instance
int t = prefs.retrieveInt("theme", 0); //get stored theme, zero is default
ThemeUtils.setTheme(t); //Set the stored theme, will default to Black
Tracker t1 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t1.setScreenName("Inhaltsverzeichnis");
t1.send(new HitBuilders.AppViewBuilder().build());
Interstitial = new InterstitialAd(this);
Interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXX/XXXXXXXXXX");
AdRequest adRequest = new AdRequest.Builder().build();
Interstitial.loadAd(adRequest);
;
Interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
Interstitial.show();
}
});
}
protected void displayInterstitial() {
// TODO Auto-generated method stub
}
public void onDismissScreen() {
// TODO Auto-generated method stub
}
public void onFailedToReceiveAd() {
// TODO Auto-generated method stub
}
public void onLeaveApplication() {
// TODO Auto-generated method stub
}
public void onPresentScreen() {
// TODO Auto-generated method stub
}
#Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStart(this);
}
#Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStop(this);
}
If I put the OnStart and OnStop method over the Interstitial Ad , Analytics works, but the Interstitial Ad goes crazy and appears every second again,
hope anyone can help me to solve this problem thanks.

how to place ads in admob in app

how to use ad-mob in app and what are the steps how i post ads in ad-mob how can i test in my device before uploading to Google play store.i am new to ad-mob. please explain me with sample example of it by step-wise.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.interstitialAds = new InterstitialAd(this, "ca-app-pub-****");
this.interstitialAds.setAdListener(this);
Button loadButton = (Button) this.findViewById(R.id.loadButton);
loadButton.setOnClickListener(loadButtonOnClick);
this.textView = (TextView) this.findViewById(R.id.stateTextView);
}
private OnClickListener loadButtonOnClick = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Loading Intertitial Ads");
AdRequest adr = new AdRequest();
// add your test device here
adr.addTestDevice(AdRequest.TEST_EMULATOR);
interstitialAds.loadAd(adr);
}
};
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
String message = "Load Ads Failed: (" + error + ")";
textView.setText(message);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
/**
* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
if (interstitialAds.isReady()) {
interstitialAds.show();
} else {
textView.setText("Interstitial ad was not ready to be shown.");
}
}
}
Get rid of your AdListener.
Call intersitial.loadAd in your constructor
Call interstitial.show at a natural break point in your app
Call interstitial.loadAd
Go to 3.
See https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial

AdListener unimplement methods android

Since I couldn't find any documentation about this methods I couldn't find an answer.
What is the purpose of these methods?
I added this AdRequest as and my logcat went crazy. Is that wrong?
#Override
public void onDismissScreen(Ad arg0) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
These are the callback methods, using these methods you can check that what is the status of the ad.
For example if the ad is not recived, onFailedToReceiveAd() will be called.

Oncreateview is not calling when i am slideing my view?

Hi I am working on sliding fragments when user tap on button are draw the view. By using this library SlidingMenu library. This library is working perfectly.But I am facing a problem in Fragment class when I am sliding a view in my fragmentactivity in fragment class there is no methods are calling What I am trying here is my code :
FragmentActivity: this activity extends SlidingFragmentActivity(This activity is in side abvoe mentioned library project)
#Override
#SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
View view1 = getLayoutInflater().inflate(
R.layout.activity_main, null);
View view2 = getLayoutInflater().inflate(R.layout.menu_frame, null);
View view3 = getLayoutInflater().inflate(R.layout.menu_frame_two, null);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSlidingMenu().setMenu(view2);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new Fragment1()).commit();
getSlidingMenu().setSecondaryMenu(view3);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame_two, new Fragment2()).commit();
setContentView(view1);
}
Fragment1.java this activity extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("oncreate view");
View v = inflater.inflate(R.layout.my_fragment_layout, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
System.out.println("on activity created");
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("oncreate");
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
System.out.println("onAttach");
}
#Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
System.out.println("on save instance state");
}
#Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
System.out.println("onHiddenChanged");
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
#Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
}
#Override
public void onViewStateRestored(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewStateRestored(savedInstanceState);
System.out.println("onViewstaterestored");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
System.out.println("onViewCreated");
}
When I am slide a view left are right side the above methods are not calling. But when I am click on menu button oncreateview is calling.
Inside on click I am calling getSlidingMenu().showMenu(); this method.
But i want to call any method when I slide on view. What was the problem in my code?
why any method is not calling inside of fragment when sliding?
Any one please help me..Thanks in advance.
Refer this tutorial. I think it's helpful for you.
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android

AdListner not working properly

I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}

Categories

Resources