Android Switch Status - android

Good morning, I just started studying Android application development, using Android Studio. My first experiment consisted in creating 3 EditText, 1 AnalogClock and 1 Switch, with "Slide Me!" as text.
With this code:
public void Slider(View v) {
int pri,sec,som;
EditText txt_pri = (EditText)findViewById(R.id.Primo);
EditText txt_sec = (EditText)findViewById(R.id.Secondo);
EditText txt_som = (EditText)findViewById(R.id.Somma);
AnalogClock Orologio = (AnalogClock)findViewById(R.id.analogClock);
pri=Integer.parseInt(txt_pri.getText().toString());
sec=Integer.parseInt(txt_sec.getText().toString());
som = pri + sec;
txt_som.setText(""+som);
Orologio.setVisibility(View.INVISIBLE);
}
which is the only thing I added, other than deleting the "Hello World!" default textbox, I am able to read two numbers and put their sum in the third EditText, while making the analog clock disappear.
What I would like to do next would be to check the switch status and do
the sum, the disappearing clock and changing "Slide Me!" to something else, if switching on;
erasing the sum EditText, the reappearing of the clock and put "Slide Me!" back, if switching off;
but I have no idea where to start from.
Thank you in advance for any help!
Ciao, Lupo

By Switch do you mean toggle? To handle toggle use the following code..
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
//set text here
} else {
// set anything else here
}
}
Or do this
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
}
}
});

Related

Change the color of a switch button based on the response of a HTTP request

I am implementing a switch button that can remotely turn on or off a light.
Whenever the switch button is pressed, a HTTP request will be sent to the server, and a response will be sent back.
The requirement is:
While the app is waiting for the response, the switch button turns yellow.
If the response is 200 OK, the button turns green.
If the response is denied or times out, the button turns red.
I am using the default switch button. It does not allow me to change the color dynamically and I have been looking around and could not find anything that can be used for my app.
How am i able to achieve this multi-state switch button?
You can can change the colour like this. I just tested and it works:
// Assuming ToggleButton with id "toggleButton"
final 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
toggle.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
} else {
// The toggle is disabled
toggle.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
}
}
});
Just as an example. Is that what you mean? Of course you can make up your own colours in the "colors.xml" file.

onCheckedChanged does not seem to work

I have a checkbox. It is set to true or false depending on if a task is done or not (its a manual change). When the task is done I want the textview label to change frmo not done to done and vice versa. So I have the following code. When they click the checkbox the onCheckedChanged method does get fired off. It chooses sets the string depending on if it is true or false correctly. But then it just exits. I get no error in the logs or on the screen but when I step through the program after it sets the string in the onCheckedChanged method it just exits the getView method completely. I cant understand what is going wrong. Theres a small problem in the first couple lines that the logic for setting if the box is true or false is not entirely correct but thats fine I can fix that no problem. I just cant understand why I cant update the label after clickign the checkbox. Any help would be great.
final CheckBox statusView = (CheckBox)convertView.findViewById(R.id.statusCheckBox);
//statusView.setChecked(true);
if(toDoItem.getStatus().toString().compareTo(ToDoItem.Status.DONE.toString()) == 0)
statusView.setChecked(true);
else
statusView.setChecked(false);
// TODO - Must also set up an OnCheckedChangeListener,
// which is called when the user toggles the status checkbox
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
}
});
TextView statusLabel = (TextView)convertView.findViewById(R.id.StatusLabel);
statusLabel.setText(statusLabelValue);
You will have to change the textview's text in the listener:
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
((TextView)(convertView.findViewById(R.id.StatusLabel))).setText(statusLabelValue);
}
});

Check state of togglebutton android

Im new to android and I don't get the code on the developers website about this really, I just want to check if the state of the toggle button is on or off. I have the following scenario
if (isCheckedToggleButton?)
{
// do something
}
else
{
// do something else
}
And a method as the guide suggests
public void onToggleClicked(View view)
{
boolean on = ((ToggleButton)view).isChecked();
if(on) {
return;
} else {
}
}
So I just want to see if the toggle button is on or off so I can decide whether to execute the code inside the if or the else. Unfortunately the method provided by the android guide is a void so it doesn't return a boolean. How can I still check the state?
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lift"
android:textOff="Uit"
android:textOn="Aan"
android:id="#+id/switchLift" android:layout_below="#id/btnEindpunt"
android:layout_alignLeft="#+id/btnEindpunt"/>
Use it like this
myToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
// Do something
else
// Do the other thing
}
});
Change
ToggleButton myToggleButton = ((ToggleButton) findViewById(R.id.switchLift));
to
Switch myToggleButton = ((Switch) findViewById(R.id.switchLift));
Also change isChecked to something else like mIsChecked or inside the listener use YourClassName.this.isChecked for changing its value. There is already a local variable with same name.

Possible to enable EditText instantly when its disabled

Can an editText field be enabled instantly when I call editText.setEnabled(true); ?
Currently I have a code which says:
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
row1TextField = (EditText) findViewById(R.id.row1EditText);
row2TextField = (EditText) findViewById(R.id.row2EditText);
if(toggleButton.isChecked())
{
if(row1TextField.isFocused()){
row2TextField.setEnabled(false);
}
}
if(!toggleButton.isChecked())
{
if(row1TextField.isFocused()){
row2TextField.setEnabled(true);
}
}
With this code, the setEnabled on EditText doesn't reflect instantly though. For example is, when I switch between the states ON and OFF of my toggleButton, the Row2TextField isn't enabled/disabled automatically eventhough I'm on row1TextField on. I have to click the other EditText and go back to row1TextField in order to disable/enable it.
Is there a way I can let it reflect its state instantly instead of changing to different Focus in order to enable it?
you should use onCheckedChangedListender for ToggleButton
and should like this
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
row1TextField.setEnabled(true);
row2TextField.setEnabled(false);
}
else
{
row1TextField.setEnabled(false);
row2TextField.setEnabled(true);
}
}
});
In this way, you can be enable/disable instantly.

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

Categories

Resources