Search code examples
androidindicator

Android: How to add on/off indicator in xml layout


Excuse me, for my very stupid question, but I'm a newbie in android programming...

I was wondering how to add an indicator in the layout of my application. The idea was to add an On/Off green led (to indicate Connected/NotConnected).

I searched a lot but i haven't found anything... ...I started to think that there are no things like indicators...


Solution

  • use ToggleButton widget

    Go through this link :- http://developer.android.com/guide/topics/ui/controls/togglebutton.html

    This is how you use it :-

    <ToggleButton 
    android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"
    android:onClick="onToggleClicked"/>
    

    and this is how you write listener in activity :-

    ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
            } else {
                // The toggle is disabled
            }
        }
    });