'I have an issue with ProgressBar Widget
Basically I have a ConsentForm Activity that the user scrolls through and give their consent. Upon pressing the consent Btn (Yes) a dialog where they have to enter their email pops up. When they enter a valid email and press the send button the progressbar should load until the next activity opens. For some reason the progress bar never appears.
Here is the Consent Activity:
package com.example.sport_screen.Consent_Activities;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.sport_screen.Database.DBHelper;
import com.example.sport_screen.For_Admin.AdminActivity;
import com.example.sport_screen.LoginActivity;
import com.example.sport_screen.R;
import com.example.sport_screen.RegisterActivity;
import com.example.sport_screen.Utils;
import com.example.sport_screen.WelcomeActivity;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import soup.neumorphism.NeumorphCardView;
public class Temp_InformedConsent_Activity extends AppCompatActivity {
NeumorphCardView denyBtn, consentBtn;
TextView par1, par2, par3, par4, par6, sendBtn;
Dialog dialog;
EditText enterEmail, messageEmail;
String valid_email, msg;
ProgressBar progressBar;
private long backPressedTime;
private Toast backToast;
@Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel();
Intent intent = new Intent(Temp_InformedConsent_Activity.this, WelcomeActivity.class);
finish();
startActivity(intent);
} else {
msg = "Press back again to logout";
backToast = toastLayout();
backToast.show();
}
backPressedTime = System.currentTimeMillis();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp_informed_consent);
DBHelper database = new DBHelper(this);
Utils.blackIconStatusBar(Temp_InformedConsent_Activity.this, R.color.light_Background);
dialog = new Dialog(this);
par1 = (TextView) findViewById(R.id.par1);
par2 = (TextView) findViewById(R.id.par2);
par3 = (TextView) findViewById(R.id.par3);
par4 = (TextView) findViewById(R.id.par4);
par6 = (TextView) findViewById(R.id.par6);
consentBtn = (NeumorphCardView) findViewById(R.id.consentBtn);
denyBtn = (NeumorphCardView) findViewById(R.id.denyBtn);
String p1 = "Consent\nTo voluntarily engage on an acceptable plan of personal fitness training. Follow the exact instructions regarding exercise, stress management and other health and fitness regarded programs. I have informed the trainer of any medication prescribed to me. I will allow a personal fitness trainer to periodically monitor performance and measure pulse, blood pressure etc. Allow physical touching and body positioning as required by the performed exercises.";
String p2 = "Risks (I have be informed of possible..)\nAbnormal blood pressure, fainting, dizziness, disorders of heart rhythm; and very rare cases: heart attack, stroke or even death. furthermore possible bodily injury, to muscles, ligaments, tendons and joints of the body.\nI fully understand the risks but knowing them it is my desire to participate.";
String p3 = "Benefits\nUnderstand that if I closely follow the program instructions, that I will likely improve my exercise capacity and fitness level after a period of 3-6 months.";
String p4 = "Confidentiality\nAny obtained information will be treated in confidentiality, not revealed to any person. Any unidentifiable information might be used for research purposes. Any information obtained will be used by the program staff to evaluate my exercise status or needs.";
String p6 = "By pressing YES, you agree to download the FULL version of Informed Consent Form, Sign It and provide it to the administration for verification later on.";
SpannableString s1 = new SpannableString(p1);
SpannableString s2 = new SpannableString(p2);
SpannableString s3 = new SpannableString(p3);
SpannableString s4 = new SpannableString(p4);
SpannableString s6 = new SpannableString(p6);
ForegroundColorSpan fcsRed = new ForegroundColorSpan(Color.RED);
ForegroundColorSpan fcsBlack = new ForegroundColorSpan(Color.BLACK);
UnderlineSpan underlineSpan = new UnderlineSpan();
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
s1.setSpan(boldSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s1.setSpan(fcsBlack, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s2.setSpan(boldSpan, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s2.setSpan(fcsBlack, 0, 40, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s3.setSpan(boldSpan, 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s3.setSpan(fcsBlack, 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s4.setSpan(boldSpan, 0, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s4.setSpan(fcsBlack, 0, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s6.setSpan(fcsRed, 0, 153, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s6.setSpan(boldSpan, 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s6.setSpan(boldSpan2, 81, 89, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s6.setSpan(underlineSpan, 29, 55, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
par1.setText(s1);
par2.setText(s2);
par3.setText(s3);
par4.setText(s4);
par6.setText(s6);
consentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.setContentView(R.layout.privacy_cons_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressBar = dialog.findViewById(R.id.progressBar);
sendBtn = dialog.findViewById(R.id.btnSend);
enterEmail = dialog.findViewById(R.id.emailPrivacyCons);
messageEmail = dialog.findViewById(R.id.messagePrivacyCons);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (enterEmail.getText().toString().isEmpty() || isEmailValid(enterEmail.getText().toString()) == false)
{
enterEmail.setError("Please enter a valid Email");
enterEmail.requestFocus();
valid_email = null;
}
else
{
progressBar.setVisibility(View.VISIBLE);
final String usernameNoReply = "[email protected]";
final String usernameSportScreen = "[email protected]";
final String password = "SportScreenApp123";
messageEmail.setText("Hello,\n\nClick the following link to Download the Full Version of Informed Consent form.\n\nhttps://www.exerciseismedicine.org/assets/page_documents/EIM%20informed%20consent.pdf" +
"\n\nPlease read this Informed Consent form carefully, and sign it freely and voluntary. " +
"\nIf you give your consent, please provide the form (filled) printed to the Administration and via email for verification." +
"\n\nTo send via email, please reply/send to this email.\nSportScreen.");
String messageToSend = messageEmail.getText().toString();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usernameSportScreen, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(usernameSportScreen));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(enterEmail.getText().toString()));
message.setSubject("Informed Consent PDF File Link");
message.setText(messageToSend);
Transport.send(message);
msg ="Email Send Successfully!";
toastLayout();
dialog.dismiss();
Intent intent = new Intent(Temp_InformedConsent_Activity.this, RegisterActivity.class);
finish();
startActivity(intent);
}catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
dialog.show();
//String cons = "YES";
//database.insertToTempInformed("", cons);
}
});
denyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.setContentView(R.layout.temp_dialog_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 4000);
}
});
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private Toast toastLayout()
{
Toast toast = Toast.makeText(Temp_InformedConsent_Activity.this, msg, Toast.LENGTH_SHORT);
View view = toast.getView();
view.setBackgroundResource(R.drawable.toast);
view.getBackground().setAlpha(180);
TextView text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(Color.parseColor("#FFFFFF"));
toast.setGravity(Gravity.BOTTOM,0,0);
toast.setMargin(0, 0.04f);
toast.show();
return toast;
}
}
And here is the xml file with the progressBar:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="350dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_Background">
<TextView
android:id="@+id/titleEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Get Link to PDF File"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageEmail"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleEmail"
app:srcCompat="@drawable/email_pdf" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:gravity="center"
android:text="Please enter your email"
android:textSize="21sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageEmail" />
<soup.neumorphism.NeumorphCardView
android:id="@+id/emailCardView"
style="@style/Widget.Neumorph.CardView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="16dp"
android:layout_marginRight="15dp"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:neumorph_shapeType="pressed">
<EditText
android:id="@+id/emailPrivacyCons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:background="@android:color/transparent"/>
</soup.neumorphism.NeumorphCardView>
<EditText
android:id="@+id/messagePrivacyCons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="Hello,\n\nClick the following link to Download the Privacy Notice form.\n\n https://www.keepandshare.com/doc9/27716/privacy-notice-pdf-80k?da=y \n\nDo Not Reply to this Email.\nSportScreen."
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/emailCardView"
app:layout_constraintTop_toTopOf="@+id/textView3"
tools:ignore="TouchTargetSizeCheck" />
<soup.neumorphism.NeumorphCardView
android:id="@+id/sendEmailBtnCardView"
style="@style/Widget.Neumorph.CardView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="24dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:neumorph_backgroundColor="@color/textColor"
app:neumorph_shapeType="flat">
<TextView
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="SEND EMAIL"
android:textColor="#FFFFFF"
android:textSize="17sp" />
</soup.neumorphism.NeumorphCardView>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="136dp"
android:layout_marginTop="172dp"
android:indeterminateTint="@color/textColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
use
android:visibility="gone"
instead of
android:visibility="invisible"
in your progressBar
XML code
Solution 2:[2]
Try this:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="80dp"
android:layout_height="80dp"
android:elevation="8dp"
android:indeterminateTint="@color/textColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="invisible"/>
Edit:
I believe the reason is that after the send button is pressed, everything is done fast and RegisterActivity
is immediately run so you don't see the progress bar.
to provide a better experience for the user, launch the RegisterActivity
after a delay. This way, the loading is shown, then before you show the "message sent" Toast message, make the progress bar INVISIBLE
and then next activity will be loaded after a 1.5 second delay like so:
progressBar.setVisibility(View.INVISIBLE);
toastLayout();
dialog.dismiss();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
Intent intent = new Intent(Temp_InformedConsent_Activity.this, RegisterActivity.class);
finish();
startActivity(intent);
}, 1500);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | M Dev |
Solution 2 |