Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ishmaelmakitla/e99cd9ce3caa969876c86d6a793c17b9 to your computer and use it in GitHub Desktop.
Save ishmaelmakitla/e99cd9ce3caa969876c86d6a793c17b9 to your computer and use it in GitHub Desktop.
/**
* This utility function is used to prompt the user to give details of the traffic incident
*/
public static void renderIncidentReportDialog(final Context appContext){
try{
final Dialog dialog = new Dialog(appContext);
dialog.setContentView(R.layout.report_creator_dialog_layout);
dialog.setTitle("Traffic Incident Report");
final EditText edtDescription = (EditText)dialog.findViewById(R.id.edtDescription);
final EditText edtLocation = (EditText)dialog.findViewById(R.id.edtLocation);
final CheckBox checkBoxLocation = (CheckBox)dialog.findViewById(R.id.chkLocation);
checkBoxLocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!checkBoxLocation.isChecked()){
//show the location description editText
edtLocation.setVisibility(View.VISIBLE);
}
else{
edtLocation.setVisibility(View.INVISIBLE);
}
}
});
//Spinner of Traffic Incident Types
spinner = (Spinner) dialog.findViewById(R.id.types_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(appContext,R.array.incident_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button dialogDismissButton = (Button) dialog.findViewById(R.id.buttonSubmit);
// if button is clicked, close the custom dialog
dialogDismissButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Location userLocation = null;
//get the values from the form
if(checkBoxLocation.isChecked()){
//get location of the user
userLocation = TrafficLightsGeofenceUtils.getInstance().getMyCurrentLocation();
if(userLocation == null){
Toast.makeText(appContext, "Unable to determine Location. Please Type Incident Location", Toast.LENGTH_LONG).show();
edtLocation.setVisibility(View.VISIBLE);
checkBoxLocation.setChecked(false);
return;
}
}
//instantiate a Traffic Report
Object selectedItem = spinner.getSelectedItem();
Log.i(TAG, "Selected Item "+selectedItem);
//all good, now check that description is not empty
if(edtDescription.getText().toString().isEmpty() && selectedItem == null){
edtDescription.setError("Incident Description Cannot Be Empty Unless You Chose One of The Incident Types..");
return;
}
if(!checkBoxLocation.isChecked() && edtLocation.getText().toString().isEmpty()){
edtLocation.setError("Location Cannot Be Empty Unless you tick the Checkbox");
return;
}
//do whatever with the values the user has entered, and then dismiss the dialog
dialog.dismiss();
}
});
dialog.show();
}catch(Exception e){
Log.e(TAG, "Error Rendering Report Prompt", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment