How to check visibility status of EditText in android? - 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

Related

Android/Kotlin - adding TextInputLayout.END_ICON_CLEAR_TEXT to textview doesn't perform the setOnFocusChangeListener

i've a problem in android app written in Kotlin.
I've a textview that has a setOnFocusChangeListener and it worked fine as long as i've added an EndIconMode.
Now in my layout i see the END_ICON_CLEAR_TEXT and this works, but nothing happens when this textview loses focus.
How can I made this works?
textInputLayout = LayoutInflater.from(activity).inflate(R.layout.textinputlayouttemplate, null) as TextInputLayout
val textView = CreateUI._TextView(castToField(field), textInputLayout.context)
if (field.validatefield || field.nextpage != 0)
{
textView.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus && !GlobalVar.drawerOpened)
{
if ((field.mandatory && !textView.text.toString().isNullOrEmpty()) || (!field.mandatory)) {
if (field.validatefield) {
validateField(field.fieldcode, textView.text.toString(), field.nextpage)
} else {
_goToNextPageNo = field.nextpage
goToNextPageIfExist()
}
}
}
}
}
textInputLayout.addView(textView, layout_wMATCHPARENT_hWRAPCONTENT)
textInputLayout.layoutParams = layout_wMATCHPARENT_hWRAPCONTENT
textInputLayout.setPadding(0, 0, 0, 0)
textInputLayout.endIconMode = TextInputLayout.END_ICON_CLEAR_TEXT
linearLayout.addView(textInputLayout)
Not sure if this is still relevant for you but apparently this is because setting the endIconMode programatically will currently override onFocusChangeListener.
You will even have issues if you set this property via xml and set your own onFocusChangeListener. If you not invoke the previous set listener aswell the clear-icon will perform not as intended.
Either you set it via xml and invoke the default listener or use endIconMode="custom" and write your own logic.
Here is a similar issue

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.

Visibility Not working with Kotlin

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

MonoDroid Toggle View Visibility

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

Categories

Resources