Pr.Pg Next Pg

EditText and ToggleButton tutorials

EditText

  • EditText is a thin veneer over TextView that configures itself to be editable.

  • EditText is generally used to take in inputs.

 

Toggle Button

  • Displays checked/unchecked states as a button with a "light" indicator and by default accompanied with the text "ON" or "OFF".

  • It is generally used as a switch to show whether it is ON or OFF.

 

Application Example

  • In this tutorial, we are going to make an Application what will take an input(password) from the user and then when the user clicks on the button Login,he will be displayed success or else which will displayed Failure in the TextBox.

  • The Toggle button will be used to provide the user with the option whether he wants to hide the password or make it visible.

  • We are going to introduce in this tutorial various attributes of the Toggle Button.

  • We create a new Layout for this activity named authentication.

  • A new activity which will be started when the login button is pressed.

  • We build a new class for the activity named Password.

  • We also have to build a new layout for the activity and we call it text.

  • The names can be anything.

  • We present you with the codes of various files and then explain them.

  • Below is the code for text.xml.

 

test.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<EditText

android:id="@+id/editText1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Enter a Password">

</EditText>

 

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:weightSum="100">

 

<Button

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="20"

android:text="Button" />

 

<ToggleButton

android:id="@+id/toggleButton1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="80"

android:checked="true"

android:textOff="Hide"

android:textOn="Show" />

</LinearLayout>

 

<TextView

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="" />

 

</LinearLayout>

 

  • In the above text.xml file, we have created Linear Layout with a Editable TextBox, a Button, a Toggle Button and a TextView.

  • The button and Toggle Button are placed in nest linear layout in horizontal orientation

  • We have introduced here Weightsum.

  • It defines the maximum weight sum.

  • If unspecified, the sum is computed by adding the layout_weight of all of the children.

 

Pr.Pg border                                              Next Pg