Visibility Not working with Kotlin - android

Hi the Given below is my Code, where my button should become INVISIBLE but the INVISIBLE is not working
fun onPlay(view: View){
var play = findViewById(R.id.play) as Button
play.isClickable=false
play.visibility=view.INVISIBLE
}

You have a mistake in your code, visibility constant should be set from Class variable, not from argument variable. Change view.INVISIBLE by View.INVISIBLE
fun onPlay(view: View){
var play = findViewById(R.id.play) as Button
play.isClickable=false
play.visibility= View.INVISIBLE // v letter should be capital
}

use play.visibility=View.VISIBLE to visible and play.visibility=View.GONE to invisible or to hide

Related

Simulating a button press with another button press

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()
}

How to check if a view is visible in future?

I have a view. The view might become visible at some time in the future. When this view is visible I want to call a method. How to do this?
val editText = findViewById<EditText>(R.id.editText)
// editText might become invisible in some time in future
// and in some in future it might visible
if(editText.isVisible(){
// code to be executed
}
Code for View.isVisible() :
fun View.isVisible() = this.visibility == View.VISIBLE // check if view is visible
Is there anything like View.setOnClickListener which could be applied and triggered when the view is visible-
editText.setOnClickListener { view ->
}
click listener is callback when the view is being clicked. it has no concern with its visibility. There is no method like isVisible(). to check Visibility
if(yourView.getVisiblity()==View.VISIBLE){ //your task}
for kotlin:
if(youView.visibility==View.VISIBLE){//your task}
I might initialize a variable int status of visibility and set it to 0 with the view invisible.
Now I would create a function instead directly setting the visibility of the view.
For example a function named onVisibilityChanged();
In that function add the set visibility code followed by setting the int to 0 or 1 as per the visibility an if-else block.
If you just set the view to visible, set the int to 1.
The reason for adding if-else block is to configure your actions based on the visibility status.
So that gives you the freedom to do whatever you want bases on the visibility.
Make sure you add this code in such a way that it is executed anytime you want.
Use the performClick() function to click any button or any other view.
I hope you understand. Or comment any query. I would have posted the code for the same but it looks like you are using Kotlin. So I'll try to post it in Kotlin if possible.
The main intention of doing such a thing is when the value of int changes, the app knows what to do and also knows that visibility has changed.
Just call the function wherever you want. It will be easy.
So this is what I am trying to do:
int visibilityStatus;
textview = findViewById(R.id.textview);
getInitialVisibility();
funcOnVisibilityChange();
}
private void getCurrentVisibility() {
if (textview.getVisibility() == View.VISIBLE) {
visibilityStatus = 1;
} else {
visibilityStatus = 0;
}
}
private void funcOnVisibilityChange() {
//Now change the visibility in thia function
textview.setVisibility(View.VISIBLE);
int currentVisibilityStatus;
if (textview.getVisibility() == View.VISIBLE) {
currentVisibilityStatus = 1;
} else {
currentVisibilityStatus = 0;
}
if (visibilityStatus != currentVisibilityStatus) {
//Visibility status has changed. Do your task
else {
//visibility status not changed
}
}
}
So all that we are doing is getting visibility of the view when the app is started and when you somehow change its visibility. Here in the example, I've directly changed the visibility. So wherever you know that visibility is going to change just put the funcOnVisibilityChange() and it will do your job... hope it helps. Let me know if you need more clarification.

Bottom OnClickListener always unresolved

I have a simple button in my recyclerview, when clicked the first time it should make the text editable, when clicked the second time, it should confirm the change. The problem I'm having is that I have the two onClickListeners set up, but they refer to each other, and the bottom one can always resolve the top one, but the top one can't resolve the bottom one.
Recyclerview: bindIngredient
fun bindIngredient(ingredient: ListIngredientsQuery.Item, clickListener: RecyclerViewClickListener) {
val ocl1 = View.OnClickListener{
//Text Editable
view.ingEditText.setText(view.ingNameTV.text.toString())
view.ingNameTV.visibility = View.GONE
view.ingEditText.visibility = View.VISIBLE
view.ingEditButton.text = "Confirm"
view.ingEditButton.setOnClickListener(ocl2)
}
var ocl2 = View.OnClickListener {
//Text Not Editable
view.ingNameTV.text = view.ingEditText.text
view.ingEditText.visibility = View.GONE
view.ingNameTV.visibility = View.VISIBLE
view.ingEditButton.setOnClickListener(ocl1)
clickListener.onConfirmSelect(ingredient)
}
this.ingredient = ingredient
view.ingNameTV.text = ingredient.name()
view.ingEditButton.setOnClickListener(ocl1)
view.veganSpinner.setSelection(Vegan.valueOf(ingredient.vegan().toString()).ordinal, false)
view.gfSpinner.setSelection(GlutenFree.valueOf(ingredient.glutenfree().toString()).ordinal, false)
}
In this example the line
view.ingEditButton.setOnClickListener(ocl2)
errors because ocl2 is unresolved. If I switch the order of the two onClickListeners being declared and initialized, the line
view.ingEditButton.setOnClickListener(ocl1)
errors because ocl1 is resolved. I take this to mean that it won't look further down to find what it needs, it'll only rely on objects that have already been initialized.
Is there a way to fix this? Is there a better way to do this? I'm tempted to just put two buttons in the same spot, give them each their own onclicklistener and swap their visibility, but this seems like a waste of resources.
You need to declare your objects before you use them.
fun bindIngredient(ingredient: ListIngredientsQuery.Item, clickListener: RecyclerViewClickListener) {
val ocl1: View.OnClickListener
val ocl2: View.OnClickListener
ocl1 = View.OnClickListener{
//Text Editable
view.ingEditText.setText(view.ingNameTV.text.toString())
view.ingNameTV.visibility = View.GONE
view.ingEditText.visibility = View.VISIBLE
view.ingEditButton.text = "Confirm"
view.ingEditButton.setOnClickListener(ocl2)
}
ocl2 = View.OnClickListener {
//Text Not Editable
view.ingNameTV.text = view.ingEditText.text
view.ingEditText.visibility = View.GONE
view.ingNameTV.visibility = View.VISIBLE
view.ingEditButton.setOnClickListener(ocl1)
clickListener.onConfirmSelect(ingredient)
}
this.ingredient = ingredient
view.ingNameTV.text = ingredient.name()
view.ingEditButton.setOnClickListener(ocl1)
view.veganSpinner.setSelection(Vegan.valueOf(ingredient.vegan().toString()).ordinal, false)
view.gfSpinner.setSelection(GlutenFree.valueOf(ingredient.glutenfree().toString()).ordinal, false)
}
However, it would be better if you just used one OnClickListener. You can simply save which state you are in, and when the button is clicked, you just check which state you are in, perform your action, and then change the state. This way you don't have to worry about switching your listeners, which can get messy.

Kotlin - Can One Button Apply 2 Action?

I want to make a Button that can apply 2 action. For example, when i press the button-- it makes the ImageView VISIBLE, and when i press it again-- the ImageView goes INVISIBLE, just like switch. Is it really possible?
val btn1 = findViewById<Button>(R.id.btn1)
btn1.setOnClickListener {
labu.visibility = View.VISIBLE
}
In Kotlin it's just 1 line of code to toggle the visibility of a View:
btn1.setOnClickListener {
labu.visibility = if (labu.visibility == View.VISIBLE) View.INVISIBLE else View.VISIBLE
}

How to check visibility status of EditText in android?

I am changing EditText visibility from invisible to visible by using setvisibility(View.INVISIBLE) and setvisibility(View.VISIBLE). But i also want to know is there any method provided in android to check EditText's visibility i.e is EditText is visible or any of that kind.
Thanks in advance.
You should be able to retrieve that by calling the method isShown() on your EditText.
you can try this way also ...
if(edittextname.getVisibility() == View.VISIBLE)
{
}
or
if(edittextname.getVisibility() == View.INVISIBLE)
{
}
Try to use this method:
isShown();
Kotlin approach -
val editText = findViewById<EditText>(R.id.editText)
if(editText.isVisible(){
// do your stuff
}
Method -
fun View.isVisible() = this.visibility == View.VISIBLE // check if view is visible
fun View.isNotVisible() = !this.isVisible()
fun View.beVisible() = this.visibility = View.VISIBLE // sets view visible
fun View.beGone() = this.visibility = View.GONE. // sets view gone

Categories

Resources