Reputation: 1529
I'm try to listen a ToogleButton and written toogleButton.setOnClickListener inside onCreate method as below but the method is not getting invoked when i click the tooglebutton
Please let me know what is going wrong..
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the EditText and Toggle buttons
final ToggleButton toggleBttn = (ToggleButton)findViewById(R.id.tggle);
final EditText editText = (EditText)findViewById(R.id.editText1);
// listen to toggleButton click
toggleBttn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(toggleBttn.isChecked())
{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
System.out.println(" Its On");
}
else
{
editText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
System.out.println(" Its Off ");
}
}
});
}
Also below is Activity XML
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:hint="Enter Password" >
<requestFocus />
</EditText>
<ToggleButton
android:id="@+id/tggle"
android:layout_below="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
UPDATED CODE
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the EditText and Toggle buttons
final ToggleButton toggleBttn = (ToggleButton)findViewById(R.id.tggle);
final EditText editText = (EditText)findViewById(R.id.editText1);
// listen to toggleButton click
toggleBttn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
System.out.println(" Its On");
}
else
{
editText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
System.out.println(" Its Off ");
}
}
});
Upvotes: 0
Views: 42
Reputation: 18112
You are using wrong listener here, use OnCheckedChangeListener
Like this example...
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
You can fine more info at http://developer.android.com/guide/topics/ui/controls/togglebutton.html
I am doing this and its working.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e = (EditText) findViewById(R.id.type);
ToggleButton t = (ToggleButton) findViewById(R.id.tggle);
t.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "is checked", Toast.LENGTH_LONG).show();
e.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
e.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
Toast.makeText(MainActivity.this, "is un-checked", Toast.LENGTH_LONG).show();
e.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
}
}
});
Upvotes: 2