Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.example.cwapp;
- import android.app.DatePickerDialog;
- import android.app.LoaderManager;
- import android.content.ContentValues;
- import android.content.CursorLoader;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.Loader;
- import android.content.pm.PackageManager;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.support.design.widget.FloatingActionButton;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.text.InputType;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.CursorAdapter;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.RatingBar;
- import android.widget.Toast;
- import java.text.SimpleDateFormat;
- import java.util.Locale;
- public class Places_mainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
- //create cursor
- private CursorAdapter cursorAdapter = null;
- //Capturing Image
- public static final int REQUEST_TAKE_PHOTO = 1;
- private static int RESULT_LOAD_IMG = 2;
- ImageView mImageView;
- Button Capture_Camera;
- Button Insert_Gallery;
- String imgDecodableString;
- private int year,currentYear;
- private int month,currentMonth;
- private int day,currentDay;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_places_main);
- //get image
- mImageView = (ImageView)findViewById(R.id.UploadimageView);
- Capture_Camera = (Button)findViewById(R.id.ivCamera);
- Insert_Gallery = (Button)findViewById(R.id.ivGallery);
- //checks if the camera is available
- if (!hasCamera()){
- Capture_Camera.setEnabled(false);
- Insert_Gallery.setEnabled(false);
- Toast.makeText(Places_mainActivity.this, "Error! Camera may be unavailable", Toast.LENGTH_LONG).show();
- }
- cursorAdapter = new PlacesCursorAdapter(this, null, 0);
- ListView list = (ListView) findViewById(R.id.places_main_list);
- list.setAdapter(cursorAdapter);
- getLoaderManager().initLoader(0, null, this);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- LayoutInflater li = LayoutInflater.from(Places_mainActivity.this);
- View getEmpIdView = li.inflate(R.layout.places_dialog_insert, null);
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Places_mainActivity.this);
- // set places_main_insert.xml to alertdialog builder
- alertDialogBuilder.setView(getEmpIdView);
- final EditText nameInput = (EditText) getEmpIdView.findViewById(R.id.NameeditText);
- final EditText LocationInput = (EditText) getEmpIdView.findViewById(R.id.Location_textView);
- final EditText DescInput = (EditText) getEmpIdView.findViewById(R.id.desc_textView);
- final EditText LatitudeInput = (EditText) getEmpIdView.findViewById(R.id.LatitudetextView6);
- final EditText LongitudeInput = (EditText) getEmpIdView.findViewById(R.id.LongitudetextView);
- final EditText PriceInput = (EditText) getEmpIdView.findViewById(R.id.PriceEditText);
- final RatingBar RankingInput = (RatingBar) getEmpIdView.findViewById(R.id.ratingBarInsert);
- final EditText NotesInput = (EditText) getEmpIdView.findViewById(R.id.NoteseditText);
- final EditText VisitDate = (EditText)getEmpIdView.findViewById(R.id.dateVisit); //EditText View gia imerominia
- final EditText ActualVisitDate = (EditText)getEmpIdView.findViewById(R.id.VisitedDate);
- //to show datePicker
- final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));
- year = c.get(Calendar.YEAR);
- month = c.get(Calendar.MONTH);
- day = c.get(Calendar.DAY_OF_MONTH);
- VisitDate.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
- currentDay = mDay;
- currentMonth = mMonth+1;
- currentYear = mYear;
- VisitDate.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
- }
- },year,month,day).show();
- }
- });
- DatePickerDialog visitDateDialog;
- DatePickerDialog ActualVisitDialog;
- SimpleDateFormat dateFormatter;
- dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.UK);
- //VisitDate.setInputType(InputType.TYPE_NULL);
- ActualVisitDate.setInputType(InputType.TYPE_NULL);
- // set dialog message
- alertDialogBuilder
- .setTitle("New Place")
- .setCancelable(true)
- .setPositiveButton("Add", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int id) {
- // get user input and set it to result
- // edit text
- //Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();
- String name = nameInput.getText().toString();
- String location = LocationInput.getText().toString();
- String desc = DescInput.getText().toString();
- String latitude = LatitudeInput.getText().toString();
- String longitude = LongitudeInput.getText().toString();
- String price = PriceInput.getText().toString();
- Double rank = Double.parseDouble(String.valueOf(RankingInput.getRating()).toString());
- Integer r = Integer.parseInt(String.valueOf(rank).replace(".0",""));
- String notes = NotesInput.getText().toString();
- Boolean isNameValid = isNameValid(name);
- Boolean isLocationValid = isLocationValid(location);
- Boolean isDescValid = isDescValid(desc);
- if (isNameValid || isLocationValid) {
- insertPlace(name,location,desc,latitude,longitude,price,r,notes);
- restartLoader();
- }
- if (isNameValid || isLocationValid || isDescValid) {
- Toast.makeText(getApplicationContext(), "Please check Name and Location. They cannot be null", Toast.LENGTH_SHORT).show();
- }
- }
- }).create().show();
- }
- });
- //If user makes an extended (long) click on a place then it can be deleted
- list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {
- final View clickedContact = view;
- AlertDialog.Builder builder = new AlertDialog.Builder(Places_mainActivity.this);
- builder.setCancelable(true)
- .setMessage("Are you sure you want to delete this place?")
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- getContentResolver().delete(DetailsProvider.CONTENT_URI, SQLHelper.COL_ID + " = " + id, null);
- Toast.makeText(getApplicationContext(),"Place Deleted",Toast.LENGTH_SHORT).show(); }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .show();
- Toast.makeText(getApplicationContext(), view.toString(), Toast.LENGTH_SHORT).show();
- return false;
- }
- });
- }
- //Checks if mandatory fields are empty
- private boolean isNameValid(String name) {
- return !name.isEmpty();
- }
- private boolean isLocationValid (String location) {return !location.isEmpty();}
- private boolean isDescValid (String location) {return !location.isEmpty();}
- //Insert New Place into db
- private void insertPlace(String placeName, String placeLocation, String placeDesc, String placeLatitude,
- String placeLongitude, String placePrice/*, Drawable photo*/, int placeRanking, String placeNotes) {
- ContentValues values = new ContentValues();
- values.put(SQLHelper.COL_NAME,placeName);
- values.put(SQLHelper.COL_LOCATION,placeLocation);
- values.put(SQLHelper.COL_DESC,placeDesc);
- values.put(SQLHelper.COL_LAT,placeLatitude);
- values.put(SQLHelper.COL_LONG,placeLongitude);
- values.put(SQLHelper.COL_PRICE,placePrice);
- values.put(SQLHelper.COL_RANK,placeRanking);
- values.put(SQLHelper.COL_NOTES,placeNotes);
- // Uri placeUri = getContentResolver().insert(DetailsProvider.CONTENT_URI,values);
- Toast.makeText(this,"Created Place " + placeName,Toast.LENGTH_LONG).show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- private void restartLoader() {
- getLoaderManager().restartLoader(0, null, this);
- }
- @Override
- public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
- return new CursorLoader(this, DetailsProvider.CONTENT_URI,null,null,null,null);
- }
- @Override
- public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
- cursorAdapter.swapCursor(cursor);
- }
- @Override
- public void onLoaderReset(Loader<Cursor> loader) {
- cursorAdapter.swapCursor(null);
- }
- //check if camera is available
- public boolean hasCamera(){
- return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
- }
- //Open Camera
- public void launchCamera (View v){
- Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(i, REQUEST_TAKE_PHOTO);
- }
- //Take foto and set it to mImageView
- //linked with button OnClick
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- try {
- //Opens camera
- if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
- {
- Bundle extras = data.getExtras();
- Bitmap photo = (Bitmap) extras.get("data");
- mImageView.setImageBitmap(photo);
- }
- // When an Image is picked
- if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
- && null != data) {
- // Get the Image from data
- Uri selectedImage = data.getData();
- String[] filePathColumn = { MediaStore.Images.Media.DATA };
- // Get the cursor
- Cursor cursor = getContentResolver().query(selectedImage,
- filePathColumn, null, null, null);
- // Move to first row
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
- imgDecodableString = cursor.getString(columnIndex);
- cursor.close();
- // Set the Image in ImageView after decoding the String
- mImageView.setImageBitmap(BitmapFactory
- .decodeFile(imgDecodableString));
- } else {
- Toast.makeText(this, "You haven't picked Image",
- Toast.LENGTH_LONG).show();
- }
- } catch (Exception e) {
- Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
- .show();
- }
- }
- //Open Gallery
- public void loadImagefromGallery(View view) {
- // Create intent to Open Image applications like Gallery, Google Photos
- Intent galleryIntent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- // Start the Intent
- startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment