Custom switch button? - android

I am using a switch button in an android studio activity.What I want to do is that when I slide from left to right,in the right side to appear a timer,and in the thumb part of switch button to have an image.Too a need it to not appear the track part before starting sliding the thumb!How it is possible to make that?

as Anjal Saneen said in the comments, you can set the visibility programmatically. You can call your switch and then set visibility for your components like so:
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myTimer.setVisibility(View.VISIBLE);
myImage.setVisibility(View.VISIBLE);
}
});

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.

how to set visiblity in android for seekbar and textview

**There is a switch button , seek bar and text view in the above image
so when this activity starts . i want seek bar and the text view invisible by default
and when i switch on the switch button , both seek bar and text view should be visible
please provide a simple code for this operation .
i have tried using all gone ,visible and invisible stuffs . but it is not showing any effect.
**
have you tried like below?
#Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
// find all view id here
textview.setVisiblity(View.GONE);
seekBar.setVisiblity(View.GONE);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
if(isChecked){
textview.setVisiblity(View.VISIBLE);
seekBar.setVisiblity(View.VISIBLE);
}else{
textview.setVisiblity(View.GONE);
seekBar.setVisiblity(View.GONE);
}
}
});
}

How to select a single checkbox while disabling clickable option in other checkbox if i have two checkboxes in android?

I have two checkboxes with yes or no options.I have to select only one option while disabling the clickable option of other checkbox. opp11 and op12 are two checkboxes.The following code seems to work but what if i have around 20 checkbox options.Is there any method to shorten the code like i can pass two options one to set onchanged listner and other to disable the clickable option.
op11.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
op12.setChecked(false);
// Code to display your message.
}
}
});

How to create AlertDialog with single choice checkbox in android?

How to implement single choice with checkboxes in AlertDialog? I try to implement with setSingleChoiceItems, but it displays radio button. setMultiChoiceItems displays checkboxes but there user can check multiple items. I need to implement singleChoice with Checkboxes. I need yr help?
Programmatically, with only two checkboxes, you can write a switch case or if-else loop with the onCheckChangedListener like;
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
{
case R.id.cbOne:
cbOne.setChecked(true);
cbTwo.setChecked(false);
break;
case R.id.cbTwo:
cbTwo.setChecked(true);
cbOne.setChecked(false);
break;
}
}
And for both checkbox items you can set the listener as:
yourCb = (CheckBox)findViewById(R.id.yourCb);
yourCb.setOnCheckedChangeListener(yourListener);
If you customise the background, the default behaviour would also suit your requirement.. custom-android-checkbox-radiobutton

create On/off button with slide

I want to create on/off button with slide .
I am using this Technic:
I put the buttons in the same position and one is visible and the other is hidden.
and when I click on one the other button is appeared and the clicked is disappeared.
Now How can I make that button slide-able.
Here is the buttons:
How can i do this ?
try using Switch but, if you are ok with minSdkVersion>=14.
hope following code helps you out.
In Layout Xml create a switch like:
<Switch
android:id="#+id/on_off_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"/>
Then in your activity get a switch from layout and set listener as follows,
Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch);
onOffSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"isChecked"+isChecked, Toast.LENGTH_LONG).show();
}
});

Categories

Resources