Reputation: 8522
I'm new to android. I tried to create a simple toggle button and catch it's state change. But it crashes when I run it on my device.
Here is the MainActivity.java code:
package com.example.addimagebutton;
import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton tbSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbSwitch = (ToggleButton) findViewById(R.id.swButton);
tbSwitch.setOnCheckedChangeListener(toggleListener);
}
OnCheckedChangeListener toggleListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(getApplicationContext(), "On",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Off",
Toast.LENGTH_SHORT).show();
}
}
};
}
Here is activity_main.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Switch
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
</LinearLayout>
I tried to run this code without the listener objects.(commenting those) But it gave me the same result.
Upvotes: 0
Views: 1191
Reputation: 14590
You are casting a switch to Toggle Button so it gives you ClassCastException
..
Change this
<Switch
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
into
<ToggleButton
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
Upvotes: 2
Reputation: 404
in your xml you should use ToggleButton instead of Switch - it should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ToogleButton
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
Upvotes: 0
Reputation: 12042
Change switch
to ToggleButton
<ToggleButton
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
Upvotes: 0
Reputation: 47817
Used ToggleButton
instead of Switch
like
<ToggleButton
android:id="@+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On"" />
Becoz your layout contains Switch
and you cast it with ToggleButton
. That's wrong
Upvotes: 1