I am trying to disable okButton till the edit text is empty.I couldn't find a solution to it. I tried adding text watcher but I don't know how to disable positive button in that.
val inputTxt = EditText(this)
alert("Enter your mobile number") {
customView = inputTxt
inputTxt.setInputType(InputType.TYPE_CLASS_NUMBER)
inputTxt.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(10)))
inputTxt.setRawInputType(Configuration.KEYBOARD_12KEY);
okButton {
startActivity(intentFor<NewActivity>()
}
isCancelable = false
cancelButton { finish() }
}.show()
This is not possible (in a clean way) with the AlertDialog API itself, but you could have your own "OK" button in the custom view that you've added. The main risk is that it is not garanteed to look exactly (same margins and paddings) as the regular "OK" button.
Related
I would like to programmatically click an editText once a button has been pressed.
When a radio button "pitot_area" is selected, I want the editText associated with this selection to be programmatically clicked, so the user has one less "click" on the screen and it happens automatically.
So far I have tried
performClick()
callOnClick()
requestFocus()
the area i would like this code to be within is below:
root.pitot_area.setOnClickListener {
setAreaLabels(root)
evNodeItem.kvParams = false
evNodeItem.PitotRectArea = false
evNodeItem.PitotRoundArea = false
evNodeItem.areaParams = true
root.area_edit_text.requestFocus()
another method I have tried is
root.area_edit_text.setFocusableInTouchMode(true);
root.area_edit_text.setFocusable(true);
root.area_edit_text.requestFocus();
for the above lines of code, I have in the .xml file for the editText
android:focusable="false"
android:focusedByDefault="false"
An error does not show and the code builds, it seems this code is not read in kotlin? Does anyone have any ideas on how to do this? Thanks!
edit
ive looked at-> Focus Edit Text Programmatically (Kotlin) question sugggested below and was unable to implement it for my code.
To set direct focus to the EditText you have to open keyboard after setting focus to the EditText.
write this lines into your button click:
For Activity:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}
For Fragment:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = activity!!.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}
I currently want to have a button that when I press it another button flashes as if it has been pressed at the same time and activates the function of the other button. My current code is as such:
fun onTwo(view: View) {
button1.callOnClick()
button1.isPressed = true
}
However the issue I am facing is that it freezes button1 as if it is pressed until it is pressed again. Anyone have a solution for this?
You could add listener in one of the button to check for clicks and then use that event to trigger click event in another button, like this:
val button1 = findViewById(R.id.btn1ID) as Button
button1.setOnClickListener {
val button2 = findViewById(R.id.btn2ID) as Button
button2.performClick()
}
Replace R.id.btn1ID and R.id.btn2ID with their respective id(a).
Reference: performClick()
You could also create a utility function to use it without making redundant variables like this:
#Suppress("UNCHECKED_CAST")
fun Activity.findButtonById(#IdRes res : Int) : Button =
findViewById(res) as Button
// and then in your create method of activity:
findButtonById(R.id.btn1ID).setOnClickListener {
findButtonById(R.id.btn2ID).performClick()
}
Try performClick() method like below:
fun onTwo(view: View)
{
button1.performClick()
}
I ended up fixing this issue using coroutines in the end as such:
fun onTwo(view: View){
GlobalScope.async{
delay(100)
button1.isPressed = false
GlobalScope.cancel()
}
button1.setPressed(true)
button1.performClick()
}
I have a alert view with custom layout which contains EditText
activity.alert {
var quantityEt: EditText? = null
customView {
linearLayout {
orientation = LinearLayout.VERTICAL
quantityEt = editText {}
quantityEt!!.requestFocus()
Log.e("LOL", ac.window.currentFocus.toString())
}
}
}.show()
then I request focus to this edit text. However, activity.window.currentFocus outputs that the currentFocus android.support.v7.widget.RecyclerView.
I need the currentFocus to be EditText. I also tried to tap it to request focus, but still got RecyclerView.
Is there a way to achieve that?
I am currently trying to work with AlertDialogs.
Currently, I have an EditText in my AlertDialog, and want to restrict the input to only Integers. I have used a basic try and catch block to avoid the app crashing form a NumberFormatException.
However, I want to set it up so that when the user tries to press the button with the incorrect input, the input does not register and the Dialog is not cancelled.
UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
int RawInput = settings.getInt("UpperLimit", 12);
try {
RawInput = Integer.parseInt(input.getText().toString());
}catch (NumberFormatException se) {
Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
//Here I want the app to not register the click and prevent the dialog box from closing.
}
editor.putInt("UpperLimit", RawInput);
editor.apply();
Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
}
});
What method can I use to achieve this?
The basic trick here is to use the dialog's setCancelable to false and call dismiss() only when the input has been validated.
More info here: AlertDialog with positive button and validating custom EditText
Use can acheive input to only Integers this by both Java or XML.
Java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
XML
android:inputType = "numberSigned"
this may helps you in both way.
If you restrict user to allow only number then you can simply try with-
android:inputType="number"
and if you want to allow with floating number then try with-
android:inputType="numberDecimal"
and in any case if you want to disable dialog button then try this-
Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setEnabled(false);
will work for you :)
Simply use in EditText's xml code:
android:inputType="numberSigned"
This will let user input only integers. Hope this helps!
I want to listen for clicks on the positive button of an AlertDialog, which I have disabled by calling button.setEnabled(false);.
How should I do this? If this is not possible, is there a known workaround?
PS. The reason I want to do this, is that I want to show a toast when somebody hits the button, saying "You need to do this before you can continue".
This is not a way to listen for clicks on a disabled button. This is a workaround.
I liked the result I got by changing the color of the button, making it look like it's disabled.
What you want to do:
// Instantiate positive button
final Button posButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
// Save the original button's background
final Drawable bg = posButton.getBackground();
// Set button's looks based on boolean
if (buttonDisabled) {
posButton.setTextColor(getResources().getColor(R.color.disabledButtonColor, null));
// R.color.disabledButtonColor == #DBDBDB, which is pretty close to
// the color a disabled button gets.
posButton.setBackgroundColor(Color.TRANSPARENT);
// Color.TRANSPARENT makes sure all effects the button usually shows disappear.
} else {
posButton.setTextColor(getResources().getColor(R.color.colorPrimaryDark, null));
// R.color.colorPrimaryDark is the color that gets used all around my app.
// It was the closest to the original for me.
posButton.setBackground(bg);
// bg is the background we got from the original button before.
// Setting it here also re-instates the effects the button should have.
}
Now, don't forget to catch your buttons actions whenever it's "disabled"
public void onClick(View v) {
if (buttonDisabled) {
// Button is clicked while it's disabled
} else {
// Button is clicked while it's enabled, like normal
}
}
That should do, have fun with it.