A button clickable when conditions are met (Android Studio) - android

Is there a feature that I can make a button clickable only when a certain condition is met?
For instance, I can ask users to swipe 10 times, and only after users swipe 10 times, a button is clickable to receive an award.
Thank you for your help in advance!

Disable the button (instead of disabled you can set isFocused = false):
button.isClickable = false
button.isEnabled = false
Add swipe-listener. Inside it increment a counter. After it becomes 10, set:
button.isClickable = true
button.isEnabled = true
Also inside a click-listener you can check the counter >= 10.

Related

Is there any way to detect the visibility of Keyboard in Android TV Leanback

I am trying to detect whether the keyboard is visible or not. The standard way of detecting the keyboard in mobile does not work in Android TV.
Any suggestions?
May I ask why you want to know the keyboard status?
If i had no other way i would:-
When you want the keyboard open ... force close and open and vice versa
Create a global boolean flag that would change depending on whether a textfield is clicked .. if clicked, boolean = true ... if keyboard opened(blooean = true), and back button pressed then set boolean = false ...
What I was looking for something like this
fun isKeyBoardVisible() = ViewCompat.getRootWindowInsets(requireView())?.isVisible(WindowInsetsCompat.Type.ime()) ?: false

How to enable or disable buttons in bulk?

I have 30 buttons in a page for each time slot like 9AM, 10AM ... for 3 days. I have to enable or disable the button based on the availability. Is there any easier way to enable or disable buttons at a time or i just have to follow the same method of using the button.setVisibility(view.GONE) for each button?
After initializing your 30 buttons, u can collect them in a List<Button>. For changing their visibility in bulk, u can just iterate through your list in a for loop and change their visibility. Something like below:
List<Button> buttons = new ArrayList<>();
buttons.add(button1);
buttons.add(button2);
........
private void setButtonsVisibility(boolean isVisible) {
if (buttons != null) {
for (Button button : buttons) {
button.setVisibility(isVisible ? View.VISIBLE : View.GONE);
}
}
}
You can try the following methods
If the buttons have same parent layout you can set the visibility to View.VISIBLE or View.GONE of the parent layout and it will be reflected to the button too.
If not than you have to set the visibility of each button or set the enabled property to true or false according to the condition.
Hope this works.

Android button changing color on second time clicked

I am making a game that changes the color and backgrounds of buttons. When the app opens I run the newGame function. Then after the game ends, to replay the game I recall the newGame function again. However, every time the game is replayed, when a button is clicked, it turns orange for a second before doing what it is supposed to do.
fun newGame() {
allButtons.forEach {
it.text = ""
it.setBackgroundResource(android.R.drawable.btn_default)
it.isEnabled = true
}
gameInProgress = true
}
At other points in the code I'll change the background or enabled property of the buttons using some of the following lines code
button.setBackgroundResource(R.color.zeroColor)
button.setBackgroundResource(R.drawable.flag)
button.isEnabled = false
Any ideas?

Disable android edittext but allow clicks, or ellipsize enabled edittext

So, I have an EditText in a ScrollView. Because this EditText can get quite long, we have decided to display only 6 lines at a time, ellipsize the rest, and then display a modal alert dialog to view/edit the full EditText.
However, this brings up two issues that I can't seem to get around. There are times when we need this EditText to be disabled - (after the screen it is on has been finalized).
However, as far as I can tell, if an EditText is disabled, its OnClickListener doesn't receive events. So my solution to that was to write this extension method:
fun EditText.disableButRemainClickable(softDisabled: Boolean) {
if (softDisabled) {
alpha = 0.5f
isFocusable = false
isFocusableInTouchMode = false
isLongClickable = false
isCursorVisible = false
} else {
alpha = 1f
isFocusable = true
isFocusableInTouchMode = true
isLongClickable = true
isCursorVisible = true
}
}
This solves that problem. However, I now have a new problem. The ellipsize property doesn't work on an editText that is enabled. What can I do?
Maybe, you can extend the EditText class and create your own CustomEditText class to maintain a state whether it is enabled or disabled.
Whenever you get the callback for onClick, you disable it and show the Dialog.
I think this might solve your problem.

Why fastforward/rewind button first time working too slow

I am working on video player using video node. My issue is when i press first time fastforward/rewind button during video playing/buffering then button is not working. After pressing 4-6 times fast forward or rewind button is working after that it is working properly but for first time i have to press 4-6 time button then working. My code is...
function setVideo()
m.InnerVideo = m.top.createChild("InnerVideo")
inner = createObject("RoSGNode", "ContentNode")
inner.url = "url..."
inner.streamformat = "hls"
m.innerVideo.visible = true
m.innerVideo.content = inner
m.innerVideo.control = "play"
end function
Event handler code is...
function onKeyEvent(key as String, press as Boolean) as Boolean
handled = false
if press
if key = "fastforward"
print "fastforward"
handled = true
end if
end if
return handled
end function
Please suggest me what should be issue? Is issue related to video file format or encoding/decoding or others?
On onKeyEvent Function, you print above of handled = false "?key" value and check which value print in here. and also check "?press" if true then pressed key and false then not pressed. Here the Best way to whatever happening in your onKeyEvent Function. Like below
function onKeyEvent(key as String, press as Boolean) as Boolean
? "Key Event is about to execute - key = "key " press = " press
end function
If prints from onKeyEvent are printed after 5th or 6th time maybe You have a problem with focus. It could be that You player is not in the focus at first and then at some point You assign a focus to it.
Try to add: m.InnerVideo.setFocus(true) in your setVideo() function.
If it does not work check if there is something else that is taking the focus off the m.InnerVideo after setVideo() function is executed.

Categories

Resources