Using the dialog - maltaisn/calcdialoglib GitHub Wiki
Here's an example activity that shows a calculator dialog on a button click.
public class MainActivity extends AppCompatActivity implements CalcDialog.CalcDialogCallback {
@Nullable
private BigDecimal value = null;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_main);
final CalcDialog calcDialog = new CalcDialog();
// Open dialog button
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcDialog.getSettings().setInitialValue(value);
calcDialog.show(getSupportFragmentManager(), "calc_dialog");
}
});
}
@Override
public void onValueEntered(int requestCode, @Nullable BigDecimal value) {
// if (requestCode == CALC_REQUEST_CODE) {} <-- If there were many dialogs, this would be used
// The calculator dialog returned a value
this.value = value;
}
}
To show the picker, you call these two methods:
dialog.getSettings().setInitialValue(BigDecimal value)
: Set the initial value to show. You can setnull
to initialize the dialog with no value and 0 will be shown. You must call this method every time the dialog is shown because the initial value is reset when the dialog is dismissed.picker.show(FragmentManager fm, String tag)
: Shows the DialogFragment. To show the dialog in another fragment, usegetChildFragmentManager()
.
The result is returned in the CalcDialogCallback
in method onValueEntered(int requestCode, BigDecimal value)
.
Calculator settings
Many settings are available for the dialog through CalcDialog.getSettings()
, see Calculator settings.