Pr.Pg Next Pg

EditText and ToggleButton tutorials

  • This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

  • In our example, we have given the weightsum as 100.

  • We can now consider it as total percentage of space available and then assign it to the other Views in the layout specifying how space they will occupy.

  • Note that higher the layout_weight lesser will be the space occupied by the component.

  • We have assigned Button more space than the ToggleButton.

  • Within the EditText we have used an attribute named android:hint which will nothing but display a Text as a Hint for the user when the text is empty.

  • For the ToggleButton, we have made the initial state as checked(i.e the button is ON).

  • We have assigned the labels for the ToggleButton when it is clicked or not clicked(Default is ON/OFF).

  • We know show the Password.java file which was mentioned before.

 

Password.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

import android.text.InputType;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.ToggleButton;

 

public class Password extends Activity {

Button b;

ToggleButton tb;

EditText et;

TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.text);

b=(Button) findViewById(R.id.button1);

tb=(ToggleButton)findViewById(R.id.toggleButton1);

tv=(TextView)findViewById(R.id.textView1);

et=(EditText)findViewById(R.id.editText1);

et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

tb.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// 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);

}

}

});

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

String pass=et.getText().toString();

if(pass.equals("password")){

tv.setText("Success");

}else{

tv.setText("Invalid");

}

}

});

}

 

}

 

 

  • In this class we have made an activity named Password and we have declared our XML components in Java.

 

Pr.Pg border                                              Next Pg