Pr.Pg Next Pg

EditText and ToggleButton tutorials

  • We have defined the actions which the button and the ToggleButton will perform in the OnClick() methods.

  • The ToggleButton, if checked(i.e if it is ON) will hide the Text, i.e it will change the text format into Password.

  • If it is not checked then the text will be visible as a plain text.

  • To change the input type to password we have to assign


  • In our example we have kept a predefined string as “password”.

  • If the string entered by the user is equal to this string then clicking this Button will change the content of the TextView as Success, otherwise it will print the message Invalid.

  • For this purpose we have used the method setText().

  • We also add our activity in the AndroidMainifest and change it as the Main activity and LAUNCHER so that it is the first activity started when application launches.

  • Below are the Screenshots of the Application when the Toggle Button is clicked and not clicked, and in the first case the string is “password” and in the second case it is “nopassword”



  • We can also have many variations such as we can start a new activity and switch over to a new window, if the password is correct or another activity if the password is incorrect.

  • We can also use the same layout to align the content in the TextView

  • For example, if the user enters “Left” and then presses the Button then we can align the Text in the TextView to the left using the setGravity() method.

  • For example we can write et.setGravity(Gravity.LEFT) in our example depending whether the user has typed “LEFT” or not.

  • This tutorial introduced us to the ToggleButton and EditText and how to change the type of text to password dynamically.

  • There are methods to set the color of the TextView like setTextColor().

  • The list of methods in our each tutorial can be checked out on the



Pr.Pg border                                              Next Pg