Last active
September 16, 2019 13:10
-
-
Save JakeSteam/0ee444726efb9776bcd5046ad66f9006 to your computer and use it in GitHub Desktop.
"Custom Alert Dialog With Dynamic Buttons" for GameDevAlgorithms.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
})); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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