The post does not load on Firebase until all fields are filled in (shows infinite ProgressBar). How to make EditText'y mandatory, and the image is not

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post); mStorage = FirebaseStorage.getInstance().getReference(); mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog"); mSelectImage = (ImageButton) findViewById(R.id.imageSelect); mPostTitle =(EditText)findViewById(R.id.titleField); mPostDesc = (EditText)findViewById(R.id.descField); mSubmitBtn = (Button)findViewById(R.id.submitBtn); mProgress = new ProgressDialog(this); mSelectImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); galleryIntent.setType("image/*"); startActivityForResult(galleryIntent,GALLERY_REQUEST); } }); mSubmitBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startposting(); } }); } private void startposting() { mProgress.setMessage("Uploading Post.."); mProgress.show(); final String title_val = mPostTitle.getText().toString().trim(); final String desc_val = mPostDesc.getText().toString().trim(); if(!TextUtils.isEmpty(title_val)&& !TextUtils.isEmpty(desc_val)&& mImageUri!=null){ StorageReference filepath = mStorage.child("Blog_images").child(mImageUri.getLastPathSegment()); filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { @SuppressWarnings("VisibleForTests") Uri downloadUri = taskSnapshot.getDownloadUrl(); DatabaseReference newPost = mDatabase.push(); newPost.child("title").setValue(title_val); newPost.child("desc").setValue(desc_val); newPost.child("image").setValue(downloadUri.toString()); mProgress.dismiss(); startActivity(new Intent(PostActivity.this, MainActivity.class)); } }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { @Override public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { @SuppressWarnings("VisibleForTests") double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount(); mProgress.setMessage("Uploading " + ((int) progress) + "%..."); mProgress.show(); } }); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if ( requestCode == GALLERY_REQUEST && resultCode == RESULT_OK ){ mImageUri = data.getData(); mSelectImage.setImageURI(mImageUri); } 

    1 answer 1

    Hello! Everything is simple here. Set a check for filling in the fields, and if the user tries to continue the robot with the application without filling in the fields, output some kind of error .. For example

    Example field validation:

     mPostTitle =(EditText)findViewById(R.id.titleField); mPostDesc = (EditText)findViewById(R.id.descField); if(mPostTitle.getText().toString() != ""){ if(mPostDesc.getText().toString() != ""){ // Все поля заполнены.. }else{ // поле mPostDasc пустое. Выводим ошибку... } }else{ // поле mPostTitle пустое. Выводим ошибку... } 
    • Yes, but it does not solve the main problem - it is necessary that EditTexts without an image can be posted, and an image without Edits is impossible. And now, even with a check, if I fill in the text, without a picture - eternal progress - Artyom
    • No one can help? - Artem