Android Switch button inside RecyclerView's every item - android

I am working with RecyclerView and using Switch component in every item of it.
<Switch
android:id="#+id/list_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:layout_toLeftOf="#+id/list_item_price"
android:textOff="#string/switch_save"
android:textOn="#string/switch_buy"
android:layout_centerVertical="true"
/>
I am using setOnCheckedChangeListener event to listen to state changes
holder.mSaveBuySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
moveItemDialog(pos, isChecked);
}
}
});
Use of buttonView.isPressed() is to identify whether the setChecked on Switch is called by User or it is called programmatically by code.
Now it is working perfect When we click on Switch.
Problem - How can I make it work when we slide the Switch? in Android 4.0+ Switch component present nice ready made toggle button but I need to know when user slides the switch so that I can execute the same code which is executed when it is pressed. That is,
if (buttonView.isPressed()) {
moveItemDialog(pos, isChecked);
}
Thank You.

I know this question is too old. But I've try with android.support.v7.widget.SwitchCompat it's also works with sliding.
This my sample code and some debug log.
<android.support.v7.widget.SwitchCompat
android:id="#+id/saklar_switch"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Debug Code
Log.d("SAKLAR","Pos: "+position+" buttonView.isPressed()"+buttonView.isPressed());
Log.d("SAKLAR","Pos: "+position+" buttonView = isCheccked? "+isChecked);
Debug Result
Pos: 2 buttonView.isPressed()true
Pos: 2 buttonView = isCheccked? true
isPressed() debug also return true while I'm sliding the switch button.

Related

Custom switch button?

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);
}
});

CheckBox replace setOnCheckedChangeListener with setOnLongClickListener

I have a CheckBox that I need to respond to OnLongClickListener instead of OnCheckedChangeListener. How do I do that? Right now even though I implement the OnLongClickListener, the CheckBox still toggles when a user clicks it. I want the toggle to happen on longClick only as in
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
...
return true;
}
});
I would suggest two things...
Not sure if they will work. I did not tested.
Maybe you can disable the click on layout file. Enable only the long click.
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:longClickable="true"
..... />
Or, you can override onCheckedChangeListener to do not take any action.
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBox.setChecked(!isChecked);
}
});

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();
}
});

Android: How to add on/off indicator in xml layout

Excuse me, for my very stupid question, but I'm a newbie in android programming...
I was wondering how to add an indicator in the layout of my application. The idea was to add an On/Off green led (to indicate Connected/NotConnected).
I searched a lot but i haven't found anything... ...I started to think that there are no things like indicators...
use ToggleButton widget
Go through this link :-
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
This is how you use it :-
<ToggleButton
android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"/>
and this is how you write listener in activity :-
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
}
}
});

How to prevent a CheckBox from being checked?

I would like to be able to prevent a CheckBox from being selected (or to set it back to unselected), when the CheckBox is clicked
How can I achieve this?
I do not want to simply disable the checkbox. I want the user to think it is checkable, but when the user tries to check it... then I will (possibly) prevent the checkbox from being checked and display a message.
Just add the android:clickable="false" attribute in the layout xml.
So for me I have:
<CheckBox
android:id="#+id/server_is_online"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:clickable="false"
android:text="#string/server_is_online"
android:textSize="23sp" />
and it works fine.
No that's probably not how you're supposed to use a checkbox, but I am using this as a dirty hack in the prototyping stage in lieu of having a nice icon with a green tick for all good, and an evil red cross for end of the world :)
you can do something like this:
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
cb.setChecked(false);
// Code to display your message.
}
}
});
Just set it to never being clicked
cb.setClickable(false);
Try the following
CheckBox repeatChkBx =
( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
repeatChkBx.setChecked(false); // perform logic of opening message
}
}
});
this code perfect work for me
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked==true )
{
buttonView.setChecked(false);
}
else
{
buttonView.setChecked(true);
}
}
}); //this code through user cant check box check/uncheck
Try this
<CheckBox
**android:background="#android:color/transparent"
**android:clickable="false"
android:id="#+id/login_access_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:background for removing on click ripple effect
android:clickable="false"
 for making it not clickable
In Android CompoundButton class perfomClick() toggles checked state of button.
#Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
You can create a class which extends CheckBox and override performClick() method if you want to manually control behaviour. Because clickable=false did not work for me.
You can try View.TouchListener as a listener to the CheckBox view like so:
inner class TouchListener : View.OnTouchListener {
override fun onTouch(view: View, event: MotionEvent): Boolean {
when (view.id) {
<Your View id> -> {
if (event.action == ACTION_DOWN) {
//do your stuff here
}
return true /*To consume click event so the checkbox doesn't get checked, you can set it checked later once you're done using setChecked(true)*/
}
}
return false
}
}
*This snippet is in Kotlin
Just include android:clickable="false" only, it will work fine.
*Note - Do not include android:focusable attribute
Check out my below working example -
<com.google.android.material.checkbox.MaterialCheckBox
android:id="#+id/chkSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:checked="false"
android:scaleX="1.1"
android:scaleY="1.1"
android:clickable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

Categories

Resources