Make a toggle button to widget - android

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

Related

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.

Button invisble but enabled

i designed my whole activity page in photoshop and opened it in eclipse.I want to put buttons on my fake image buttons. Doesnt it make any problem? I use the code below to make the button invisible but it disables button too. How can i fix this problem?
android:visibility="invisible"
You should not do like that (having a fake ImageButton on a View you want to make clickable), as many error can occur with doing it. Assuming you are trying to put this button on a whole ImageView, or a whole Layout, or at least a whole view, you should consider adding a click listener to this view. Try to find more information about how to use the View.setOnClickListener(View.OnClickListener) method. You will have less error to debug with it.
But if you still want to do, try instead using android:alpha property:
android:alpha="0"
It will make the button become invisible, but still present, and still active
This android:alpha attribute allows you to specify an opacity value for a color.
To be honest you shouldn't be doing that unless you are testing something or prototyping. Instead, you should cut your design in photoshop and export the images for your buttons.
Your approch will most likely cause the buttons to be positioned incorrectly above the image when run on different devices with different screen sizes.
The correct way to do this would be to use the ImageButton view with a StateListDrawable set as the source image.
You can read more about State Lists here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
It's very easy to declare it in XML and have different images for various button states (pressed, disabled, focused, ...).
To sum it all up. I would do the following things:
Export images for my buttons in all the different states that you prepared (i find neutral and pressed as an absolute minimum).
Import them as drawables in your project (be sure to generate different versions for every screen density you want to support).
Create a State List Drawable XML Resource as described here.
Set the resource created in step 3 as the android:src property of your ImageButton (setImageResource(int)).
Try android:background="#null" for your button. It will make that default gray background disappear, making your button invisible but still allowing you to add text etc if you want to.
That being said: I would not recomment a button here. I would prefer Pauls answer in your particular case.

Android Custom Buttons: Alternative to Multiple Graphics?

I think all custom button tutorials I have been able to find for Android assume you are using three images for your button: a normal image, a pressed image, and a focused image.
Instead of essentially tripling the size of a given button's resources (and creating more work for the artist/UX guy), is it possible to only supply a normal button image, and for the other states, draw some sort of overlay over the existing button by extending the Button class?
Has anybody tried doing this with any success, or is it just accepted that all custom buttons need an image for each state and that is that?
You could by overriding the draw methods of the view in your custom button but it would be a simple process as you would have to also identify the different states yourself.
I think the correct answer to this question is essentially what #Luksprog said in the comments... It simply isn't worth it. Just make the extra art.

ToggleButton state change programmatically rather than automatically in 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.

How to highlight clicks in app widget?

I have an app widget which runs neatly. However, I am unable to highlight a click on a linked item. I've seen it in the standard app widgets like 'Music' and 'Power Control', for instance. Moreover, I've also been studying the Music app widget's source at album_appwidget.xml. The only thing I could think of is the LinearLayout defined at lines 23-35 which states
android:clickable="true"
Unfortunately, this does not work for me. So does anyone have a hint on how to highlight a click on an app widget? I've tried the LinearLayout, TextView and Button. None of them displayed a border as a highlight.
Thanks in advance,
Steff
you need to create images for those states like focussed state, pressed etc like in a button and define them in your background.
Try looking at the custom buttons where its explained how to accomplish the task thats similar to your needs.
http://www.gersic.com/blog.php?id=56.
if you want to look more and add more states you may ge better idea if you look at the android source code for buttons where they have images for each state of the button and every other widget.

Categories

Resources