0

Hope you all are well.

I would like to hide a Button (btnAppointment) if my test readings in my EditText are below or equal 13.5 or greater than 33 in my Android Application window. The Button (btnAppointment) should only appear on the screen if my EditText field inputted values are between 13.60 and 32.99. And not be shown onscreen if outside these parameters.

I was wondering whether an IF statement with button.setEnabled(false); would do the trick and if so where about would I need to input it into my Code. Whether it be protected void onCreate(Bundle savedInstanceState) or Create my own public void appointmentTeacherOnClick?

Below I have inserted my code for calculating and displaying my Inputted Test field prompts.

public void calculateTest(View v){
    String status;
    test = Double.parseDouble(edtData.getText().toString());
    String result = String.format("%.2f", test);
    Log.d("MyActivity", result);

    if( test < 9.5) {
        status = "Normal - Well Done =)";
    } else if (test >= 9.5 && test < 13.5){
        status = "Caution - Keep on Track =|";
    } else if (test >= 13.5 && test < 33.0) {
        status ="Action Needed =(";
    } else {
        status = "Normal Results are between 0 - 33";
    }

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Result Feedback...");
    alertDialog.setMessage(status);
    alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    alertDialog.show();
M.S.R.92
  • 5
  • 4

2 Answers2

1

To hide a view you need to use setVisibility() and set it to View.INVISIBLE

Button btnAppointment = (Button) findViewById(R.id.btn_appointment);
btnAppointment.setVisibility(View.INVISIBLE);

In your XML file:

<Button
    android:id="@id+/btn_appointment
    ...
/>

See this question: How to hide a button programmatically?

Community
  • 1
  • 1
1

if you want to control the visibility of button, enabling or disabling the button won't help. Enabling/Disabling property will still show the button and will only decide whether button can be clicked or not.

You need two things to achieve your task,

  1. EditText - text change listener
  2. Button Visibility property

How to do both the task are already answered on SO, here is the most popular one,

How to hide a button programmatically? (For hiding button).

Counting Chars in EditText Changed Listener (For creating edittext change listener)

Community
  • 1
  • 1
Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36