Advertisement
kitlolz012

EditRestoAct xml and java code

Nov 17th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.88 KB | Source Code | 0 0
  1. EditResto Activity Java Code :
  2.  
  3. package com.example.gypsy;
  4.  
  5. import static android.content.ContentValues.TAG;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.content.pm.PackageManager;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.provider.MediaStore;
  13. import android.util.Log;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18.  
  19. import androidx.activity.result.ActivityResultLauncher;
  20. import androidx.activity.result.contract.ActivityResultContracts;
  21. import androidx.annotation.NonNull;
  22. import androidx.appcompat.app.AppCompatActivity;
  23. import androidx.core.app.ActivityCompat;
  24. import androidx.core.content.ContextCompat;
  25. import androidx.fragment.app.FragmentManager;
  26. import androidx.fragment.app.FragmentTransaction;
  27. import androidx.viewpager2.widget.ViewPager2;
  28.  
  29. import com.google.android.gms.tasks.Task;
  30. import com.google.android.gms.tasks.Tasks;
  31. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  32. import com.google.android.material.textfield.TextInputEditText;
  33. import com.google.android.material.textfield.TextInputLayout;
  34. import com.google.firebase.auth.FirebaseAuth;
  35. import com.google.firebase.auth.FirebaseUser;
  36. import com.google.firebase.database.DataSnapshot;
  37. import com.google.firebase.database.DatabaseError;
  38. import com.google.firebase.database.DatabaseReference;
  39. import com.google.firebase.database.FirebaseDatabase;
  40. import com.google.firebase.database.Query;
  41. import com.google.firebase.database.ValueEventListener;
  42. import com.google.firebase.storage.FirebaseStorage;
  43. import com.google.firebase.storage.StorageReference;
  44. import com.google.firebase.storage.UploadTask;
  45. import java.util.ArrayList;
  46. import java.util.HashMap;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.concurrent.atomic.AtomicBoolean;
  50. import java.util.concurrent.atomic.AtomicInteger;
  51. import android.Manifest;
  52. import android.content.pm.PackageManager;
  53. import android.widget.Toast;
  54.  
  55. public class EditRestoActivity extends AppCompatActivity {
  56.     private FirebaseStorage storage;
  57.     private StorageReference storageReference;
  58.     private FirebaseDatabase database;
  59.     private DatabaseReference databaseReference;
  60.     private FirebaseAuth auth;
  61.     private FirebaseUser currentUser;
  62.     private List<Uri> imageUris = new ArrayList<>();
  63.     private Uri imageUri;
  64.     private ActivityResultLauncher<Intent> galleryLauncher;
  65.     private FloatingActionButton uploadButton;
  66.     private TextInputEditText restoNameEditText;
  67.     private TextInputEditText restoAddressEditText;
  68.     private TextInputEditText restoDescriptionEditText;
  69.     private TextInputEditText restoContactEditText;
  70.     private TextInputEditText restoEmailEditText;
  71.     private TextInputEditText cancellationPolicyEditText;
  72.     private TextInputEditText amenityEditText;
  73.     private TextInputEditText gcashNumberET;
  74.     private TextInputEditText gcashNameET;
  75.     private Button postButton, cancelButton;
  76.     private String userId;
  77.     private RestoImageSliderAdapter adapter;
  78.     private ViewPager2 viewPager;
  79.     private LinearLayout dotsLayout;
  80.     private ImageView[] dots;
  81.  
  82.     @Override
  83.     protected void onCreate(Bundle savedInstanceState) {
  84.         super.onCreate(savedInstanceState);
  85.         setContentView(R.layout.activity_edit_resto);
  86.  
  87.         viewPager = findViewById(R.id.viewPager);
  88.         dotsLayout = findViewById(R.id.dotsLayout);
  89.         uploadButton = findViewById(R.id.uploadButton);
  90.         restoNameEditText = findViewById(R.id.restoNameEditText);
  91.         restoAddressEditText = findViewById(R.id.restoAddressEditText);
  92.         restoDescriptionEditText = findViewById(R.id.restoDescriptionEditText);
  93.         restoContactEditText = findViewById(R.id.restoContactEditText);
  94.         restoEmailEditText = findViewById(R.id.restoEmailEditText);
  95.         amenityEditText = findViewById(R.id.amenityEditText);
  96.         cancellationPolicyEditText = findViewById(R.id.cancellationPolicyEditText);
  97.         gcashNumberET = findViewById(R.id.gcashNumber);
  98.         gcashNameET = findViewById(R.id.gcashName);
  99.         postButton = findViewById(R.id.postButton);
  100.         cancelButton = findViewById(R.id.cancelButton);
  101.  
  102.  
  103.         TextInputEditText latitudeEditText = findViewById(R.id.latitudeEditText);
  104.         TextInputEditText longitudeEditText = findViewById(R.id.longitudeEditText);
  105.  
  106.  
  107.  
  108.  
  109.         auth = FirebaseAuth.getInstance();
  110.         currentUser = auth.getCurrentUser();
  111.         userId = currentUser.getUid();
  112.  
  113.         storage = FirebaseStorage.getInstance();
  114.         storageReference = storage.getReference();
  115.         database = FirebaseDatabase.getInstance();
  116.         databaseReference = database.getReference();
  117.  
  118.         // adapter para sa multiple images ng resto, slider
  119.         adapter = new RestoImageSliderAdapter(this,imageUris, viewPager,dotsLayout);
  120.         // yung dots nag-iindicate ng pang ilang images na, parang sa ig
  121.         dotsLayout = findViewById(R.id.dotsLayout);
  122.         viewPager.setAdapter(adapter);
  123.         adapter.updateDotsIndicator(0);
  124.  
  125.         viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
  126.             @Override
  127.             public void onPageSelected(int position) {
  128.                 super.onPageSelected(position);
  129.                 adapter.updateDotsIndicator(position);
  130.             }
  131.         });
  132.  
  133.  
  134.         galleryLauncher = registerForActivityResult(
  135.                 new ActivityResultContracts.StartActivityForResult(),
  136.                 result -> {
  137.                     if (result.getResultCode() == Activity.RESULT_OK) {
  138.                         Intent data = result.getData();
  139.                         if (data != null && data.getData() != null) {
  140.                             imageUri = data.getData();
  141.                             imageUris.add(imageUri);
  142.  
  143.                             adapter.notifyDataSetChanged();
  144.                         }
  145.                     }
  146.                 }
  147.         );
  148.  
  149.         uploadButton.setOnClickListener(v -> openGallery());
  150.  
  151.         // button "save changes" mapopost na sa tourist interface
  152.         postButton.setOnClickListener(v -> {
  153.             String restoName = restoNameEditText.getText().toString();
  154.             String restoAddress = restoAddressEditText.getText().toString();
  155.             String restoDescription = restoDescriptionEditText.getText().toString();
  156.             String restoContact = restoContactEditText.getText().toString();
  157.             String restoEmail = restoEmailEditText.getText().toString();
  158.             String cancellationPolicy = cancellationPolicyEditText.getText().toString();
  159.             String amenitiesResto = amenityEditText.getText().toString();
  160.             String gcashNumber = gcashNumberET.getText().toString();
  161.             String gcashName = gcashNameET.getText().toString();
  162.  
  163.             String latitude = latitudeEditText.getText().toString(); // Get latitude
  164.             String longitude = longitudeEditText.getText().toString(); // Get longitude
  165.  
  166.             checkIfExists(userId, restoName, restoAddress, restoDescription, restoContact, restoEmail,
  167.                     cancellationPolicy, imageUri, amenitiesResto, gcashNumber, gcashName, Double.parseDouble(latitude), Double.parseDouble(longitude));
  168.  
  169.             // back to RestoEditFrag, or yung read-only, na nakaretrieve na
  170.             Intent intent = new Intent(this, MainActivity3.class);
  171.             intent.putExtra("frag", "RestoEditFragment");
  172.             startActivity(intent);
  173.         });
  174.  
  175.         retrieve(userId);
  176.  
  177.         cancelButton.setOnClickListener(v -> {
  178.             RestoEditFragment restoEditFragment = new RestoEditFragment();
  179.  
  180.             FragmentManager fragmentManager = getSupportFragmentManager();
  181.             FragmentTransaction transaction = fragmentManager.beginTransaction();
  182.             transaction.replace(R.id.container, restoEditFragment);
  183.             transaction.addToBackStack(null);
  184.             transaction.commit();
  185.         });
  186.  
  187.  
  188.         dots(0); // para lang to sa dots sa image slider
  189.         viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
  190.             @Override
  191.             public void onPageSelected(int position) {
  192.                 super.onPageSelected(position);
  193.                 dots(position);
  194.             }
  195.         });
  196.  
  197.     }
  198.  
  199.     // if nag eexist na yung user sa "hotels", di na pwede mag generate ng new set of data, para iwas doble doeble
  200.     private void checkIfExists(String userId, String restoName, String restoAddress, String restoDescription, String restoContact,
  201.                                String restoEmail, String cancellationPolicy, Uri imageUri, String amenitiesResto, String gcashNumber, String gcashName, double latitude, double longitude) {
  202.         DatabaseReference restoRef = databaseReference.child("restaurants");
  203.         Query query = restoRef.orderByChild("user_id").equalTo(userId);
  204.         query.addListenerForSingleValueEvent(new ValueEventListener() {
  205.             @Override
  206.             public void onDataChange(@NonNull com.google.firebase.database.DataSnapshot dataSnapshot) {
  207.                 if (dataSnapshot.exists()) {
  208.                     String existingRestoId = dataSnapshot.getChildren().iterator().next().getKey();
  209.  
  210.                     // Filter out URLs and keep only local file URIs in imageUris
  211.                     List<Uri> localImageUris = new ArrayList<>();
  212.                     for (Uri uri : imageUris) {
  213.                         if (uri.getScheme() == null) { // Check if it's a local file URI
  214.                             localImageUris.add(uri);
  215.                         }
  216.                     }
  217.  
  218.                     update(restoName, restoAddress, restoDescription, localImageUris, restoContact, restoEmail, cancellationPolicy,
  219.                             existingRestoId, amenitiesResto, gcashNumber, gcashName, latitude, longitude);
  220.                     adapter.setUris(localImageUris);
  221.                     adapter.notifyDataSetChanged();
  222.                     Log.d(TAG, "Resto exists for user: " + userId);
  223.                 } else {
  224.                     create(userId, restoName, restoAddress, restoDescription, restoContact, restoEmail, cancellationPolicy,
  225.                             imageUris, amenitiesResto, gcashNumber, gcashName, latitude, longitude);
  226.                     adapter.setUris(imageUris);
  227.                     adapter.notifyDataSetChanged();
  228.                     Log.d(TAG, "Resto does not exist for user: " + userId);
  229.                 }
  230.             }
  231.             @Override
  232.             public void onCancelled(@NonNull DatabaseError databaseError) {
  233.                 Log.e("check", "error" + databaseError.getMessage());
  234.             }
  235.         });
  236.     }
  237.  
  238.     private void create(String userId, String restoName, String restoAddress, String restoDescription, String restoContact,
  239.                         String restoEmail, String cancellationPolicy, List<Uri> imageUris, String amenitiesResto,
  240.                         String gcashNumber, String gcashName, double latitude, double longitude) {
  241.  
  242.         DatabaseReference restosRef = databaseReference.child("restaurants");
  243.         DatabaseReference restoRef = restosRef.push();
  244.         String restoId = restoRef.getKey();
  245.  
  246.         List<String> imageUrls = new ArrayList<>();
  247.         AtomicInteger uploadCount = new AtomicInteger(0);
  248.         AtomicBoolean hasUploadFailed = new AtomicBoolean(false);
  249.  
  250.         List<UploadTask> uploadTasks = new ArrayList<>();
  251.         List<Task<Uri>> uploadUriTasks = new ArrayList<>();
  252.  
  253.         for (int i = 0; i < imageUris.size(); i++) {
  254.             final int finalIndex = i;
  255.             Uri imageUri = imageUris.get(i);
  256.             StorageReference imageRef = storageReference.child("restaurant_images/" + restoId + "/image" + (i + 1));
  257.             Log.d(TAG, "Image Storage Reference: " + imageRef.toString());
  258.             UploadTask uploadTask = imageRef.putFile(imageUri);
  259.  
  260.             uploadTask.addOnFailureListener(new MyFailureListener());
  261.             uploadTasks.add(uploadTask);
  262.  
  263.             Task<Uri> uploadUriTask = uploadTask.continueWithTask(task -> {
  264.                 if (!task.isSuccessful()) {
  265.                     throw task.getException();
  266.                 }
  267.                 return imageRef.getDownloadUrl();
  268.             });
  269.  
  270.             uploadUriTasks.add(uploadUriTask);
  271.         }
  272.  
  273.         Task<List<Uri>> allUploadsTask = Tasks.whenAllSuccess(uploadUriTasks);
  274.  
  275.         allUploadsTask.addOnSuccessListener(downloadUris -> {
  276.             for (Uri downloadUri : downloadUris) {
  277.                 String imageUrl = downloadUri.toString();
  278.                 imageUrls.add(imageUrl);
  279.             }
  280.  
  281.             if (!hasUploadFailed.get()) {
  282.                 save(restoId, imageUrls, restoName, restoAddress, restoDescription, restoContact, restoEmail,
  283.                         cancellationPolicy, amenitiesResto, gcashNumber, gcashName, latitude,longitude);
  284.  
  285.                 adapter.setUris(imageUris);
  286.                 adapter.notifyDataSetChanged();
  287.             } else {
  288.                 Log.e(TAG, "Image upload failed.");
  289.             }
  290.         }).addOnFailureListener(e -> {
  291.             Log.e(TAG, "Failed to upload images: " + e.getMessage());
  292.             hasUploadFailed.set(true);
  293.  
  294.             for (int j = 0; j < uploadTasks.size(); j++) {
  295.                 UploadTask remainingUploadTask = uploadTasks.get(j);
  296.                 if (remainingUploadTask != null && !remainingUploadTask.isComplete()) {
  297.                     remainingUploadTask.cancel();
  298.                 }
  299.             }
  300.         });
  301.     }
  302.  
  303.     private void update(String restoName, String restoAddress, String restoDescription, List<Uri> localImageUris,
  304.                         String restoContact, String restoEmail, String cancellationPolicy, String restoId, String amenitiesResto,
  305.                         String gcashNumber, String gcashName, double latitude, double longitude) {
  306.         DatabaseReference restosRef = databaseReference.child("restaurants");
  307.         Query query = restosRef.orderByChild("user_id").equalTo(userId);
  308.  
  309.         query.addListenerForSingleValueEvent(new ValueEventListener() {
  310.             @Override
  311.             public void onDataChange(@NonNull com.google.firebase.database.DataSnapshot dataSnapshot) {
  312.                 if (dataSnapshot.exists()) {
  313.                     for (com.google.firebase.database.DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
  314.                         String restoId = childSnapshot.getKey();
  315.  
  316.                         DatabaseReference restoRef = restosRef.child(restoId);
  317.                         restoRef.child("name").setValue(restoName);
  318.                         restoRef.child("address").setValue(restoAddress);
  319.                         restoRef.child("description").setValue(restoDescription);
  320.                         restoRef.child("contact").setValue(restoContact);
  321.                         restoRef.child("email").setValue(restoEmail);
  322.                         restoRef.child("cancellationPolicy").setValue(cancellationPolicy);
  323.                         restoRef.child("amenities").setValue(amenitiesResto);
  324.                         restoRef.child("gcashNumber").setValue(gcashNumber);
  325.                         restoRef.child("gcashName").setValue(gcashName);
  326.  
  327.                         restoRef.child("latitude").setValue(latitude);
  328.                         restoRef.child("longitude").setValue(longitude);
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                         List<String> existingImageUrls = new ArrayList<>();
  335.                         DataSnapshot imagesSnapshot = childSnapshot.child("images");
  336.                         for (DataSnapshot imageSnapshot : imagesSnapshot.getChildren()) {
  337.                             String imageUrl = imageSnapshot.getValue(String.class);
  338.                             existingImageUrls.add(imageUrl);
  339.                         }
  340.  
  341.                         List<String> newImageUrls = new ArrayList<>();
  342.                         for (int i = 0; i < localImageUris.size(); i++) {
  343.                             Uri imageUri = localImageUris.get(i);
  344.                             StorageReference imageRef = storageReference.child("restaurant_images/" + restoId + "/image" + (i + 1));
  345.  
  346.                             // Upload the image and get the download URL
  347.                             UploadTask uploadTask = imageRef.putFile(imageUri);
  348.  
  349.                             uploadTask.addOnSuccessListener(taskSnapshot -> {
  350.                                 // Handle the image upload success and get the download URL
  351.                                 imageRef.getDownloadUrl().addOnSuccessListener(downloadUri -> {
  352.                                     String newImageUrl = downloadUri.toString();
  353.                                     newImageUrls.add(newImageUrl);
  354.  
  355.                                     // Check if all new images have been uploaded
  356.                                     if (newImageUrls.size() == localImageUris.size()) {
  357.                                         // Update the 'images' node with the new URLs
  358.                                         Map<String, Object> imagesMap = new HashMap<>();
  359.                                         for (int j = 0; j < newImageUrls.size(); j++) {
  360.                                             imagesMap.put("image" + (j + 1), newImageUrls.get(j));
  361.                                         }
  362.  
  363.                                         // Remove extra URLs if any
  364.                                         for (int k = localImageUris.size() + 1; k <= existingImageUrls.size(); k++) {
  365.                                             imagesMap.put("image" + k, null);
  366.                                         }
  367.  
  368.                                         // Update the 'images' node with the modified URLs
  369.                                         restoRef.child("images").updateChildren(imagesMap);
  370.  
  371.                                         adapter.setUris(localImageUris);
  372.                                         adapter.notifyDataSetChanged();
  373.                                     }
  374.                                 });
  375.                             });
  376.  
  377.                             // Handle the image upload failure
  378.                             uploadTask.addOnFailureListener(new MyFailureListener());
  379.                         }
  380.                     }
  381.                 }
  382.             }
  383.  
  384.             @Override
  385.             public void onCancelled(@NonNull DatabaseError databaseError) {
  386.                 Log.e("update", "error" + databaseError.getMessage());
  387.             }
  388.         });
  389.     }
  390.  
  391.  
  392.     private void openGallery() {// lalagyan natin to ng permission first
  393.  
  394.         Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  395.         galleryLauncher.launch(galleryIntent);
  396.         Log.d(TAG, "opening gallery with URI: " + imageUri);
  397.     }
  398.     // always save changes
  399.     private void save(String restoId, List<String> imageUrls, String restoName, String restoAddress, String restoDescription,
  400.                       String restoContact, String restoEmail, String cancellationPolicy, String amenitiesResto,
  401.                       String gcashNumber, String gcashName, double latitude, double longitude) {
  402.  
  403.             DatabaseReference restosRef = databaseReference.child("restaurants");
  404.             DatabaseReference restoRef = restosRef.child(restoId);
  405.  
  406.             restoRef.child("name").setValue(restoName);
  407.             restoRef.child("address").setValue(restoAddress);
  408.             restoRef.child("description").setValue(restoDescription);
  409.             restoRef.child("contact").setValue(restoContact);
  410.             restoRef.child("email").setValue(restoEmail);
  411.             restoRef.child("cancellationPolicy").setValue(cancellationPolicy);
  412.             restoRef.child("user_id").setValue(userId);
  413.             restoRef.child("amenities").setValue(amenitiesResto);
  414.             restoRef.child("gcashNumber").setValue(gcashNumber);
  415.             restoRef.child("gcashName").setValue(gcashName);
  416.  
  417.             restoRef.child("latitude").setValue(latitude);
  418.             restoRef.child("longitude").setValue(longitude);
  419.  
  420.             Map<String, Object> imagesMap = new HashMap<>();
  421.             for (int i = 0; i < imageUrls.size(); i++) {
  422.                 imagesMap.put("image" + (i + 1), imageUrls.get(i));
  423.             }
  424.             restoRef.child("images").setValue(imagesMap);
  425.     }
  426.  
  427.     // then reretrieve natin sa mga edit texts
  428.     private void retrieve(String userId) {
  429.         DatabaseReference restoRef = databaseReference.child("restaurants");
  430.         Query query = restoRef.orderByChild("user_id").equalTo(userId);
  431.         query.addListenerForSingleValueEvent(new ValueEventListener() {
  432.             @Override
  433.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  434.                 if (dataSnapshot.exists()) {
  435.                     DataSnapshot restoSnapshot = dataSnapshot.getChildren().iterator().next();
  436.  
  437.                     String title = restoSnapshot.child("name").getValue(String.class);
  438.                     String restoAddress = restoSnapshot.child("address").getValue(String.class);
  439.                     String description = restoSnapshot.child("description").getValue(String.class);
  440.                     String restoContact = restoSnapshot.child("contact").getValue(String.class);
  441.                     String restoEmail = restoSnapshot.child("email").getValue(String.class);
  442.                     String cancellationPolicy = restoSnapshot.child("cancellationPolicy").getValue(String.class);
  443.                     String amenitiesResto = restoSnapshot.child("amenities").getValue(String.class);
  444.                     String gcashNumber = restoSnapshot.child("gcashNumber").getValue(String.class);
  445.                     String gcashName = restoSnapshot.child("gcashName").getValue(String.class);
  446.  
  447.                     Double latitudeDouble = restoSnapshot.child("latitude").getValue(Double.class);
  448.                     Double longitudeDouble = restoSnapshot.child("longitude").getValue(Double.class);
  449.  
  450.                     // Check for null before invoking toString()
  451.                     String latitude = (latitudeDouble != null) ? latitudeDouble.toString() : "defaultLatitude";
  452.                     String longitude = (longitudeDouble != null) ? longitudeDouble.toString() : "defaultLongitude";
  453.  
  454.  
  455.                     Log.d(TAG, "Retrieved Latitude: " + latitude);
  456.                     Log.d(TAG, "Retrieved Longitude: " + longitude);
  457.  
  458.                     List<String> imageUrls = new ArrayList<>();
  459.                     DataSnapshot imagesSnapshot = restoSnapshot.child("images");
  460.                     for (DataSnapshot imageSnapshot : imagesSnapshot.getChildren()) {
  461.                         String imageUrl = imageSnapshot.getValue(String.class);
  462.                         imageUrls.add(imageUrl);
  463.                     }
  464.  
  465.                     restoNameEditText.setText(title);
  466.                     restoDescriptionEditText.setText(description);
  467.                     restoAddressEditText.setText(restoAddress);
  468.                     restoContactEditText.setText(restoContact);
  469.                     restoEmailEditText.setText(restoEmail);
  470.                     cancellationPolicyEditText.setText(cancellationPolicy);
  471.                     amenityEditText.setText(amenitiesResto);
  472.                     gcashNumberET.setText(gcashNumber);
  473.                     gcashNameET.setText(gcashName);
  474.  
  475.                     TextInputEditText longitudeEditText = findViewById(R.id.longitudeEditText);
  476.                     TextInputEditText latitudeEditText = findViewById(R.id.latitudeEditText);
  477.  
  478.                     longitudeEditText.setText(String.valueOf(latitude));
  479.                     latitudeEditText.setText(String.valueOf(longitude));
  480.  
  481.  
  482.  
  483.  
  484.  
  485.                     imageUris.clear();
  486.  
  487.                     imageUris.clear();
  488.                     for (String imageUrl : imageUrls) {
  489.                         imageUris.add(Uri.parse(imageUrl));
  490.                     }
  491.  
  492.                     adapter.setUris(imageUris);
  493.                     adapter.notifyDataSetChanged();
  494.                 }
  495.             }
  496.  
  497.             @Override
  498.             public void onCancelled(@NonNull DatabaseError databaseError) {
  499.                 Log.e("retrieve", "error" + databaseError.getMessage());
  500.             }
  501.         });
  502.     }
  503.     private void dots(int position) {// again for dots lang nung image slider
  504.         dots = new ImageView[imageUris.size()];
  505.         dotsLayout.removeAllViews();
  506.  
  507.         for (int i = 0; i < dots.length; i++) {
  508.             dots[i] = new ImageView(this);
  509.             dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.dots_unselected));
  510.  
  511.             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  512.                     LinearLayout.LayoutParams.WRAP_CONTENT,
  513.                     LinearLayout.LayoutParams.WRAP_CONTENT
  514.             );
  515.  
  516.             params.setMargins(8, 0, 8, 0);
  517.             dotsLayout.addView(dots[i], params);
  518.         }
  519.         if (dots.length > 0) {
  520.             dots[position].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.dots_selected));
  521.         }
  522.     }
  523.  
  524.  
  525. }
  526.  
  527.  
  528. XML :
  529.  
  530. <?xml version="1.0" encoding="utf-8"?>
  531. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  532.     android:layout_width="match_parent"
  533.     android:layout_height="match_parent"
  534.     android:background="@color/basecolor2"
  535.     xmlns:app="http://schemas.android.com/apk/res-auto"
  536.     xmlns:app1="http://schemas.android.com/tools"
  537.     android:fillViewport="true">
  538.  
  539.     <LinearLayout
  540.         android:layout_width="match_parent"
  541.         android:layout_height="wrap_content"
  542.         android:orientation="vertical"
  543.         android:padding="16dp">
  544.  
  545.         <androidx.viewpager2.widget.ViewPager2
  546.             android:id="@+id/viewPager"
  547.             android:layout_width="match_parent"
  548.             android:layout_height="300dp"
  549.             android:background="@drawable/image_placeholder">
  550.         </androidx.viewpager2.widget.ViewPager2>
  551.  
  552.         <LinearLayout
  553.             android:id="@+id/dotsLayout"
  554.             android:layout_width="match_parent"
  555.             android:layout_height="wrap_content"
  556.             android:gravity="center"
  557.             android:orientation="horizontal"/>
  558.  
  559.         <com.google.android.material.floatingactionbutton.FloatingActionButton
  560.             android:layout_width="wrap_content"
  561.             android:layout_height="wrap_content"
  562.             android:baselineAlignBottom="false"
  563.             android:clickable="true"
  564.             app:fabSize="normal"
  565.             android:src="@drawable/add_photo"
  566.             android:id="@+id/uploadButton"
  567.             app:layout_anchor="@id/appbar"
  568.             android:layout_gravity="end"
  569.             android:layout_marginRight="45dp"
  570.             android:backgroundTint="@color/contrastcolor2"/>
  571.  
  572.         <com.google.android.material.textfield.TextInputLayout
  573.             android:id="@+id/restoNameInputLayout"
  574.             android:layout_width="match_parent"
  575.             android:layout_height="wrap_content"
  576.             android:layout_marginVertical="5dp"
  577.             android:hint="Restaurant Name">
  578.  
  579.             <com.google.android.material.textfield.TextInputEditText
  580.                 android:id="@+id/restoNameEditText"
  581.                 android:layout_width="match_parent"
  582.                 android:layout_height="wrap_content"
  583.                 android:textSize="18sp"
  584.                 android:inputType="text" />
  585.  
  586.         </com.google.android.material.textfield.TextInputLayout>
  587.  
  588.  
  589.         <com.google.android.material.textfield.TextInputLayout
  590.             android:id="@+id/restoAddressInputLayout"
  591.             android:layout_width="match_parent"
  592.             android:layout_height="wrap_content"
  593.             android:layout_marginVertical="5dp"
  594.             android:hint="Location">
  595.  
  596.             <com.google.android.material.textfield.TextInputEditText
  597.                 android:id="@+id/restoAddressEditText"
  598.                 android:layout_width="match_parent"
  599.                 android:layout_height="wrap_content"
  600.                 android:textSize="18sp"
  601.                 android:inputType="text" />
  602.  
  603.         </com.google.android.material.textfield.TextInputLayout>
  604.  
  605.  
  606.  
  607.  
  608.  
  609.         <com.google.android.material.textfield.TextInputLayout
  610.             android:layout_width="match_parent"
  611.             android:layout_height="wrap_content"
  612.             android:layout_marginVertical="5dp"
  613.             android:hint="Latitude">
  614.  
  615.             <com.google.android.material.textfield.TextInputEditText
  616.                 android:id="@+id/latitudeEditText"
  617.                 android:layout_width="match_parent"
  618.                 android:layout_height="wrap_content"
  619.                 android:padding="15dp"
  620.                 android:textSize="18sp"
  621.                 android:inputType="numberDecimal" />
  622.         </com.google.android.material.textfield.TextInputLayout>
  623.  
  624.         <com.google.android.material.textfield.TextInputLayout
  625.             android:layout_width="match_parent"
  626.             android:layout_height="wrap_content"
  627.             android:layout_marginVertical="5dp"
  628.             android:hint="Longitude">
  629.  
  630.             <com.google.android.material.textfield.TextInputEditText
  631.                 android:id="@+id/longitudeEditText"
  632.                 android:layout_width="match_parent"
  633.                 android:layout_height="wrap_content"
  634.                 android:padding="15dp"
  635.                 android:textSize="18sp"
  636.                 android:inputType="numberDecimal" />
  637.         </com.google.android.material.textfield.TextInputLayout>
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.         <com.google.android.material.textfield.TextInputLayout
  668.             android:id="@+id/restoDescriptionInputLayout"
  669.             android:layout_width="match_parent"
  670.             android:layout_height="wrap_content"
  671.             android:layout_marginVertical="5dp"
  672.             android:hint="Description">
  673.  
  674.             <com.google.android.material.textfield.TextInputEditText
  675.                 android:id="@+id/restoDescriptionEditText"
  676.                 android:layout_width="match_parent"
  677.                 android:layout_height="wrap_content"
  678.                 android:textSize="18sp"
  679.                 android:inputType="textMultiLine" />
  680.  
  681.         </com.google.android.material.textfield.TextInputLayout>
  682.  
  683.         <com.google.android.material.textfield.TextInputLayout
  684.             android:id="@+id/restoContactInputLayout"
  685.             android:layout_width="match_parent"
  686.             android:layout_height="wrap_content"
  687.             android:layout_marginVertical="5dp"
  688.             android:hint="Contact Number">
  689.  
  690.             <com.google.android.material.textfield.TextInputEditText
  691.                 android:id="@+id/restoContactEditText"
  692.                 android:layout_width="match_parent"
  693.                 android:textSize="18sp"
  694.                 android:layout_height="wrap_content"
  695.                 android:inputType="phone" />
  696.  
  697.         </com.google.android.material.textfield.TextInputLayout>
  698.  
  699.         <com.google.android.material.textfield.TextInputLayout
  700.             android:id="@+id/restoEmailInputLayout"
  701.             android:layout_width="match_parent"
  702.             android:layout_height="wrap_content"
  703.             android:layout_marginVertical="5dp"
  704.             android:hint="Email Address">
  705.  
  706.             <com.google.android.material.textfield.TextInputEditText
  707.                 android:id="@+id/restoEmailEditText"
  708.                 android:layout_width="match_parent"
  709.                 android:layout_height="wrap_content"
  710.                 android:textSize="18sp"
  711.                 android:inputType="textEmailAddress" />
  712.  
  713.         </com.google.android.material.textfield.TextInputLayout>
  714.  
  715.         <com.google.android.material.textfield.TextInputLayout
  716.             android:id="@+id/amenityInputLayout"
  717.             android:layout_width="match_parent"
  718.             android:layout_height="wrap_content"
  719.             android:layout_marginVertical="5dp"
  720.             android:hint="Add Amenity">
  721.  
  722.             <com.google.android.material.textfield.TextInputEditText
  723.                 android:id="@+id/amenityEditText"
  724.                 android:layout_width="match_parent"
  725.                 android:layout_height="wrap_content"
  726.                 android:textSize="18sp"
  727.                 android:inputType="text" />
  728.  
  729.         </com.google.android.material.textfield.TextInputLayout>
  730.  
  731.         <com.google.android.material.textfield.TextInputLayout
  732.             android:id="@+id/cancellationPolicyInputLayout"
  733.             android:layout_width="match_parent"
  734.             android:layout_height="wrap_content"
  735.             android:layout_marginVertical="5dp"
  736.             android:hint="Cancellation Policy">
  737.  
  738.             <com.google.android.material.textfield.TextInputEditText
  739.                 android:id="@+id/cancellationPolicyEditText"
  740.                 android:layout_width="match_parent"
  741.                 android:layout_height="wrap_content"
  742.                 android:textSize="18sp"
  743.                 android:inputType="textMultiLine" />
  744.  
  745.         </com.google.android.material.textfield.TextInputLayout>
  746.  
  747.  
  748.         <com.google.android.material.textfield.TextInputLayout
  749.             android:layout_width="match_parent"
  750.             android:layout_height="wrap_content"
  751.             android:hint="Gcash Number"
  752.             android:layout_marginVertical="5dp"
  753.             app:counterEnabled="true">
  754.  
  755.             <com.google.android.material.textfield.TextInputEditText
  756.                 android:id="@+id/gcashNumber"
  757.                 android:layout_width="match_parent"
  758.                 android:layout_height="wrap_content"
  759.                 android:textSize="18sp"
  760.                 android:padding="15dp"
  761.                 android:inputType="textMultiLine" />
  762.  
  763.         </com.google.android.material.textfield.TextInputLayout>
  764.  
  765.  
  766.  
  767.         <com.google.android.material.textfield.TextInputLayout
  768.             android:layout_width="match_parent"
  769.             android:layout_height="wrap_content"
  770.             android:hint="Gcash Name"
  771.             android:layout_marginVertical="5dp"
  772.             app:counterEnabled="true">
  773.             <com.google.android.material.textfield.TextInputEditText
  774.                 android:id="@+id/gcashName"
  775.                 android:layout_width="match_parent"
  776.                 android:layout_height="wrap_content"
  777.                 android:inputType="textMultiLine"
  778.                 android:padding="15dp"
  779.                 android:textSize="18sp" />
  780.         </com.google.android.material.textfield.TextInputLayout>
  781.  
  782.         <LinearLayout
  783.             android:layout_width="match_parent"
  784.             android:layout_height="wrap_content"
  785.             android:orientation="horizontal">
  786.  
  787.             <androidx.appcompat.widget.AppCompatButton
  788.                 android:layout_width="0dp"
  789.                 android:layout_weight="1"
  790.                 android:layout_height="50dp"
  791.                 android:layout_gravity="start"
  792.                 android:gravity="center"
  793.                 android:text="Save Changes"
  794.                 android:id="@+id/postButton"
  795.                 android:textSize="15sp"
  796.                 android:padding="7dp"
  797.                 android:textColor="@color/white"
  798.                 android:layout_marginTop="8dp"
  799.                 android:layout_marginRight="10dp"
  800.                 android:layout_marginLeft="10dp"
  801.                 android:background="@drawable/gradient4" />
  802.  
  803.             <androidx.appcompat.widget.AppCompatButton
  804.                 android:layout_width="0dp"
  805.                 android:layout_weight="1"
  806.                 android:layout_height="50dp"
  807.                 android:layout_gravity="start"
  808.                 android:gravity="center"
  809.                 android:text="Cancel"
  810.                 android:id="@+id/cancelButton"
  811.                 android:textSize="15sp"
  812.                 android:padding="7dp"
  813.                 android:textColor="@color/white"
  814.                 android:layout_marginTop="8dp"
  815.                 android:layout_marginRight="10dp"
  816.                 android:layout_marginLeft="10dp"
  817.                 android:background="@drawable/gradient4" />
  818.         </LinearLayout>
  819.  
  820.     </LinearLayout>
  821. </ScrollView>
  822.  
Tags: Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement