Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

android - Admob ad on custom Dialog

I am trying to implement Admob ads on app. What I want to do is to ad a banner ad on a custom Dialog. I have tried everything but can't find the solution.

I have made a custom xml for the dialog. When adding admob on xml, it won't run. So I tried do it programmatically. But still can't make it work.

public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);

          //ad loading
          dialog.setContentView(R.layout.custom_dialog);
          RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l);
          layout.addView(ad);
          AdRequest r = new AdRequest();
          ad.loadAd(r);

          dialog.setTitle("Σχετικ? με την λ?ξη :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);



         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
     }
   }   

On a button click I display a custom Dialog. In the OnCreate method of my Activity I have made the adView

  AdView  ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx");

I get a NullPointerException at : layout.addView(ad);

Any ideas ?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem solved! I had to inflate the custom dialog interface first. The code below is the working code!

 public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
          dialog.setContentView(R.layout.custom_dialog);

          LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View main = inflater.inflate(R.layout.custom_dialog, null);
          dialog.setContentView(main);
          WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

          LinearLayout linear = (LinearLayout)main.findViewById(R.id.ad_layout);

              ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx");


        AdRequest request = new AdRequest();
        Set<String> keywords = new HashSet<String>();
        keywords.add("game");
        request.setKeywords(keywords);

          linear.addView(ad);
          ad.loadAd(request);

          dialog.setTitle("Title :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);


         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           lp.width = width;
           lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
           dialog.getWindow().setAttributes(lp);
           dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
           dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
     }
   }    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...