So I made a custom dialog where it asks for information(like a feedback.
I want the app to check if all the EditTexts have value,
this is my java for opening feedback dialog
private void dialog() {
myDialog.setContentView(R.layout.activity_pop_up);
editTextEmailValue = (EditText) myDialog.findViewById(R.id.editTextEmail);
editTextMessageValue = (EditText) myDialog.findViewById(R.id.editTextMessage);
editTextNumeValue = (EditText) myDialog.findViewById(R.id.editTextNume);
Button btnSend = (Button) myDialog.findViewById(R.id.sendBtn);
Button btnClose = (Button) myDialog.findViewById(R.id.close);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This is what I've tried (the check method is bewlow)
if (check() > 1) {
editTextNumeValue.setError("Obligatoriu!");
editTextMessageValue.setError("Obligatoriu!");
editTextEmailValue.setError("Obligatoriu!");
} else if (check() == 0) {
sendMail();
}
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
And this is my check method :
private int check() {
int counter = 0;
if (editTextEmailValue.equals("")) {
editTextEmailValue.setError("Obligatoriu!");
counter++;
} else if (editTextMessageValue.equals("")) {
counter++;
editTextMessageValue.setError("Obligatoriu!");
} else if (editTextNumeValue.equals("")) {
editTextNumeValue.setError("Obligatoriu!");
counter++;
}
return (counter);
}
When I press the send button(for email) it enters the check method and goes out with 0 1 2 3 (I have only 3 textView) but when it checks in the btnSend.onClickListener if the condition are met it still send the mail so from that I can say from check method the counter have value 0 all the time so can you help me?
Your logic above is wrong. You are checking
if(counter > 1)
but your counter will never be greater than 1. Change it to
if(counter > 0) or if(count >= 1)
also use editText.getText().toString() for the correct editText entries !
and use .isEmpty() instead of .equals("")
i agree with Luis Fernando Scripcaru .
and i suggest you one thing try editTextEmailValue.getText().toString().equals("") instead of editTextEmailValue.equals("").
You can remove your check() method and try like this It might work :)
int Counter = 0;
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This is what I've tried (the check method is bewlow)
if(TextUtils.isEmpty(editTextEmailValue)){
editTextEmailValue.setError("Error");
Counter = 1;
}
if(TextUtils.isEmpty(editTextMessageValue)){
editTextMessageValue.setError("Error");
Counter = 2;
}
if(TextUtils.isEmpty(editTextNumeValue)){
editTextNumeValue.setError("Error");
Counter= 3;
}
if(Counter > 1){
sendMail();
//you can add other operations for 123 numbers too
}
}
});
You check
if (check() > 1)
It will never be true. Because check() has IF ELSE, so only one increment will be performed and return value will never be more than 1, maximum value of our check is 1.
so, change this to
if (check() > 0)
or
if (check() >= 1)
Related
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editTextName);
textView = findViewById(R.id.textViewOne);
button = findViewById(R.id.buttonOne);
view = findViewById(R.id.snackbar_action);
String x = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {//Checking if edit text field is left empty.
if (editText.getText().toString().equals("")) {
Snackbar.make(view, R.string.text_label, Snackbar.LENGTH_SHORT).show();
}//Checking if editText field is numeric.
else if (TextUtils.isDigitsOnly(x)){
Snackbar.make(view, R.string.text_number, Snackbar.LENGTH_SHORT).show();
} else {
textView.setText(NATOConverter("" + editText.getText().toString()));
}
}
});
}
Hello guys im struggling trying to figure out how to check if a my edittext value contains letters and numbers im trying to display a snackbar if the user enters for example "JDF23D" telling them to remove the numbers but my error is no matter what it stays on that else if what would you suggest?
Try next code
if (editText.getText().toString().matches(".*[0-9].*")){
//some code
}
else{
//some code
}
If you want to check whether the string contains a number, you should use this,
public boolean containsDigit(String x){
for(int i = 0; i < x.length(); i++){
if(48 <= (int) (x.charAt(i)) <= 57)
return true;
}
return false;
}
Returns true if even one character is a number, else false.
Try more simplest
boolean match = Pattern.compile("\\d+").matcher(s).find();
You use this method:
TextUtils.isDigitsOnly(x)
but this method checks if your string only contains digits. So your example "JDF23D" will return false.
You can take a look at this post
So I am trying to create a multiple choice quiz, that will generate a fitness program based on your choices. I have three multiple choice buttons, and each time a question is answered, the question and the answers are rotated. I am trying to grab the answer that the user selected for each question, however my program is not allowing me to correctly grab the data.
I have tried using getters and setters but my program still cannot grab the information
private TextView blank;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
String experience;
String preference;
int days;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blank = (TextView) findViewById(R.id.program);
mButtonChoice1 = (Button)findViewById(R.id.choice1);
mButtonChoice2 = (Button)findViewById(R.id.choice2);
mButtonChoice3 = (Button)findViewById(R.id.choice3);
mButtonChoice1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice1.getText() == "Two"){
//QUESTION 1 ANSWER_1
updateQuestion();
days = 2;
}
else if (mButtonChoice1.getText() == "0-6 months") { //QUESTION 2 ANSWER_1
updateQuestion();
experience = "0-6 months";
}
}
mButtonChoice2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice2.getText() == "Three"){ //QUESTION 1 ANSWER_2
updateQuestion();
days = 3;
}
else if(mButtonChoice2.getText() == "6-18 months"){ //QUESTION 2 ANSWER_2
updateQuestion();
experience = "6-18 months";
}
}
}
mButtonChoice3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice3.getText() == "Four"){ //QUESTION 1 ANSWER_3
updateQuestion();
days = 4;
}
else if (mButtonChoice3.getText() == "1.5+ years") { //QUESTION 2 ANSWER_3
updateQuestion();
experience = "1.5+ years";
}
}
}
//here i am just testing if my program is able to receive the data that was entered for each variable
if (day == 2 && preference == "Strength" && experience == "0-6 months") {
blank.setText("test working");
}
else {
blank.setText("Test not working");
}
}// oncreate
However, right when I start the program, before I even choose any of the choices, the test testVIew section already states "test not working".
My program will be able to get the value of the variable if I place the if statement inside the onclickListener function, however, I need the if statement at the bottom because I need to account for all cases of each variable, and cannot put the if statement in the onclick methods.
I WANT TO BE ABLE TO GET THE values of the variables preference,days,experience outside of the onClickListener
Try to:
if (day == 2 || preference == "Strength" || experience == "0-6 months")
or
if(day ==2 ){
statements;
}
else if(preference == "Strength"){
statements;
}
else if(experience == "0-6 months"){
statements;
}
else {
blank.setText("Test not working");
}
In your onCreate, you juste set the listeners in the button, when you check the answers, the activity is still not displayed on screen. the activity do the checks before you can see the buttons etc because the checks are in onCreate.
You should do the checks after the buttons have been pressed. For instance, in your updateQuestion() you detect that the last question has been answered and you make the checks and display the result at that time.
I have 1 button in activity. i want to use this 1 button for multiple task.
So how can i do ?
If i pressed 1st time this button then it's change 2 button
if i pressed 2nd time then it's update my data
but it's only work 1st time 2nd time it's not work
see my code what i tried
Intent extras = getIntent();
{
if (extras.hasExtra("edit")) {
if (extras.getStringExtra("edit").equals("home")) {
etCompanyName.setEnabled(false);
etWebsite.setEnabled(false);
etEmail.setEnabled(false);
etPhoneHome.setEnabled(false);
etPhonePrimary.setEnabled(false);
etAddressLine1.setEnabled(false);
etAddressLine2.setEnabled(false);
etCity.setEnabled(false);
spStates.setEnabled(false);
etZip.setEnabled(false);
spContries.setEnabled(false);
//1st time use hear
txtSave.setText(getResources().getString(R.string.label_edit));
txtClose.setText(getResources().getString(R.string.label_back));
txtSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSave.setText(getResources().getString(R.string.label_add));
txtClose.setText(getResources().getString(R.string.label_cancel));
etCompanyName.setEnabled(true);
etWebsite.setEnabled(true);
etEmail.setEnabled(true);
etPhoneHome.setEnabled(true);
etPhonePrimary.setEnabled(true);
etAddressLine1.setEnabled(true);
etAddressLine2.setEnabled(true);
etCity.setEnabled(true);
spStates.setEnabled(true);
etZip.setEnabled(true);
spContries.setEnabled(true);
}
});
if (extras != null) {
Company value = (Company) extras.getSerializableExtra("company");
etCompanyName.setText(value.getName());
etWebsite.setText(value.getWebsite());
etEmail.setText(value.getEmail());
etPhoneHome.setText(value.getPhoneHome());
etPhonePrimary.setText(value.getPhonePrimary());
etAddressLine1.setText(value.getAddressLine1());
etAddressLine2.setText(value.getAddressLine2());
etCity.setText(value.getCity());
etZip.setText(value.getZipcode());
}
} else {
//2nd time use hear
txtSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Company company = new Company();
company.setName(etCompanyName.getText().toString().trim());
company.setWebsite(etWebsite.getText().toString().trim());
company.setEmail(etEmail.getText().toString().trim());
company.setPhoneHome(etPhoneHome.getText().toString().trim());
company.setPhonePrimary(etPhonePrimary.getText().toString().trim());
company.setAddressLine1(etAddressLine1.getText().toString().trim());
company.setAddressLine2(etAddressLine2.getText().toString().trim());
company.setZipcode(etZip.getText().toString().trim());
company.setCity(etCity.getText().toString().trim());
company.setState(spStates.getSelectedItem().toString());
company.setCountry(spContries.getSelectedItem().toString());
company.setDate(Util.getInstance(AddCompanyActivity.this).getCurrentDate());
long isUpdated = myDb.updateCompany(company);
if (isUpdated != -1) {
Toast.makeText(getApplicationContext(), "Company Update Successfully: " + isUpdated, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Something wrong", Toast.LENGTH_SHORT).show();
}
finish();
}
});
}
}
}
You can see my above code i can used txtSave button for perform 2 task but it's only change two buttons and i'll change data and click on button then it's can't perform
Try this way, first declare global variable on your activity class file like below :
int count = 0;
After that add your click listener like that:
yourButton.setOnClickListener(v -> {
if (count == 0) { // the first click
count++;
// do your stuff
}else { // the second click
count = 0; // initialize the count to limit the button click just for the first and the second time only
// do your stuff
}
});
You should not create multiple OnClickListener for Button , Create only 1 and use it
example:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().equals("1")){
//perform action for 1
btn.setText("2");
//change button1 to button2
}else if(btn.getText().equals("2")){
//perform action for 2
btn.setText("3");
}
}
});
you could use single onclicklistener with switch case
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
swtich(extras.getStringExtra().toLowerCase(){
case "1":
// do something
break;
case "2":
// do something else
break;
}
});
}
I have an array and two buttons ( Next and Previous ).
When you click on the next button the mCurrent index updates(++) and the Cursor points to the next item in the list, and the opposite for the previous button.
String[] fruits = {"Pineaple", "Orange", "Banana", "Apple"};
int mCurrentIndex = 0;
This is the event handlers for the buttons:
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % fruits.length;
updateFruit();
}
});
prevButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex - 1);
if(mCurrentIndex < 0){
mCurrentIndex = fruits.length - 1;
}
updateFruit();
}
});
The code is working normal. But I want to find out whether there could be a way to refactor the previousButton code to be like the nextButton code(Making it shorter), by eliminating the if statement and replacing with something like inverse of a modulus (that is if it exists) and it will still work the same.
In each case the mCurrentIndex is reset when it reaches the end of the array.
The code is working normal. But I want to find out whether there could
be a way to refactor the previousButton code to be like the nextButton
code(Making it shorter), by eliminating the if statement and replacing
with something like inverse of a modulus (that is if it exists) and it
will still work the same.
mCurrentIndex = (mCurrentIndex + fruits.length - 1) % fruits.length;
should do that
I think you can use
public void onClick(View view) {
mCurrentIndex = (fruits.length + (mCurrentIndex - 1)) % fruits.length;
updateFruit();
}
Use one method,
updateFuit(int refIndex) {
mCurrentIndex = (mCurrentIndex + refIndex) % fruits.length;
// Do rest of the updates..
}
For buttons,
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateFruit(1);
}
});
prevButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateFruit(fruits.length - 1);
});
I have a for loop, but I need it to stop doing anything until I get a response from the user (aka the user selects left or right). I'm doing this in Android, but I'm not sure of the best way to go about this. The "left" and "right" are images. So I'm listening for left or right to be hit before I keep moving on. I need the loop to go on about 50 times. This is what I have so far.
int testlength = 50;
for (int i = 0; i < testlength; i++) {
left.setImageResource(R.drawable.left);
right.setImageResource(R.drawable.right);
//Stop the for loop and get the input from the user
}
The ideal solution in my opinion would be to use semaphores:
class Example {
private Semaphore sem;
public Example() {
sem = new Semaphore(0);
}
public void onClick(...) {
// Do stuff
sem.release();
}
public void myLoop() {
int testlength = 50;
for (int i = 0; i < testlength; i++) {
left.setImageResource(R.drawable.left);
right.setImageResource(R.drawable.right);
sem.acquire();
}
}
}
Remember that this assumes that myLoop isn't running on the UI thread.
You don't need a loop.
Just exit the routine once you have 50 responses:
int count = 0;
String[] responses = new String[50];
left.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
left.setImageResource(R.drawable.left);
right.setImageResource(R.drawable.right);
responses[count] = "Left";
count++;
if(count == 50)
{
// Exit the activity
}
}
};
right.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
left.setImageResource(R.drawable.left);
right.setImageResource(R.drawable.right);
responses[count] = "Right";
count++;
if(count == 50)
{
// Exit the activity
}
}
};
You want to do something like this -
ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
That is, you want to use an event rather than loop waiting for someone to click. Tight loops are generally a nasty idea.
I got the code from this thread - how to apply click event listener to image in android