Static Creation
main.xml
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Static Toggel Button"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
/>
<ToggleButton
android:id="@+id/tb_toglebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Button"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
ToggleButtonActivity.java
package com.togglebutton;
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 ToggleButtonActivity extends Activity {
ToggleButton toggleButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton=(ToggleButton)findViewById(R.id.tb_toglebtn);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==true){
Toast.makeText(getApplicationContext(), "ON State", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "OFF State", Toast.LENGTH_LONG).show();
}
}
});
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.togglebutton"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name=".ToggleButtonActivity"
android:label="@string/title_activity_toggle_button" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment