PialKanti

DateDialog

Jan 13th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. package com.android.example.cwapp;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.app.LoaderManager;
  5. import android.content.ContentValues;
  6. import android.content.CursorLoader;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.Loader;
  10. import android.content.pm.PackageManager;
  11. import android.database.Cursor;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.net.Uri;
  15. import android.os.Bundle;
  16. import android.provider.MediaStore;
  17. import android.support.design.widget.FloatingActionButton;
  18. import android.support.v7.app.AlertDialog;
  19. import android.support.v7.app.AppCompatActivity;
  20. import android.text.InputType;
  21. import android.view.LayoutInflater;
  22. import android.view.Menu;
  23. import android.view.View;
  24. import android.widget.AdapterView;
  25. import android.widget.Button;
  26. import android.widget.CursorAdapter;
  27. import android.widget.EditText;
  28. import android.widget.ImageView;
  29. import android.widget.ListView;
  30. import android.widget.RatingBar;
  31. import android.widget.Toast;
  32.  
  33. import java.text.SimpleDateFormat;
  34. import java.util.Locale;
  35.  
  36. public class Places_mainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
  37. //create cursor
  38. private CursorAdapter cursorAdapter = null;
  39.  
  40.  
  41. //Capturing Image
  42. public static final int REQUEST_TAKE_PHOTO = 1;
  43. private static int RESULT_LOAD_IMG = 2;
  44. ImageView mImageView;
  45. Button Capture_Camera;
  46. Button Insert_Gallery;
  47. String imgDecodableString;
  48.  
  49. private int year,currentYear;
  50. private int month,currentMonth;
  51. private int day,currentDay;
  52.  
  53.  
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_places_main);
  58. //get image
  59. mImageView = (ImageView)findViewById(R.id.UploadimageView);
  60. Capture_Camera = (Button)findViewById(R.id.ivCamera);
  61. Insert_Gallery = (Button)findViewById(R.id.ivGallery);
  62.  
  63. //checks if the camera is available
  64. if (!hasCamera()){
  65. Capture_Camera.setEnabled(false);
  66. Insert_Gallery.setEnabled(false);
  67. Toast.makeText(Places_mainActivity.this, "Error! Camera may be unavailable", Toast.LENGTH_LONG).show();
  68. }
  69.  
  70.  
  71.  
  72. cursorAdapter = new PlacesCursorAdapter(this, null, 0);
  73.  
  74. ListView list = (ListView) findViewById(R.id.places_main_list);
  75. list.setAdapter(cursorAdapter);
  76.  
  77. getLoaderManager().initLoader(0, null, this);
  78. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  79. fab.setOnClickListener(new View.OnClickListener() {
  80. @Override
  81. public void onClick(View view) {
  82.  
  83. LayoutInflater li = LayoutInflater.from(Places_mainActivity.this);
  84. View getEmpIdView = li.inflate(R.layout.places_dialog_insert, null);
  85.  
  86.  
  87. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Places_mainActivity.this);
  88. // set places_main_insert.xml to alertdialog builder
  89. alertDialogBuilder.setView(getEmpIdView);
  90.  
  91. final EditText nameInput = (EditText) getEmpIdView.findViewById(R.id.NameeditText);
  92. final EditText LocationInput = (EditText) getEmpIdView.findViewById(R.id.Location_textView);
  93. final EditText DescInput = (EditText) getEmpIdView.findViewById(R.id.desc_textView);
  94. final EditText LatitudeInput = (EditText) getEmpIdView.findViewById(R.id.LatitudetextView6);
  95. final EditText LongitudeInput = (EditText) getEmpIdView.findViewById(R.id.LongitudetextView);
  96. final EditText PriceInput = (EditText) getEmpIdView.findViewById(R.id.PriceEditText);
  97. final RatingBar RankingInput = (RatingBar) getEmpIdView.findViewById(R.id.ratingBarInsert);
  98. final EditText NotesInput = (EditText) getEmpIdView.findViewById(R.id.NoteseditText);
  99. final EditText VisitDate = (EditText)getEmpIdView.findViewById(R.id.dateVisit); //EditText View gia imerominia
  100. final EditText ActualVisitDate = (EditText)getEmpIdView.findViewById(R.id.VisitedDate);
  101.  
  102.  
  103. //to show datePicker
  104. final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));
  105. year = c.get(Calendar.YEAR);
  106. month = c.get(Calendar.MONTH);
  107. day = c.get(Calendar.DAY_OF_MONTH);
  108.  
  109. VisitDate.setOnClickListener(new View.OnClickListener() {
  110. @Override
  111. public void onClick(View view) {
  112. new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
  113. @Override
  114. public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
  115. currentDay = mDay;
  116. currentMonth = mMonth+1;
  117. currentYear = mYear;
  118. VisitDate.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
  119. }
  120. },year,month,day).show();
  121. }
  122. });
  123.  
  124. DatePickerDialog visitDateDialog;
  125. DatePickerDialog ActualVisitDialog;
  126. SimpleDateFormat dateFormatter;
  127.  
  128. dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.UK);
  129. //VisitDate.setInputType(InputType.TYPE_NULL);
  130. ActualVisitDate.setInputType(InputType.TYPE_NULL);
  131.  
  132.  
  133.  
  134.  
  135. // set dialog message
  136. alertDialogBuilder
  137. .setTitle("New Place")
  138. .setCancelable(true)
  139. .setPositiveButton("Add", new DialogInterface.OnClickListener() {
  140. @Override
  141. public void onClick(DialogInterface dialog, int id) {
  142. // get user input and set it to result
  143. // edit text
  144. //Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();
  145.  
  146.  
  147.  
  148.  
  149. String name = nameInput.getText().toString();
  150. String location = LocationInput.getText().toString();
  151. String desc = DescInput.getText().toString();
  152. String latitude = LatitudeInput.getText().toString();
  153. String longitude = LongitudeInput.getText().toString();
  154. String price = PriceInput.getText().toString();
  155. Double rank = Double.parseDouble(String.valueOf(RankingInput.getRating()).toString());
  156. Integer r = Integer.parseInt(String.valueOf(rank).replace(".0",""));
  157. String notes = NotesInput.getText().toString();
  158.  
  159.  
  160. Boolean isNameValid = isNameValid(name);
  161. Boolean isLocationValid = isLocationValid(location);
  162. Boolean isDescValid = isDescValid(desc);
  163. if (isNameValid || isLocationValid) {
  164. insertPlace(name,location,desc,latitude,longitude,price,r,notes);
  165. restartLoader();
  166. }
  167. if (isNameValid || isLocationValid || isDescValid) {
  168. Toast.makeText(getApplicationContext(), "Please check Name and Location. They cannot be null", Toast.LENGTH_SHORT).show();
  169. }
  170. }
  171. }).create().show();
  172. }
  173.  
  174. });
  175.  
  176.  
  177. //If user makes an extended (long) click on a place then it can be deleted
  178. list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  179. @Override
  180. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {
  181. final View clickedContact = view;
  182. AlertDialog.Builder builder = new AlertDialog.Builder(Places_mainActivity.this);
  183.  
  184. builder.setCancelable(true)
  185. .setMessage("Are you sure you want to delete this place?")
  186. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  187. @Override
  188. public void onClick(DialogInterface dialog, int which) {
  189.  
  190. getContentResolver().delete(DetailsProvider.CONTENT_URI, SQLHelper.COL_ID + " = " + id, null);
  191. Toast.makeText(getApplicationContext(),"Place Deleted",Toast.LENGTH_SHORT).show(); }
  192. })
  193. .setNegativeButton("No", new DialogInterface.OnClickListener() {
  194. @Override
  195. public void onClick(DialogInterface dialog, int which) {
  196.  
  197. }
  198. })
  199. .show();
  200. Toast.makeText(getApplicationContext(), view.toString(), Toast.LENGTH_SHORT).show();
  201. return false;
  202. }
  203. });
  204. }
  205.  
  206.  
  207.  
  208. //Checks if mandatory fields are empty
  209. private boolean isNameValid(String name) {
  210. return !name.isEmpty();
  211. }
  212. private boolean isLocationValid (String location) {return !location.isEmpty();}
  213. private boolean isDescValid (String location) {return !location.isEmpty();}
  214.  
  215.  
  216.  
  217. //Insert New Place into db
  218. private void insertPlace(String placeName, String placeLocation, String placeDesc, String placeLatitude,
  219. String placeLongitude, String placePrice/*, Drawable photo*/, int placeRanking, String placeNotes) {
  220.  
  221. ContentValues values = new ContentValues();
  222. values.put(SQLHelper.COL_NAME,placeName);
  223. values.put(SQLHelper.COL_LOCATION,placeLocation);
  224. values.put(SQLHelper.COL_DESC,placeDesc);
  225. values.put(SQLHelper.COL_LAT,placeLatitude);
  226. values.put(SQLHelper.COL_LONG,placeLongitude);
  227. values.put(SQLHelper.COL_PRICE,placePrice);
  228. values.put(SQLHelper.COL_RANK,placeRanking);
  229. values.put(SQLHelper.COL_NOTES,placeNotes);
  230.  
  231. // Uri placeUri = getContentResolver().insert(DetailsProvider.CONTENT_URI,values);
  232. Toast.makeText(this,"Created Place " + placeName,Toast.LENGTH_LONG).show();
  233. }
  234.  
  235.  
  236.  
  237.  
  238. @Override
  239. public boolean onCreateOptionsMenu(Menu menu) {
  240. // Inflate the menu; this adds items to the action bar if it is present.
  241. getMenuInflater().inflate(R.menu.menu_main, menu);
  242. return true;
  243. }
  244.  
  245. private void restartLoader() {
  246. getLoaderManager().restartLoader(0, null, this);
  247. }
  248.  
  249.  
  250. @Override
  251. public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
  252. return new CursorLoader(this, DetailsProvider.CONTENT_URI,null,null,null,null);
  253. }
  254.  
  255. @Override
  256. public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
  257. cursorAdapter.swapCursor(cursor);
  258. }
  259.  
  260. @Override
  261. public void onLoaderReset(Loader<Cursor> loader) {
  262. cursorAdapter.swapCursor(null);
  263. }
  264.  
  265. //check if camera is available
  266. public boolean hasCamera(){
  267. return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
  268. }
  269.  
  270. //Open Camera
  271. public void launchCamera (View v){
  272. Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  273. startActivityForResult(i, REQUEST_TAKE_PHOTO);
  274. }
  275.  
  276. //Take foto and set it to mImageView
  277. //linked with button OnClick
  278. @Override
  279. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  280. super.onActivityResult(requestCode, resultCode, data);
  281. try {
  282. //Opens camera
  283. if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
  284. {
  285. Bundle extras = data.getExtras();
  286. Bitmap photo = (Bitmap) extras.get("data");
  287. mImageView.setImageBitmap(photo);
  288. }
  289. // When an Image is picked
  290. if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
  291. && null != data) {
  292. // Get the Image from data
  293.  
  294. Uri selectedImage = data.getData();
  295. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  296.  
  297. // Get the cursor
  298. Cursor cursor = getContentResolver().query(selectedImage,
  299. filePathColumn, null, null, null);
  300. // Move to first row
  301. cursor.moveToFirst();
  302.  
  303. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  304. imgDecodableString = cursor.getString(columnIndex);
  305. cursor.close();
  306. // Set the Image in ImageView after decoding the String
  307. mImageView.setImageBitmap(BitmapFactory
  308. .decodeFile(imgDecodableString));
  309.  
  310. } else {
  311. Toast.makeText(this, "You haven't picked Image",
  312. Toast.LENGTH_LONG).show();
  313. }
  314. } catch (Exception e) {
  315. Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
  316. .show();
  317. }
  318.  
  319. }
  320.  
  321.  
  322. //Open Gallery
  323. public void loadImagefromGallery(View view) {
  324. // Create intent to Open Image applications like Gallery, Google Photos
  325. Intent galleryIntent = new Intent(Intent.ACTION_PICK,
  326. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  327. // Start the Intent
  328. startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
  329. }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment