Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Last active September 16, 2019 13:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JakeSteam/0ee444726efb9776bcd5046ad66f9006 to your computer and use it in GitHub Desktop.
Save JakeSteam/0ee444726efb9776bcd5046ad66f9006 to your computer and use it in GitHub Desktop.
"Custom Alert Dialog With Dynamic Buttons" for GameDevAlgorithms.com
public class AlertDialogHelper {
private static void displayAlertDialog(Context context, String title, String body, DialogAction... actions) {
LayoutInflater inflater = LayoutInflater.from(context);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
final View inflatedLayout = inflater.inflate(R.layout.custom_alert_dialog, null);
final AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setView(inflatedLayout);
((TextView)inflatedLayout.findViewById(R.id.title)).setText(title);
((TextView)inflatedLayout.findViewById(R.id.body)).setText(body);
final LinearLayout buttonContainer = (LinearLayout)inflatedLayout.findViewById(R.id.buttonContainer);
for (final DialogAction action : actions) {
TextView button = (TextView)inflater.inflate(R.layout.custom_alert_dialog_button, null);
button.setText(action.getText());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
action.getRunnable().run();
dialog.dismiss();
}
});
buttonContainer.addView(button, params);
}
dialog.show();
}
public static void outOfItems(final Activity activity, final int itemTier, final int itemType) {
displayAlertDialog(activity, activity.getString(R.string.outOfItems), activity.getString(R.string.outOfItemsLong),
new DialogAction(activity.getString(R.string.lowerBet), new Runnable() {
@Override
public void run() {
}
}),
new DialogAction(activity.getString(R.string.exit), new Runnable() {
@Override
public void run() {
activity.finish();
}
}),
new DialogAction(activity.getString(R.string.shop), new Runnable() {
@Override
public void run() {
activity.startActivity(new Intent(activity, ShopActivity.class)
.putExtra("tier", itemTier)
.putExtra("type", itemType)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
}
}));
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_tan"
style="@style/Theme.Dimmed">
<uk.co.jakelee.blacksmithslots.components.FontTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:paddingLeft="3dp"
style="@style/LargeFontSize"/>
<uk.co.jakelee.blacksmithslots.components.FontTextView
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:paddingLeft="3dp"
style="@style/FontSize"/>
<LinearLayout
android:id="@+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<uk.co.jakelee.blacksmithslots.components.FontTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
style="@style/DialogButton" />
<style name="Theme.Dimmed" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
@rohiteshd
Copy link

Hey Jake,
I was looking for a custom alert and came across your snippets. But can't figure out changes required in gradle, to use uk.co.jakelee.blacksmithslots.components.FontTextView. Help a little, please?

Cheers,
Rohitesh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment