How to enable or disable buttons in bulk? - android

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.

Related

A button clickable when conditions are met (Android Studio)

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.

How to set conditional visibility in android

I want to set visibility of a imageview on the basis of a condition. how would i do it ??
here is my code :
if (web.canGoBack() != true) {
bc.setVisibility(View.INVISIBLE);
}else {
bc.setVisibility(View.VISIBLE);
}
here bc is the imageview over a webview which is in framelayout
What you are trying to do is fine.
But, if want it in 1 line then.
bc.setVisibility(web.canGoBack()? View.VISIBLE : View.INVISIBLE);
Try bc.setVisibility(View.GONE);
But the real issue may be your response from web.canGoBack(). Please do check that condition is working fine.

Android :getVisibility() error

i have alistview when i click the list view it goes to login screen, when login is successful it comes back to the listview with a icon ..so far its working good, problem is when the icon appear again if i click on the listview the loginscreen is comming ..i dnt want the login screen to load again once the icon is visible..i tried the following but it is giving errors
if (img.getVisibility() == 8) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
Any help is appreciated.
use
if (img.getVisibility() != View.Visible)
Dont use hardcode values.
Change your condition as:
if (img.getVisibility() == View.Visible)
EDIT : or better way you can use View.isShown() for checking View or it's child's are Visible or not
You should not really on your UI state for app logic. You would better use startActivityForResult, then in onActivityResult set a flag and use this flag in your click listener, use it also to make the icon visible or not.
You should also consider setting the flag in the shared preferences for persistence if you leave your activity.
what kind of error you face ?? can you show it to use ? you should not use a hard code value to compare the visibility use one of these View.GONE or View.INVISIBLE or VIEW.VISIBLE
like this
if(img.getVisibility != View.VISIBLE){
// do something
}
Use
if (img.getVisibility() != View.Visible)
instead of
if (img.getVisibility() == 8)
check http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29

Can I make a button appear disabled and still listen for clicks?

I'd like to gray out a button so it appears disabled to the user, but still listen for clicks so that I can display a message to the user that explains why the button isn't applicable.
I'd like to ensure that the Android API is the one configuring whatever is the appropriate standard disabled appearance as opposed to manually setting the button color to gray, etc. What's the best way to do this?
Related: Android - Listen to a disabled button
this is custom button which expose the event of touch when disabled
it working for sure, and tested. designed specific to you
public class MyObservableButton extends Button
{
public MyObservableButton(Context context, AttributeSet attrs)
{
super(context, attrs);
}
private IOnClickWhenEnabledListner mListner;
public void setOnClickWhenEnabledListener(IOnClickWhenEnabledListner listener) {
mListner = listener;
}
private interface IOnClickWhenEnabledListner {
public void onClickWhenEnabled();
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!isEnabled() && mListner != null) {
mListner.onClickWhenEnabled();
}
}
return super.onTouchEvent(event);
}
}
this is the right way to accomplish what you want from my point of view as android developer.
there is no problem extanding all android's views, and use them on the xml files, and source instead..
good luck
You could also manually set the background of your Button to the default one for disabled. But leave the button enabled and handle the click events in the normal fashion
something like this should do it:
mBtn.setBackgroundResource(android.R.drawable.btn_default_normal_disabled);
I am not an Android expert, so there could be better ways, but what about disabling the button and overlaying a transparent view that will catch the events?
You could have the view always there laying below the button so you just need to change a z-index, or create it dynamically when needed.
If your button is on the newer types that controls color via backgroundTint rather then background, this is what you should do:
if (enabled) {
ViewCompat.setBackgroundTintList(btn, getResources().getColorStateList(R.color.button_states)); // either use a single color, or a state_list color resource
} else {
ViewCompat.setBackgroundTintList(btn, ColorStateList.valueOf(Color.GRAY));
}
// make sure we're always clickable
btn.setEnabled(true);
btn.setClickable(true);
You could also set the Button's style to look grayed out (for both pressed and non-pressed states), then setOnClickListener() as normal, but have the onClick() method give the message that it isn't clickable.
Since button extends textview. You may use the methods in textview such as .setTextColor() or .setBackgroundColor() .
Now for the the display you have 2 options:
AlertDialog - displays an alert but doesn't self close unless specified.
Toast - displays a text over a given time and self closes.
take your pick.

What makes a button switch and keep color on click?

I want to create a Button that behaves like a switch.
It should change its color when the user clicks it, and keep the color.
So the button is white at first and when the user clicks it, the color changes to black. When the user clicks it again it switches back to white and so on.
I tried it with a simple if else construct but only managed to get the button to be white at first and when being clicked change to black, but it won´t change back to white when clicked again.
Here´s the code so far. I guess it´s a dumb simply mistake but can´t seem to get through with it. "changecolor" is a variable I declared myself.
// Select Button Safe or At-Risk
final Button button7 = (Button) findViewById(R.id.SafeBT);
button7.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks, change color
if (changecolor == 0) {
button7.setBackgroundColor(color.black);
changecolor = 1;
} else {
button7.setBackgroundColor(color.white);
changecolor = 0;
}
}
});
Tanks for advice and help in advance.
where you have declared your variable changecolor?? .
Second thingis that you can simply an UI Element which call it : ToggleButton , it is like a Switch Button ON/OFF . is that what you want ? see this link : http://developer.android.com/reference/android/widget/ToggleButton.html

Categories

Resources