Friday, 28 August 2015

Android Programming Tutorials 29 : ToggleButton In Android

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: ToggleButtonApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.EditText;
    import android.widget.ToggleButton;

    public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                            final EditText et = (EditText) findViewById(R.id.etToggle);
                            final ToggleButton tb = (ToggleButton) findViewById(R.id.tbCheck);
                           
                            tb.setOnClickListener(new OnClickListener() {
                            @Override
                           public void onClick(View v) {
                           // TODO Auto-generated method stub
                           if(tb.isChecked()){
    et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            }else{
                            et.setInputType(InputType.TYPE_CLASS_TEXT);
                            }
                            }
                            });
                  }
       }

     2.Simple Copy and Past the below code:  actvitiy_main.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ToggleButton
            android:id="@+id/tbCheck"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="22dp"
            android:layout_marginTop="74dp"
            android:checked="true"
            android:text="ToggleButton" />

        <EditText
            android:id="@+id/etToggle"
            android:layout_width="199dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/tbCheck"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </RelativeLayout>



    3.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
            <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch Enter the data into the Edit Text and see click on the Toggle button to ON then you can not see the data you entered instead of that it appear dots, If Toggle Button is OFF the data is visible. 

    Stay Tune For Next Tutorial... Media Player Functionality In Android:



    Please Send Your Comments To ambilpura.sunil@gmail.com

    No comments:

    Post a Comment