Translate To Your Own Langauge
toggle button in android sample code
This control is provided by Android so that developers can use one button to perform two operations depending upon the requirement. In today's tutorial, I will show you how to implement "Toggle Button in Android" with the help of relevant Example. Follow these steps.
Step 1: Drag and drop the Toggle button control from Pallete into your xml file or just copy and paste the following code into your .xml file.
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="21dp" android:text="ToggleButton" /> </RelativeLayout> |
Step 2: Now make the following changes into your .java file.
package com.deepak.togglebutton; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { ToggleButton toggle1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggle1=(ToggleButton)findViewById(R.id.toggleButton1); toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean b) { // TODO Auto-generated method stub if (b) { Toast.makeText(getApplicationContext(), "Button is ON", 0).show(); } else { Toast.makeText(getApplicationContext(), "Button is OFF", 0).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |
Subscribe to:
Post Comments (Atom)
You may be interested in:
- Registration Form in Android with Validation
- Login with Validation (Android Example)
- Implementing Tab Example in android using TabActivity
- Splash screen in Android using Thread
- Showing Map
- button in android example
- Spinner Android Example
- Change the Profile Mode Programmatically (Android Example)
- Login Example (Android)
- Generating Google APIs
0 Responses to “toggle button in android sample code”
Post a Comment
Comment your feedback for the post. You can also ask your queries regarding android.