Remove multiple toast? [duplicate] - android

This question already has answers here:
How to prevent Multiple Toast Overlaps
(9 answers)
Closed 7 years ago.
I set a button on layout, and if user click button that will display toast...
button.setOnClickListener(toastListener);
OnClickListener toastListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
};
But when user click button many time, they will display more toast.
Can I always display one toast on screen whether how many times user click button?
Thanks a lot

I haven't tried it for real but I suspect just cancelling it on the next click and making a new one would be alright.
Toast mToast;
public void onContentChanged() {
...
button.setOnClickListener(toastListener);
OnClickListener toastListener = new OnClickListener() {
#Override
public void onClick(View v) {
if(mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
mToast.show();
}
};

I use the following method to achieve this.
private void showToastMessage(final String message) {
mHandler.post(new Runnable() {
public void run() {
if (mToast == null) {
if (getActivity() != null) {
mToast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
}
}
if (getActivity() != null) {
mToast.setText(message);
mToast.show();
}
}
});
}

Related

Android: TextView visibility changes

I have started with android programming and I am trying to do:
Let the user enter their login credential.
If the user login credential is valid let them login.
If login credential is invalid then give them an error message saying that the user name or password entered is invalid.
For the last part, the problem is that I have a TextView with default hidden visibility. I want to make it visible for a few seconds and make it disappear.
This post and this post have helped but they are essentially trying to do the opposite.
My code is given below:
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check the validity of the user name.
if (valid) {
// Let the user log in.
} else {
// Make TextView visible for a few seconds.
}
}
});
How do I go about this?
In your case first you need to show TextView then make it INVISIBLE .
Use textView.postDelayed method as:
if(valid){ //let the user login }
else
{
// make TextView visible here
textView.setVisibility(View.VISIBLE);
//use postDelayed to hide TextView
textView.postDelayed(new Runnable() {
public void run() {
textView.setVisibility(View.INVISIBLE);
}
}, 3000);
//how to make the textview visible for a few seconds
}
To show any information or error message you can use small pop-up message for some time. Following the code and image for toast.
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
You can use toast.
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Toasts in android
try this
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setVisibility(View.VISIBLE);
v.postDelayed(new Runnable() {
#Override
public void run() {
mTextView.setVisibility(View.GONE);
}
}, 2000);
}
});
you can achieve this with the help of timertask,as your textview is invisible first make it visible and then with the help of timertask make it invisible again
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
txt.setVisibility(View.VISIBLE);
Timer t = new Timer(false);
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txt.setVisibility(View.INVISIBLE);
}
});
}
}, 5000);
}
});
but i would suggest if you want user confirmation show error in alertdialog or a toast to show error would be fine
You can do this by using handler.
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
//how to make the textview visible for a few seconds
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
mTextView.setVisibility(View.VISIBLE);
}
}, 1000/* 1sec delay */);
}
});

Android Show dialog when Runnable with Progress Dialog finishes

I have a Fragment. When the Fragment is created I want to show a Progress Dialog for 3 seconds, then dismiss it and show a pop up dialog. I attach below my code.
From the onCreate() of my Fragment:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait", "text..", true);
myPd_ring.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try
{
Thread.sleep(3000);
} catch(Exception e)
{
}
myPd_ring.dismiss();
}
}).start();
showPopup(0, 8, 1, "Next photo");
And my popup method:
public void showPopup(final int type, int photoNo, int currPhoto, String message) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup_erroare);
dialog.setCancelable(false);
TextView textHeader;
TextView textContent;
textHeader = (TextView) dialog.findViewById(R.id.text_titlu);
textContent = (TextView) dialog.findViewById(R.id.text_error);
textHeader.setText("Procedura fotografiere");
textContent.setText("Poza nr. " + currPhoto+ " of" + noPhoto+
". " + message);
if (type == 0) {
}
Button btn_nu = (Button) dialog.findViewById(R.id.button_nu);
if (type == 0) {
btn_nu.setText("NU");
}
btn_nu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
btn_nu.setVisibility(View.GONE);
Button btn_da = (Button) dialog.findViewById(R.id.button_da);
btn_da.setText("Fotografiere");
btn_da.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (type == 0) {
captureImage();
}
dialog.dismiss();
}
});
dialog.show();
}
The problem is that my ProgressDialog doesn't appear, the popup appears directly. If I put my pop up invoking method in the new Thread() body I get an error. It seems that you can invoke a dialog from a Runnable.
you can use the below code:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait",
"text..", true);
myPd_ring.setCancelable(false);
myPd_ring.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myPd_ring.dismiss();
showPopup(0, 8, 1, "Next photo");
}
}, 3000);
If you have any query please let me know.
Never use sleep() in final APP.
try this way, new a Runnable(), and call postDelay(runnable, delayTimes) on a Handle.
Android Handler has a handy method postDelay() that does what you need, for example:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait", "text..", true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myPd_ring.dismiss();
// show popup
}
}, 3000);
In Android, all View related tasks must be executed in the main thread, doing it in other threads will result in error. If you want to do in inside a background task for example, you'll need to use myactivity.runOnUiThread()
runOnUiThread(new Runnable() {
#Override
public void run() {
// show my popup
}
});

How to prevent custom dialog from popping twice in asynctask onPostExecute?

I'm using a custom dialog in onpostexecute method in AsyncTask, it is being popped twice. When the user clicks on a button the dialog has to be closed, this seems to work fine.
Can someone shed some light on why it is being called twice ?
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null) {
pDialog.dismiss();
}
try {
if (responseFromServer.contains("x")) {
// Pop up to create password
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_password);
dialog.setTitle("Title...");
dialog.setCancelable(false);
final TextView etpassword = (TextView) dialog.findViewById(R.id.etpassword_dialog);
final Button btnpassword = (Button) dialog
.findViewById(R.id.btnsavepassword_dialog);
btnpassword.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (etpassword.getText().toString().length() == 0) {
Toast.makeText(getActivity(), "Enter password", Toast.LENGTH_SHORT)
.show();
} else if (etpassword.getText().toString().length() < 6) {
Toast.makeText(getActivity(),
"Password should contain minimmum 6 characters",
Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
}
}
});
if (!dialog.isShowing()) {
dialog.show();
}
}
else {
Toast.makeText(getActivity(), "Unexpected error occurred. Please try again",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.v("Main FRagment FB async::::::", e.getMessage());
}
}
You can write following condition before display a custom dialog,
if ( !dialog.isShowing() )
{
dialog.show();
}
your code is correct there is no problem in code. Check button click. i think you are calling twice execute() method of AsyncTask.
Can you post the calling code like how are you calling AsyncTask fron button click.

Change onClickListener dynamically in handleMessage depending on Message

I have an Activity with Android.os.Handler, which is used for communication with an openGL thread.
I want to get messages from the opengl thread and draw some GUI in the activity depending on the message data, so I do:
Handler handle = new Handler(new Handler.Callback(){
public boolean handleMessage(Message msg){
// update GUI like
TextView v1 = (TextView) GamescreenActivity .this.findViewById(R.id.mytextview)
// then what I actually would like to do but it does not work:
Button b = (Button) GamescreenActivity.this.findViewById(R.id.mybutton);
b.setOnClickListener(null);
if (msg.what == MY_OWN_CONSTANT) {
b.setOnClickListener(getOnClickDoSomething(msg));
}
}
View.OnClickListener getOnClickDoSomething(final Message msg) {
return new View.OnClickListener() {
public void onClick(View v) {
makeDialog(msg);
}
};
}
private void makeDialog(Message msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(GamescreenActivity.this);
builder.setMessage("yo")
.setTitle(""+ msg.what);
AlertDialog dialog = builder.create();
dialog.show();
}
I hope it is clear what i'm trying to achieve. What I get by now is msg being null in the makeDialog methode almost all the time.
What I get by now is msg being null in the makeDialog methode almost all the time.
I believe the Message has already been recycled when this happens. Let's create a local copy of msg and modify your code a little to make it more efficient. First create a new field variable:
Message message;
Next change your if-else block:
if (msg.what == MY_OWN_CONSTANT) {
message = Message.obtain(msg);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(GamescreenActivity.this);
builder.setMessage("yo")
.setTitle(""+ message.what)
.show();
message.recycle(); // Recycle our message when we're done
}
});
}
else {
b.setOnClickListener(null);
}

how to get text from radiobutton in string and check whether it is empty or not

What is the error in radioButton1.getText().toString().equals("") in code below?
I want to force the user to fill in this field and if he leaves the field empty then toast a message that fills the required field. How do I do this?
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(radioButton1.getText().toString().equals("")||radioButton2.getText().toString().equals("")||radioButton3.getText().toString().equals("")||radioButton4.getText().toString().equals("")
||petn.getText().toString().equals("")||breed.getText().toString().equals("")||color.getText().toString().equals("")||alergic.getText().toString().equals("")||ath_locat.getText().toString().equals("")||d_condi.getText().toString().equals("")||weight1.getText().toString().equals("")
)
{
Log.e("onclick", ",pd");
Toast msg = Toast.makeText(
My2Activity.this,"Please fill the * required field",Toast.LENGTH_LONG);
msg.show();
}
else{
PostData();
Intent d = new Intent(getApplicationContext(),
My1Activity.class);
startActivity(d);
}
}
});
if(radioButton1.isChecked())
{
}
else
{
}
you want to set OnCheckedChangeListener to radio button
radioButton1.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
}
}
I think you should use
radioButton1.isChecked()

Categories

Resources