activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="107dp" android:layout_marginTop="37dp" android:text="ToggleButton" /> </RelativeLayout>
MainActivity.java
package com.blogspot.kantapp.togglebuttoninandroid; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggleButton1); toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(buttonView.isChecked()){ Toast.makeText(getApplicationContext(), "toggleButton is on", 3000).show(); }else{ Toast.makeText(getApplicationContext(), "toggleButton is off", 3000).show(); } } }); } }
No comments:
Post a Comment