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 */);
}
});
Related
I am using following code for using a button. I works.(sendBtn is a button in a fragment)
sendText = view.findViewById(R.id.send_text);
View sendBtn = view.findViewById(R.id.send_btn);
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
Now i want to disable the button for 1 sec after a click
I found following solution but above code works "without onClick(View v)"method and without implementing View.OnClickListener in class. How to provide delay in such case..How code is working without onClick method.
#Override
public void onClick(View v) {
((Button) findViewById(R.id.click)).setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
((Button) findViewById(R.id.click))
.setEnabled(true);
}
}, 1000);
}
});
Use this method when you want to interrupt user clicks:
private static long mLastClickTime = 0L;
public static boolean isOpenRecently() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
Usage:
if (v.getId() == R.id.sendBtn) {
if (isOpenRecently()) return;
// Your logic
}
Basically you are using lambda in your code, where v->{} represents the onCLick(View v) function.
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
You can do the following to disable the button for 1 second
void doOnSendButtonClick(View v){
//Send the message (your logic here)
send(sendText.getText().toString());
//Disable button
sendBtn.setEnabled(false);
//enable button after 1000 millisecond
new Handler(Looper.getMainLooper()).postDelayed(() -> {
sendBtn.setEnabled(true);
}, 1000);
}
And call this method when user clicks on the button
sendButton.setOnClickListener(view -> doOnSendButtonClick(view));
You could use a CountDownTimer.
CountDownTimewr timer = new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// enable button
((Button) findViewById(R.id.click)).setEnabled(true);
}
}
#Override
public void onClick(View v) {
// disable btn and start timer of one second in millis
((Button) findViewById(R.id.click)).setEnabled(false);
timer.start();
}
});
Please try this way
Handler(Looper.getmainLooper()) and remove callback after you done your task inside runnable thread...also try to put this logic in any method outside in this class and call from setonclicklistener
Try the Timer.shedule with timertask
you can use runOnUIThread method inside where you can update UI.
you can also try to check the time difference of click and enable again in looping like while.
I wanted to make a snackbar dialog on double press to exit...(java)
Requested with these
On 1st time back pressed show dialogue " press back again to exit " for 2 seconds
On pressing back again show "do you want to exit ? " with the
confirmation button for 2 seconds
As like below -
Create id for your layout in activity_main
CoordinatorLayout coordinatorLayout;
#Override
public void onBackPressed() {
coordinatorLayout= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
if (!doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = true;
Snackbar.make(coordinatorLayout, "Do you really want to exit?", Snackbar.LENGTH_LONG)
.setAction("YES", new View.OnClickListener() {
#Override
public void onClick(View view) {
//button action here
System.exit(0);
}
}).setActionTextColor(Color.YELLOW)
.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
Hope this helps
Here is the solution for you -
private final long DOUBLE_PRESS_BACK_TO_EXIT_TIME = 2000;
boolean doubleBackPressed = false;
#Override
public void onBackPressed() {
// todo: show the snackbar here.
this.doubleBackPressed = true;
utils.showToastLong(getString(R.string.press_again_to_exit));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackPressed = false;
// todo: hide the snackbar here.
}
}, DOUBLE_PRESS_BACK_TO_EXIT_TIME);
}
To customize the Snackbar you may follow this link
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
}
});
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);
}
I have a very simple scenario: I have to make my progress bar invisible at the starting, but on the button click, have to make it visible so that the tasks which I am running in the background will be done and till then my progress bar will run.
I am using a very simple way. I have put the progress bar in XML, then simply at the onCreate method of the activity, first making it invisible by mProgress.setVisibility(4) and then when I am clicking my button trying to make this visible again.
But unfortunately its not working! Anyone please reply why its not doing this.
mProgress = (ProgressBar) findViewById(R.id.progressBar);
mProgress.setVisibility(4);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
strpatientid = txtpatientid.getText().toString();
if (strpatientid.length() == 0) {
Toast.makeText(getApplicationContext(),
"Enter the Patient ID",
Toast.LENGTH_LONG).show();
return;
}
else {
mProgress.setEnabled(false);
mProgress.setVisibility(View.VISIBLE);
setProgressBarVisibility(true);
}
}
try it like this:
mProgress=(ProgressBar) findViewById(R.id.progressBar);
mProgress.setVisibility(View.INVISIBLE);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
strpatientid = txtpatientid.getText().toString();
if (TextUtils.isEmpty(strpatientid)) {
Toast.makeText(getApplicationContext(),"Enter the Patient ID",
Toast.LENGTH_LONG).show();
mProgress.setVisibility(View.INVISIBLE);
return;
}
else{
//mProgress.setEnabled(false); //you dont need this
mProgress.setVisibility(View.VISIBLE);
setProgressBarVisibility(true);
}
}