Toast when fields are Empty in Application [duplicate] - android

This question already has answers here:
How do I check if my EditText fields are empty? [closed]
(30 answers)
Closed 4 years ago.
I want to display toast when fields are empty.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
});

Try this :
When the fields are empty, apply this check :-
if (edittext1.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 1 can not be empty",Toast.LENGTH_LONG).show();
}
else if (edittext2.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 2 can not be empty",Toast.LENGTH_LONG).show();
}
else{
//success code here
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}

Before doing that
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
Try to check if data is empty as follow
if (edittext1.getText().toString().isEmpty() || edittext2.getText().toString().isEmpty()) {
Toast.makeText(this,"fill the required field",Toast.LENGTH_LONG).show();
} else {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
}

Instead of toast, "setError" to edit texts. It would be better
editText.setError("This field can not be blank");

Try this,
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittext1.getText().toString().length()<=0){
Toast.makeText(this, "email is empty", Toast.LENGTH_SHORT).show();
}else if (edittext2.getText().toString().length()<=0){
Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
}else{
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
}
});

Related

Android code for checking visibility of edit text

Hi i have a small problem. I have spinner when i select "yes" edit text 1 and edit text 2 will display when i select "NO" edit text will disappear its working fine for me. But when i press on button for validating the edit text logcat as "AUDIO_OUTPUT_FLAG_FAST denied by client". this is the code.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(edittext1.getText().toString().length()==0)
{
Toast.makeText(getApplicationContext(), "Please Enter Key NO(FX)", Toast.LENGTH_SHORT).show();
}
else if(sfpchangeddata.contains("SFP Changed"))
{
Toast.makeText(getApplicationContext(), "Please Select SFP changed or Not", Toast.LENGTH_SHORT).show();
}
else if(edittext2.getVisibility()==View.VISIBLE)
{
if(edittext2.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
else if(edittext3.getVisibility()==View.VISIBLE)
{
if(edittext3.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Old Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
}
});
isShown() method returns boolean value, so you can use this in your if loop.
if(edittext.isShown())
{
//Set the code here if the edittext is visible.
}
else
{
//Here the code which will run if ediitext is invisible.
}
Hope this will help you.
You can check the visibility for an editText by using isShown() on your ediText

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.

Remove multiple toast? [duplicate]

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();
}
}
});
}

Android: Enable / Disable a button in Runtime?

Ok, so I have two buttons. The first one is to "Load Text" and the second is to "Speak Out".
Now, I don't want the Speak button to be active while there is no text loaded.
I've managed to set value into the EditText by Load Text button's onClickListener method. Inside the same method I have called,
btnSpeak.setEnabled(true);
I have initialized this as,
btnSpeak = (Button) findViewById(R.id.button1);
The entire coding is,
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
//for checking
if(btnSpeak.isEnabled())
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
This is to check the status and assign language to the TTS for further use. I get the toast as "Button should work" but it doesn't get enabled. Why is it so? What's the work around?
I have in .xml file as,
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="200dp"
android:enabled="false"
android:text="#string/tts_text" />
Should I have it enabled in here and then disable and enable in runtime??
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
//for checking
if(btnSpeak.isEnabled())
{
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
YourVoicemethod();
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
});
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
You have to use setClickable
btnSpeak.setClickable(true);
btnSpeak.setEnabled(true);
Also, use isEnabled() to check the state
//for checking
if(btnSpeak.isEnabled())
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
Check your button is pressed or not and write your code.
Sample code.
if(button2.isPressed()){
button2.setEnabled(false);
button1.setEnabled(true);
}
else if(button1.isPressed()){
button1.setEnabled(false);
button2.setEnabled(true);
}
I hope this will help you.
May be is too late to answer and you already found a solution, but you can use invalidate to force the button to redraw itself according to its new state:
btnSpeak.setEnabled(true);
btnSpeak.invalidate();
you can achieve by disable and enabling the visibility of button
here is an example
View button = findViewById(R.id.buttonid);
button.setVisibility(View.GONE);
Use one listener for both buttons
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
listener = new OnClickListener({
#Override
public void onClick(View v) {
switch(button_id){
case id1:
button2.setEnabled(false);
button1.setEnabled(true);
break;
case id2:
button1.setEnabled(false);
button2.setEnabled(true);
break;
}
}
});

Focus removed while typing in edit text

I have an EditText
serialText = (EditText) findViewById(R.id.pinText);
Now onclick of somebutton, I should get the focus to edit text to write something. But when I click inside of edit text to write, focus is getting removed, i.e., whatever i type does not appear in the edit text
This is the code to do that,
public void method(View v) {
arrowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
serialNumber = serialText.getText().toString();
if (serialNumber.equals("")) {
} else {
}
} }...
Here if I don't type anything it comes to if loop without any problem, but samething is not happening for else part.
Can you please help me?
This is my full code
public void onClick(View v) {
if (v.getId() == R.id.pinBtn) {
arrowButton = (Button) findViewById(R.id.arrowBtn);
serialText = (EditText) findViewById(R.id.pinText);
serialText.setVisibility(View.VISIBLE);
arrowButton.setVisibility(View.VISIBLE);
//serialNumber = serialText.getText().toString();
arrowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
serialNumber = serialText.getText().toString();
if (serialNumber.equals("")) {
Toast.makeText(getApplicationContext(),
"Please enter the serial number",
Toast.LENGTH_SHORT).show();
}
else {
isRegularSerialNumber(serialNumber);
String encodedserialNumber = Base64.encodeToString(
serialNumber.getBytes(), Base64.DEFAULT);
receiveSerialData(encodedserialNumber);
if (serialResponse.equals("Validated Successfully!")) {
showAudio(v);
} else {
Toast.makeText(getApplicationContext(),
"Invalid Serial Number", Toast.LENGTH_SHORT)
.show();
}
}
}
});
}
}
Thanks
I solved the problem finally, the problem was when we keep an edit text in the listview and when try to write inside that, keyboard pushes the listview up. So we need to add the property android:windowSoftInputMode="adjustPan" to the listview or to the activity containing the list view in the manifest file.
Instead of using serialNumber.equals(""), try replacing it with:
serialNumber.length() == 0
or
serialNumber.matches("")
The code you posted is onClick() of arrowButton, you should post onClick() of serialText

Categories

Resources