MonoDroid Toggle View Visibility - android

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

Related

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.

How to set "ImageView" to be clickable or non-clickable

I am making a simple memory game. It basically, some ImageView come up and after that user should click ImageView according to order. However, ImageView can also be clickable while sorting.
So, I want the ImageViewto be non-clickable while sorting is not finish and then I want ImageViewcan be clickable when the sorting is finished. How can I do that?
-edit 1
After your solutions, i tried image.setEnable = false method in kotlin and i have the result.
here related section
kotlin
fun orderImages() {
index=0
Collections.shuffle(controlArray)
println(controlArray)
runnable = object : Runnable {
override fun run() {
for (image in imageArray) {
image.visibility = View.INVISIBLE
image.isEnabled = false
}
if (index < controlArray.size) {
imageArray[controlArray[index]].visibility = View.VISIBLE
index++
} else {
handler.removeCallbacks(runnable)
for (image in imageArray) {
image.visibility = View.VISIBLE
image.isEnabled = true
}
}
handler.postDelayed(runnable, 1000)
}
}
handler.post(runnable)
userArray.clear()
}
To programmatically disable clicking listener, use image.setEnabled(false);. However, as you described I think your sorting function is intensive computation so it take along time and it can block UI thread if the time is a few seconds. Therefor you must run the sorting function on a new thread. Assuming you have a button buttonStartSorting in your activity.
final Handler handler = new Handler();
buttonStartSorting.setOnClickListener(new View.OnClickListener() {
//disalbe listener for images
for(imv in imageViews){
imv.isEnable = false;
}
//do sort images on new thread
Thread(Runnable {
yourSortImagesMethod(imageViews);
handler.post(Runable{
//enable listener for images
for(imv in imageViews){
imv.isEnable = true;
}
})
}).start()
})
There is actually a setClickable(boolean clickable) method as you can see here: https://developer.android.com/reference/android/view/View.html#setClickable(boolean)
You can simply add setOnClickListener() on imageview and take a flag to make it clickabe or non clickable based on flag state.
private boolean isSorted = false;
somewhere in your code where you are adding view for image list, use click listener-
holder.borrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isSorted) {
//do what you want to do after it is sorted.
}
}
});
So in this way onClick will do anything only if the images are sorted. Now somewhere in your code there should be a condition in your class so that the state of the flag will be changed. So it would be something like--
...
//Updating flag based on condition
if(condition) {
isSorted = true;
}
...
This block of code allows you to enable or disable all views in the layout layout.
fun enableDisableViewGroup(viewGroup: ViewGroup, enabled: Boolean) {
val childCount = viewGroup.childCount
for (i in 0 until childCount) {
val view = viewGroup.getChildAt(i)
view.isEnabled = enabled
if (view is ViewGroup) {
enableDisableViewGroup(view, enabled)
}
}
}

Programmatically hide views if it is shown. Show views if it is hidden

My button use code that shows and hides the views:
public void onClick (View v){
if (What code you need to enter here to determine hidden views or shown)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
}
What code I need to add in the "if()", so that clicking on my button was checked condition. If the activity is hidden, it should be shown, and Vice versa. If the views is shown, hide it.
I'm guessing since you are using setVisibility, that you want to check the visibility of a View , not an Activity.
In that case you just use getVisibility()
(I used != cause the visibility could be IINVISIBLE as well, change per your needs) :
public void onClick (View v){
if (testActivity.getVisibility() != View.VISIBLE)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
} });
Don't understand why, but only that removed the answer of a man who has solved my problem. Here is his response, and this code works:
public void onClick (View v){
if ((testActivity.getVisibility() == View.VISIBLE))
{
testActivity.setVisibility(View.GONE);
}
else
{
testActivity.setVisibility(View.VISIBLE);
}

How to make a view visible when a button is clicked and make that view invisible when the button is again clicked?

I have a layout which is invisible when the activity starts.When I click on a button the layout becomes visible.My requirement is when I click the button for the second time, the layout should be invisible.I know this is a silly question but as I am a new to android, I am unable to figure it out.
Try the following code to toggle the visibility of the view:
v.setVisibility(v.getVisibility() == View.INVISIBLE ? View.VISIBLE
: View.INVISIBLE);
You can also implement by using boolean FLAG.
e.g. Declare
boolean visibility_Flag = false;
button..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(visibility_Flag){
YourView.setVisibility(View.INVISIBLE);
visibility_Flag = false;
} else {
YourView.setVisibility(View.VISIBLE);
visibility_Flag =true;
}
}
});

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