Reputation: 511
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();
}
Upvotes: 0
Views: 591
Reputation: 2773
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();
Upvotes: 0
Reputation: 739
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.
Upvotes: 1