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
}
Related
I have a LinearLayout with 7 FAB's inside, and I'm trying to change their background tint color by clicking, the thing is, the first FAB does trigger and change the color but the rest doesn't and also, if I click in the first FAB and then in the rest of them, they change the tint color, but doesn't trigger! I tried to debug but nothig..
This is what I did.
val monday: FloatingActionButton = dialogLayout.findViewById(R.id.monday);
val tuesday: FloatingActionButton = dialogLayout.findViewById(R.id.tuesday);
monday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){
monday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
monday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
tuesday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){
tuesday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
tuesday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
It's very weird, but I think it has to do with the view it self as if the first item registered surpass the other ones.. I don't know!
This is your problem:
tuesday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and ----------> monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){ <----------------------
tuesday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
tuesday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
You are comparing to Monday's state and not Tuesday's. I am guessing this happens in the rest of your buttons.
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.
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
I'd like to be able to show or hide a tableview with the click of a button. I know how to set the visibility of the view, just not how to toggle back and forth.
Edit: Another option that seems to work for anyone else needing to do this:
showHide.Click += delegate
{
if (otherEquip.Visibility == ViewStates.Visible)
{
otherEquip.Visibility = ViewStates.Invisible;
}
else
{
otherEquip.Visibility = ViewStates.Visible;
}
};
There is no built in toggle method that I know of. There are also three visibility states a view can have - visible, invisible, gone - so "toggling" doesn't really work there. If you wanted to swap between invisible and visible, for example, you could do something like:
view.Visibility = view.Visibility == ViewStates.Invisible
? ViewStates.Invisible
: ViewStates.Visible;
Or if you wanted to make it more reusable you could put it in an extension method:
public static class ViewExtensions
{
public static void ToggleVisibility(this View view)
{
view.Visibility = view.Visibility == ViewStates.Invisible
? ViewStates.Invisible
: ViewStates.Visible;
}
}
and then call on the view:
view.ToggleVisibility();
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