Display Alert Dialog only when EditText is empty. - java

All- I have an app in which the user inputs values into multiple edittext fields and than the user taps a button to calculate all the numeric inputs into a formula and outputs the answer. My problem is that when one of the edittect fields is left empty the app force closes when the calculate button is hit. I know I can fix this by having an alert dialog box pop up when the calculate button is hit and there are one or more empty edittext fields. I don't know how to put the alert dialog inside of an "if" statement. For example "if any one of the edittext fields are empty .show() this alert dialog". Could someone please give me some sample code.

If you have (or create) an array/list of all the EditText fields, you can run through them like this
for(EditText e : editTextList)
{
if(e.getText().toString().length() == 0)
{
//Show dialog
break;
}
}
You can create a list of EditTexts like this:
List<EditText> editTextList = new ArrayList<EditText>();
and then just add
editTextList.add(myEditText);

Related

I want to add Edit Text dynamically not more not less than 5 Edit Texts in android java

I need to add 5 Input fields (EditText) dynamically one by one on button click and want to take values from them and store them into database using Room Persistence with MVVM.
Here I'm adding the view dynamically
private void addEditTextView() {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
Any suggestion would be very helpful.
Thank you in advance.
Add view based on child count
private void addEditTextView() {
if (binding.layoutList.getChildCount() <= 5) {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
}
"When I clicked Add button it is adding input field one by one, this code is working but I just want to limit for 5 fields not more not less and take values from them."
If you want to add exactly 5 fields on button click I recommend designing a fragment with the 5 fields in place, then when the button is clicked, inflate the fragment into your parent view. Then code the fragment appropriately with the data you're working with.
Then if you wanted, you could deflate the fragment on button click to clear the view or add some other way to clear the fragment when you want. Much easier than what you're doing currently in my own opinion.
You might as well include a submit button in your fragment assuming this is some kind of form.
You can simply define an integer and increase it every time you add the EditText but you should check if your integer is less than 5 everytime the method is called.
Example
private void addEditTextView() {
int count = 0;
if (count < 5){
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
count++;
}
}

How can i display 2 EditText data from array in one Activity in textview....?

When i display one textview it works, but when i display the second, the first textview disappears. Please help.
Here is my code
Intent intent = getIntent();
String[] data = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
TextView name = new TextView(this);
name.setTextSize(25);
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
// Show text view
setContentView(name);
/* AlertDialog dialog = new AlertDialog.Builder(DisplayMessageActivity.this).create();
dialog.setTitle(name);
dialog.setMessage(message);
*/
// Show the Up button in the action bar.
setupActionBar();
Its overwrite the value on TextView.
So Use:
name.setText(data[0]+"\n"+data[1]);
Instead Of:
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
U can also use :
String dataStr="";
for(int i=0;i<data.length();i++)
{
dataStr=dataStr+"\n"+data[i];
}
name.setText(dataStr);
you must set text in one step
name.setText(data[0]+"\n"+data[1])
You are not appending text to your textview. You are actually replacing the text in textview. Before you do name.setText(); just add name.getText().toString();
For example: name.setText(name.getText().toString()+" My New data ");
Though it would be a good practice if you can use a StringBuilder for this. But above code should do the trick.
If you need to display whole array to textview,Just convert the array to string and set as text
name .setText(Arrays.toString(array));

Sending the written text to screen in android

I am just a beginner in android and i created a textbox, user enters some text and presses send button, and i want this text to be sent to screen, like texting message or like whatsapp.
I created a textbox and a button, then i added onClick method named sendAnswer to button and it is like the following:
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
}
where "answer" is the id of editText box. But this does not work. How can i do that?
Also, i will try to do the following in the next step: When user enters a text and sends it, an automatic message will be output to screen by the phone, it will be like chatting with the phone. Whatever user sends will be seen at the right hand side of the screen and whatever phone says will be seen at the left hand side of the screen. Can you give me some idea how to do these? Thank you
If you have installed android samples.
please look at how to use ListView and how to dynamically add items to it.
you will need a ListView for what you want to do.
have a look at
android-sdks/samples/android-8/BluetoothChat
it may help you.
check this example
Manage Items dynamically
It is like one way traffic. You are only getting the text from EditText to display the text on Screen you need to have two things:-
Have a View i.e. TextView, EditText or anything in screen to show the Text other than EditText (answer)
Set editTextStr to that using setText() to that View.
So you function should like this,
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = null;
if(editText!=null){ //Its good to check the return value of findViewById
editTextStr = editText.getText().toString();
}else{
editTextStr = "Answer EditText doesnot exists";
}
TextView textView = (TextView)findViewById(R.id.output); //replace output with the ID of your new output view
if(textView!=null){
textView.setText(editTextStr);
}
Log.d(editTextStr, editTextStr);
}
String editTextStr;
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
calltextview();
}
public void calltextview()
{
TextView textView = (TextView)findViewById(R.id.chat);
textView.setText(editTextStr);
}
make change in the layout like your desired design.try it.
As you want to send the string to the next screen , use Intents
And to my Understanding point of view on your question.
You can use PutExtra(...)
Also You must Check this,
Click here
In the next step , You can Change the position of string in the second activity to match your requirement.
This will surely help you...

Different layouts depending on checkbox

There are 4 checkboxes and a button and as default all checkboxes are checked. Upon button click I need to test to see if any checkboxes are deselcted and if one is then the next activity (that the button launches) needs to hide a text input box. Right now I have it checking like:
if (!picCheck.isChecked()) {
intent.putExtra(PIC_CHECK, 1);
} else {
intent.putExtra(PIC_CHECK, 2);
}
This basically will send the next activity a message with a value of either 1 or 2,
being deselcted
2 being selected.
Then in the next activity I would hide textbox if that message == 1.
On an unrelated note does this look correct?
EditText editText = (EditText) findViewById(R.id.eventNameBox);
String nameMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.eventLocationBox);
String locMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.aboutEvent);
Will this convert whatever is in the eventNameBox, eventLocationBox and aboutEvent into strings? The next code after that is:
intent.putExtra(NAME_MESSAGE, nameMessage);
intent.putExtra(LOCATION_MESSAGE, locMessage);
intent.putExtra(ABOUT_MESSAGE, aboutMessage);
This will work?
If you're just trying to hide the TextView why don't you just use
textView.setVisibility(View.GONE);
this will hide the TextView

How to disable OnClickListerners

I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.

Categories