Reputation: 159
I am attempting to build my first Android application, and I'm stuck already. I have two toggle switches, and when they're turned on, a dialog window appears. I would like the "Cancel" button to turn the switch back off. I've tried toggleButton.setChecked(false), Switch.setChecked(false), etc., but because these switches were created in an XML file, there's no object to execute that method on/with. How do I go about toggling these switches within my program? I have my onClick listener in my main activity, and the dialog creation as a different class. This may be wrong, but it works up to this point.
MainActivity.java:
package com.example.arduinoautomation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Switch;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void lampToggle(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
lampOnDialog lamp_on = new lampOnDialog();
lamp_on.message = "Lamp is on.";
lamp_on.show(this.getFragmentManager(),"switch");
} else {
// Disable vibrate
}
}
public void lightToggle(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
lampOnDialog light_on = new lampOnDialog();
light_on.message = "Light is on";
light_on.show(this.getFragmentManager(), "switch");
} else {
// Disable vibrate
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
lampOnDialog.java:
package com.example.arduinoautomation;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
public class lampOnDialog extends DialogFragment {
String message = "";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="57dp"
android:onClick="lampToggle"
android:text="Lamp" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/switch1"
android:layout_marginTop="49dp"
android:onClick="lightToggle"
android:text="Lights" />
</RelativeLayout>
Upvotes: 1
Views: 3938
Reputation: 1961
You have to define your ToggleButton as a View in your activity:
ToggleButton toggle;
And then instantiate it, typically in your onCreate method:
toggle = (ToggleButton) findViewById(R.id.switch1);
Then, you can use the setChecked method on your view from wherever:
toggle.setChecked(false);
EDIT
You can't access the View, because your dialog is another class, and the toggle view is in your Activity class. Try creating the dialog inside the Activity class:
public void showDialog(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setPositiveButton("Yes, you will", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toggle.setChecked(true);
}
}).setNegativeButton("No, you won't", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toggle.setChecked(false);
}
}).show();
}
and then show a dialog from anywhere in your activity by calling your showDialog()
method:
showDialog("Hi, I'll be your dialog today");
Upvotes: 1