'How can I program a while loop that waits for my input in Android?

My question is simple. What method can I use to tell my program that a button is pressed? I'm trying some codelines but its not really working (I tryed with isPressed). in my logs I can read the line --> TAMAÑO DEL CONTADOR: < the numbers until it reaches max.> before I can I even place a value, so I understand the loop doesnt wait for my inputs.

Here is my code:

public class navegador extends AppCompatActivity {

public String Sdat;
public EditText ET2;
int tam;

ArrayList<String> Medidas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navegador);

    ET2 = findViewById(R.id.ET1);
    Button bot = findViewById(R.id.bot);

    tam = getIntent().getIntExtra("numero",0);

    Medidas = new ArrayList<>();
    int contador = 0;

    System.out.println("TAMAÑO DEL ARRAY: "+ tam);

    while ( contador <= tam){

        System.out.println("TAMAÑO DEL CONTADOR: " + contador);

        bot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View bot) {
                if(bot.isPressed()){
                    MeterMedida();
                }
            }
        });
        contador++;

        if(contador == tam){

            Toast.makeText(this, "Distancia máxima alcanzada. Toca Crear tabla.", Toast.LENGTH_SHORT).show();

        }
    }
}

public void MeterMedida(){

    Sdat = ET2.getText().toString();
    Medidas.add(Sdat);
    ET2.setText("");

}

public void LanzarLista (View view){

    Intent A = new Intent(this, Lista.class);
    A.putStringArrayListExtra("Lista", Medidas);
    startActivity(A);
}
}

Thanks a lot, ask me for more information if you think you need it.

EDIT

As usual, less is more, I removed the while, and contador variable, now it works like I wanted and its pretty much simple. Thank you so much.



Solution 1:[1]

You don't need a while loop:

    //a list to keep all the entered floats
    private List<Float> floats = new ArrayList<Float>();

    //each time you click the listener is invoked
    bot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View bot) {

                //every time the button is clicked, fill the current typed float


             floats.add(Float.parseFloat(ET2.getText().toString()));
             ET2.setText("");

        }
    });

Solution 2:[2]

You said that you wanted to tell your app when a button is pressed, do something. There's no need to use isPressed() method. Just do it like this.

 bot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View bot) {     
            //When you press this button (bot), codes here will be executed.
            MeterMedida();

        }
    });

Solution 3:[3]

Well, I’m not gonna give complete answer. Just a few hints for yourself to figure out.

  1. Declare int contador = 0; in class level
  2. Remove while ( contador <= tam)
  3. Remove if(bot.isPressed())
  4. Remove contador++; in your onCreate
  5. Inside MeterMedida() check for if(contador <= tam). If true, add to ArrayList and increment contador. Else show error Toast.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 HotJava
Solution 3 Gokul Nath KP