Enable Button when Spinner and EditText are not empty in Dialog
Please try below code with only one change, put below line in subSpinner.setOnItemSelectedListener and also please declare alertDialog globally.
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
Here is Your Code.
private void openDialog(){
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View subView = inflater.inflate(R.layout.dialog_layout, null);
final EditText subEditTextExchangeRate = subView.findViewById(R.id.textInputExchangeRate);
final Spinner subSpinner = subView.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subSpinner.setAdapter(adapter);
subSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).equals("Select currency")){
currencyName = "";
}else {
currencyName = parent.getItemAtPosition(position).toString();
}
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(subView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
exchangeRate = subEditTextExchangeRate.getText().toString();
st2 = exchangeRate;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
subEditTextExchangeRate.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String exchangeRateInput = subEditTextExchangeRate.getText().toString();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
}//This is the logic that I am looking for but the method is watching only EditText
@Override
public void afterTextChanged(Editable s) {
}
});
}
How to enable button after using spinner
You've not registered a listener on spinner.
Add this in your onCreate
spinner.setOnItemSelectedListener(this);
and then write you code in onItemSelected.
How to lock the spinner after button click in android
Simple just disable the spinner on button click
spinner.setEnabled(false);
Related Topics
Android Edittext Text Control - How to Disable Possibility to Insert Gif from Google Keyboard
Android Check Null or Empty String in Android
Android: Open a Pdf from My App Using the Built in Pdf Viewer
How to Get the Timezone Offset in Gmt(Like Gmt+7:00) from Android Device
How Disable Redirect in Webview
Failure Install Parse Failed Manifest Malformed
Setcontentview(R.Layout.Main); Gives Error from Start
Image Scale Type Center Crop on Flutter
How to Handle Notification When App in Background in Firebase
How to Painlessly Change My App Title Text Color
How to Use Python to Execute Adb Commands
Android Studio - Failed to Apply Plugin [Id 'Com.Android.Application']
Force Override Camera Image on Android
How to Access the Camera from Within a Webview
How to Display a Loading Spinner While Getting Data from Firestore