I have programmatically created an Android TextView
on a Click event.
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_addvehicles, null))
// Add action buttons
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Dialog dialogAddVehicles = (Dialog) dialog;
etVehicleModel = (EditText) dialogAddVehicles.findViewById(R.id.etModel);
String vehicleModel = etVehicleModel.getText().toString();
SharedPreferences.Editor editor = myVehicleData.edit();
editor.putString("VehicleModel", vehicleModel);
editor.commit();
vehicleModelReturned = myVehicleData.getString("VehicleModel", "");
TextView valueTV = new TextView(AddMyVehicle.this);
linearLayout.addView(valueTV);
valueTV.setText(vehicleModelReturned);
valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
valueTV.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
valueTV.setTextColor(Color.parseColor("#333333"));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FireMissilesDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
The above code works perfectly and creates a new TextView
, But when I close the application and re-run it the TextView
would not display.
Is there any method that I could use to save the TextView
so that even if I close the Application the TextView
would remain.
You can find the whole code Here -> http://pastebin.com/0MZRmNAA