I need to be able to access the state of a ToggleButton seeing is how there is no way to create methods for the specific state of a ToggleButton. So here is where I'm at so far:
ToggleButton syncSwitch = (ToggleButton)findViewById(R.id.toggleButton1);
syncSwitch.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(getApplicationContext(), "Click!", Toast.LENGTH_SHORT).show();
}
});
Now all I need is some type of boolean method that can tell the handler the state of the ToggleButton.
What do you mean by 'create methods for the specific state'? You can get the state using isChecked().
You can also check by using togglebtn.setOnCheckedChangeListener Listener like this:
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getApplicationContext(),
String.valueOf(buttonView.isChecked()), Toast.LENGTH_SHORT).show();
}
});
Related
I want that when I click first time on this check box it should change the image from my Image View and when I click again on same checkbox it should change the image in Image View with another Image.
I Tried:-
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setBackgroundResource(R.drawable.saveimage);
}else{
myImage.setBackgroundResource(R.drawable.deleteimage);
}
}
});
It's done:-
Take globally (boolean switchStatus = false);
if(switchStatus == true){
dialog.dismiss();
image1.setImageResource(R.drawable.icon_delete);
switchStatus = false;
}else{
dialog.dismiss();
image1.setImageResource(R.drawable.icon_checkmark);
switchStatus = true;
}
There is nothing wrong with your code apart from the lack of using setchecked method. When you check your chechbox , call checkbox.setchecked(true) and when you unckeck it, call checkbox.setchecked(false);
Because your image is in Drawable I think you should try this:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setImageDrawable(getResources().getDrawable(R.drawable.saveimage,getApplicationContext().getTheme()));
}else{
myImage.setImageDrawable(getResources().getDrawable((R.drawable.deleteimage,getApplicationContext().getTheme()));
}
}
});
In my recyclerview onBindView method, I have this piece of code:
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// execute the code inside the onCheckedChanged method
}
});
And then I also have this:
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something
}
});
I'm doing this because I need padding around the checkbox (linear layout), but I need the checkbox to stay the same during scrolling by using OnCheckedChangedListener. I need to use the boolean isChecked parameter to execute my further code.
How can I execute the code inside the onCheckedChanged by clicking on the linear layout and not on the checkbox?
If you need to change the checkbox field on clicking linear layout add this code
holder.checkBox.setChecked(!holder.checkBox.isChecked());
else add this
holder.checkBox.setChecked(holder.checkBox.isChecked());
This will call the onCheckedChanged method.
try this :
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.checkBox.isChecked()){
holder.checkBox.setChecked(false);
//do your code for check status false
}
else{
holder.checkBox.setChecked(true);
//do your code for check status true
}
}
});
I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) radioButton.setChecked(false);
else radioButton.setChecked(true);
}
});
I have made a solution for it using set selected attribute of the radio button.
final RadioButton radioButton = findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?
You are generating a endless loop. That will cause stackoverflow error.
Because you are changing radio button checked status inside onCheckedChanged.
If you need changing check status then do it in click of a button.
Don't change check status inside onCheckedChanged
Perhaps you need to change status of RadioButton for some condition. For that you will do following.
public class MainActivity extends AppCompatActivity {
RadioButton radioButton = null;
CompoundButton.OnCheckedChangeListener listener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO: 8/6/2018 your logics
}
};
radioButton.setOnCheckedChangeListener(listener);
changeStatus(radioButton, true);
}
private void changeStatus(RadioButton radioButton, boolean status){
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(status);
radioButton.setOnCheckedChangeListener(listener);
}
}
When you need to change status then call
changeStatus(radioButton, true);
In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE); however it just ruines the format of the xml.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
Try this :
rCM.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});
I've made a sample and it works for me just take a look at this code :
I've declared those variables as global
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate() I've got this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton for the use that you want, I recommend you to use CheckBox cause you can only click ONE and this you have to check if the first RadioButton is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
By the way if you want to use this code :
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView when you start the Activity and you'll have to put any RadioButton (if you want) android:checked="true" or false if you want to be showed or not the TextView, but with this code you won't get the click event on the RadioButtons.
I just defined a ToggleButton variable, found the view by the id, set activated to false, and set a OnClickListener. In the onClick method, I checked if it was enabled in a if statement. If it was enabled, then it should have logged it, but I checked in Console and LogCat, and it didn't display anything. Here's my code:
tb = (ToggleButton) findViewById(R.id.onoff);
tb.setActivated(false);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(tb.isEnabled()){
Log.d("", "activated");
}else if(!tb.isEnabled()){
Log.d("", "deactivated");
}
}
});
I don't know how to do the 'code inside text' thing (like onClick should have a gray box over it).
public boolean isEnabled ()
This method is inherited from base class view, which mostly defines whether it is user intractable or not.
You will need to use isChecked() method to determine whether ToggleButton is on or off.
Update your code as:
tb = (ToggleButton) findViewById(R.id.onoff);
tb.setActivated(false);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((ToggleButton)v).isChecked()){
Log.d("", "activated");
}else{
Log.d("", "deactivated");
}
}
});
or second way
in your xml code
<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"/>
in your activity
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}