ToggleButton state change programmatically rather than automatically in Android? - android

I have created a custom class which extends ToggleButton and I override the toggle method and do not do anything in that. This has helped me in having control of switching togglebutton from on to off. Is this proper way of doing?
I wanted to have the control of togglebutton switching. I mean it should go from on to off based on certain conditions otherwise it should remain in the state it was.
This way its working but want to know whether its the correct way of doing or not.

What you are looking for is ToggleButton.setChecked.

Related

How to make a radio button fully toggleable?

I've been changing over some app menu options lately in order to simplify the graphics and give me more space to work with. I added a radio button, and have it set up to toggle on and off when pressed via a listener:
View.OnClickListener toggleListener = view -> {
Settings.getSetting().toggleSetting();
holder.settingToggleButton.setChecked(!holder.settingToggleButton.isChecked());
};
...
holder.settingToggleButton.setOnClickListener(toggleListener);
But when I load into my app and toggle the button, it'll work initially (options that are on get toggled off, and vice versa) but when I then try to tap it again and toggle the option back on/off, it doesn't work. It sticks either on or off, whichever state it was initially toggled into when I first pressed it. I'm not sure why this is happening, as the logic is very straight-forward... any ideas as to why this set up isn't working?
holder.settingToggleButton.setChecked(!holder.settingToggleButton.isChecked());
The logic here is correct, as #Ansh suggested, I would say the problem is the UI never receives the setChecked attribute and what you're seeing is the default behavior of a radiobutton, which takes the initial input but isn't toggleable without logic code.
If you think that's not the case, try putting down a Button that updates the toggle state of a specific radiobutton. This'll help you see what you're doing wrong.
I have discovered the answer to my question; it's a niche solution related directly to my problem, but overall, it was indeed related to not checking for/modifying the ID of the toggle button. If you're ever having issues with the button toggling one way but not the other, or not toggling at all, make sure all references to that button, and all the changes it's involved in, take it into account.

how to mimic android checkbox using button and state drawables

I want to use checkbox in RemoteViews but it does not support Checkbox object, so I think that I can achieve a similar effect using Button with custom state-list drawables. Basically I want an on/off switch which toggles on click and looks like a Checkbox.
I believe I am not the only one having this requirement so maybe someone has already made this checkbox-like button before. Please share with me the xml or point me in a direction. I don't have 100% understanding of drawables but I can find a way through it if I am heading in the right direction.
One shortcut I could take is to use an ImageButton and then alternate the images of checked/unchecked states programmatically, but I think inherent state changes would be faster.

When to apply design-changes from PreferenceActivity in MainActivity? #onResume?

i`m new to android-coding and am working on my first app. I have the following question:
If one wants the user to be able (for instance) to change the backgroundcolor of the app in PreferenceActivity, where should this change be applied in the MainActivity of the app?
In my case i have many views on a few pages which i want to be customizable in one or the other way. I do it like this:
If there are relevant changes in PreferenceActivity i set a flag and save the changes and the flag in SharedPreferences
Since onResume of MainActivity is called whenever the user returns, i read from the flag in SharedPreferences if changes of the views need to be made and if so, i apply the changes.
I want to use a flag because i dont want to apply the changes again and again whenever onResume is called because its many views that are affected and i dont want to slow down the app unnecessarily.
How would you all do this? i`ll be happy about any hint. Maybe i should even apply the changes in PReferenceActivity already? i dont know......
thx

Can anyone please help me in understanding drawableStateChanged() method of toggle button in android?

Can anyone please help me in understanding drawableStateChanged method of toggle button in android??? I wanted to know why exactly it is used for and how to implement it??
I found it on following link:
http://developer.android.com/reference/android/widget/ToggleButton.html.
from reading through the documentation which states:
This function is called whenever the state of the view changes in
such a way that it impacts the state of drawables being shown.
it seems that this function will be called by the framework whenever the component needs to be redrawn and you can override it to (for example) perform so application specific logic which you need to do when the component is redrawn, like manually drawing something over the top of the component, or changing the font or doing something which is not possible using the stock attributes.
This question has an example of how you might implement it.

Make a toggle button to widget

How do I add a button to a widget, not for opening an activity or something?
Just for toggling for example Wi-Fi :) Could it be done by a ImageButton or something like that? I just can't figure out how to add an onClickListener.
I have updated my answer now that I understand you are trying to write an AppWidget.
AppWidgets support only a limited set of Views and ToggleButton is not one of them. The list of Views supported by AppWidgets can be found here:
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
You will have to use an ImageButton and remember its toggled state yourself. You can set it's image source to a different image depending on whether it is toggled or not. See ImageButton for more info:
http://developer.android.com/reference/android/widget/ImageButton.html

Categories

Resources