Admob ad on custom Dialog - android

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!

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;
}
}
}

Related

Incoming Call Screen obstruction by an Activity (Theme Dialog) over it

I am trying to show an Activity (with Theme Dialog) over an Incoming call Screen to show some information. I am done with the that, but the problem is whenever the call comes, Activity Dialog pops up over it and it covers SLIDER ( for accepting/Rejecting calls) .
I want the Activity Dialog over Incoming call screen but still wants the user to pick/reject calls.
I did use this But now unable to finish activity(Dialog).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Warm Regards
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.notedialog);
this.setFinishOnTouchOutside(false);
initializeContent();
phone_no = getIntent().getExtras().getString("phone_no");
String note = getIntent().getExtras().getString("note");
tv_client.setText(phone_no + " is calling you");
note_mEditText.setText(note);
dialog_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.finish();
// this.setFinishOnTouchOutside(false);
System.exit(0);
}
});
No need for an Activity with dialog style.
Call this method only when you want to show the dialog.
This will create a dialog on top of your phone.
final Dialog dialog;
public void showDialog(){
dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.TOP);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.notedialog);
dialog.setCancelable(true);
dialog.show();
}
And to cancel the dialog call this method.
public void cancelDialog(){
if(dialog!= null ){
dialog.dismiss()
}
}
OR
If you insist to use an Activity dialog You can use this method to change the position of window.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
View view = getWindow().getDecorView();
WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
lp.gravity = Gravity.CENTER | Gravity.TOP
lp.x = 10;
lp.y = 10;
lp.width = 300;
lp.height = 300;
getWindowManager().updateViewLayout(view, lp);
}

Android - Unable to add window

I am using AwesomeValidation to validate all forms in my app.
It works well for every other form other than a form which has a PopupWindow when user clicks a TextView.
When I click on the PopupWindow's button which validates the form inside it, it does nothing. But after I input anything inside the EditText, it shows
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#15f1d142 is not valid; is your activity running?
Here's my code for the popup window :
View.OnClickListener phoneReinputHandler = new View.OnClickListener() {
public void onClick(View arg0) {
/*Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class);
backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout);
//backDim.setVisibility(View.VISIBLE);
startActivity(intent);*/
mainLayout = (RelativeLayout)findViewById(R.id.activity_signup_step_two_mainLayout);
backDim = (RelativeLayout)findViewById(R.id.bac_dim_layout);
backDim.setVisibility(View.VISIBLE);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_handphone, null, false),
(int)(width * .8),
(int)(height*.35),
true);
//pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pw.setOutsideTouchable(true);
RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities);
//popupFunctionalitiesWrapper.setGravity(Gravity.CENTER);
int popupFunctionalitiesWrapperWidth = layoutResize.width(70);
RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth;
popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER);
popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams);
userPhonePopup = (EditText)pw.getContentView().findViewById((R.id.popup_handphone_phoneNumber));
mAwesomeValidation.addValidation(userPhonePopup, Patterns.PHONE, "Phone number must not be empty");
userPhoneCfmPopup = (EditText)pw.getContentView().findViewById((R.id.popup_handphone_phoneNumberConfirm));
Button buttonPhoneCodeResend = (Button)pw.getContentView().findViewById(R.id.popup_handphone_phoneNumberButton);
buttonPhoneCodeResend.setOnClickListener(new View.OnClickListener() {
AwesomeValidation resendPhoneValidation = new AwesomeValidation(ValidationStyle.COLORATION);
#Override
public void onClick(View v) {
Context context = v.getContext();
if(!((Activity) context).isFinishing())
{
if (!userPhonePopup.getText().toString().equals(userPhoneCfmPopup.getText().toString())) {
resendPhoneValidation.addValidation(userPhoneCfmPopup, "/^" + userPhonePopup.getText().toString() + "$/", "Nomor handphone harus sama");
}
resendPhoneValidation.validate();
}
}
});
pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
backDim.setVisibility(View.GONE);
}
});
}
};
A problem with Context?

Android Dialog UI scrambles with keyboard

I am using a custom dialog with EditTexts and Button.
But it gets scrambled whenever the keyboard is popped up..
I have tried this to solve this
d.getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
And My dialog code is this:
final Dialog d = new Dialog(NewOrderDetrails.this);
d.setContentView(R.layout.accept_dialog);
d.setTitle("Enter Details");
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button dispatch,cal;
System.out.println("Delivery Phone" + getIntent().getStringExtra("Cphne") );
invoice=(EditText)d.findViewById(R.id.accept_invoice_no);
bill=(EditText)d.findViewById(R.id.accept_bill_amount);
discount=(EditText)d.findViewById(R.id.accept_discount);
dispatch = (Button) d.findViewById(R.id.accept_dispatch);
payblebill=(EditText)d.findViewById(R.id.accept_pay_amount);
etd=(EditText)d.findViewById(R.id.accept_etd);
cal=(Button)d.findViewById(R.id.accept_calculate);
cal.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
d.show();
d.getWindow().setAttributes(lp);
dispatch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
}
});
I tried everything i could, but the problem is still there, And believe me it's annoying.. Please help.

multiple popups in android

I have two Linear Layouts in single activity .
I want to display custom popups on each of them when clicked.
When I click on first layout, popup apperas,then on clicking on 2nd layout ,both popups are displayed. how can i have only single popup displayed at a time?
this is my code
workLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater=
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate
(R.layout.activity_your_places__work__popup, null);
updateTextView = (TextView)
popupView.findViewById(R.id.UpdateTextView);
updateTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Your_Places2Activity.this,
UpdateWorkAddressActivity.class);
startActivity(i);
}
});
deleteTextView = (TextView)
popupView.findViewById(R.id.DeleteTextView);
deleteTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
//code to delete address
}
});
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//dismiss other popup if is showing
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();}
//display popup
popupWindowWork.showAsDropDown(workLinearLayout, 0, -70);
}
});
i have done same thing on other linear layout
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
in this line, you are dismissing the popupWindowHome dialog if popupWindowHome is showing, with popupWindowHome being the NEW dialog. Move the if statement before the constructor call.
if(popupWindowHome != null && popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

How to close dialog from withing onClickListener

I already have the AlertDialog.Builder set to final.
I have a button inside of the dialog box that when clicked, makes a purchase for the user and then closes. I'm having issues with the "closes" portion. Here's the code for the button:
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100, 0.0f);
buyButton.setLayoutParams(buttonParams);
buyButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view){
if (STOCK_TO_PURCHASE.size() != 0) {
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
purchaseStock(thisPlayer);
buyStockDialog.dismiss();
} else {
buyStockDialog.dismiss();
}
}
});
The IDE is telling me I have to typecast buyStockDialog. If I typecast it though, I get a runtime error telling me I can't typecast buyStockDialog to DialogInterface.
What's the best way to handle this?
I already have a custom onBackPressed method and considered forcing the application to mimic the back button being pressed. What do you think the best solution here is?
Declaration line for AlertDialog.Builder as per request:
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
final AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
As per Second request, the full Dialog's Code and Layout:
public void buyStock(View view){
Context context = getApplicationContext();
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
final AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
//create ScrollView to hold everything
ScrollView scrollView = new ScrollView(context);
//generate content for dialog
LinearLayout dialogContainer = new LinearLayout(context);
dialogContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350, 1);
params.gravity = Gravity.CENTER;
dialogContainer.setLayoutParams(params);
dialogContainer.setPadding(15, 15, 0, 15);
dialogContainer.setBackgroundColor(Color.WHITE);
//each hotel stock options
for (int i = 0; i < hotels.size(); i++) {
Hotel testHotel = hotels.get(i);
testHotel.setPrice(200);
View stockPicker = getStockPicker(testHotel);
LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 75, 1.0f);
pickerParams.gravity = Gravity.LEFT;
stockPicker.setLayoutParams(pickerParams);
dialogContainer.addView(stockPicker);
stockPicker.setBackgroundColor(0xffffff);
}
LinearLayout scrollWrapper = new LinearLayout(context);
scrollWrapper.addView(scrollView);
scrollWrapper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300, 1.0f));
scrollView.addView(dialogContainer);
scrollView.setLayoutParams(params);
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.setBackgroundColor(Color.WHITE);
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100, 0.0f);
buyButton.setLayoutParams(buttonParams);
buyButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view){
if (STOCK_TO_PURCHASE.size() != 0) {
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
purchaseStock(thisPlayer);
buyStockDialog.dismiss();
} else {
buyStockDialog.dismiss();
}
}
});
dialogLayout.addView(scrollWrapper);
dialogLayout.addView(buyButton);
buyStockDialog.setView(dialogLayout);
buyStockDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
int numInterations = STOCK_TO_PURCHASE.size();
for (int i = 0; i < numInterations; i++) {
STOCK_TO_PURCHASE.remove(0);
}
}
});
buyStockDialog.show();
}
There is a difference between AlertDialog, which has a dismiss() function. And AlertDialog.Builder which is the builder pattern for the alert dialog. When you are about to show the dialog you call
buyStockDialog.create()
The returned value there is the one you have to call dismiss on.
You have to do something like this where you declare your dialog:
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
final Dialog dialog = builder.create();
And inside your click listener where you want to dismiss the dialog you will call this:
dialog.dismiss();

Categories

Resources