• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ToggleButton handling

v1nsai

Member
**SOLVED**
corrected code is below


I'm having a little trouble handling events from my ToggleButton. I can't figure out where to use the setOnClickListener() call and how to set up handlers for the button being turned on and off. Here's what I have, can anyone suggest a fix?


Code:
package com.v1nsai.dataoff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Settings extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        //ToggleButton wifitoggle = (ToggleButton)findViewById(R.id.wifitoggle);
        //wifitoggle.setOnClickListener(this);
    }
        
    public void onClick( View v )
    {        
        //Debug.startMethodTracing("settings");
        ToggleButton wifitoggle = (ToggleButton)findViewById(R.id.wifitoggle); 
        wifitoggle.setOnClickListener(this);
        switch( v.getId() )
        {
            case R.id.wifitoggle:
                if( wifitoggle.isChecked() == true )
                {
                    startService(new Intent( this, WifiService.class ) );
                    Toast.makeText( this, "Wifi kill service on", 3 ).show();
                }
                if( wifitoggle.isChecked() == false )
                {
                    stopService( new Intent( this, WifiService.class ) );
                    Toast.makeText( this, "Wifi kill service off", 3 ).show();
                }
                break;
                
            default:
                break;
        }
        //Debug.stopMethodTracing();
    }
}
 
Back
Top Bottom