how to fix CheckBox? - android

I have six CheckBoxes so, I need that if user is check the checkBox he can see button orange that available, but if he does not check the button is grey color and not available.
So, I tried to do that and the problem is, if the user check all the checkBoxes and after not check, the button grey is still available and does not change to orange.
this is my code of one checkBox:
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mCheckBox.isChecked()) {
mImageButtonOrange.setEnabled(true);
mImageButtonOrange.setVisibility(View.VISIBLE);
mImageButtonError.setVisibility(View.GONE);
} else {
mImageButtonOrange.setEnabled(false);
mImageButtonOrange.setVisibility(View.VISIBLE);
mImageButtonError.setVisibility(View.VISIBLE);
}
The checkBox is checked, the orange button is available:
enter image description here
so, after we check, we want to unCheck, the button grey is available Although, the checkBox is checked and the button orange is not availble:
enter image description here
so, how I can change this, that after I uncheck the CheckBox and I have another checkBox that checks, the button orange is Visibility and button grey gone?

You can also use RadioGroup for the same and manage orange Button visibility using RadioGroup onCheckedChange event.
Put all the RadioButton inside <RadioGroup></RadioGroup> in xml .

Related

How do I programmatically check an Android radio button with ripple animation?

tl;dr: When the user clicks an unrelated text view, I want a radio button to be selected with a ripple animation. performClick doesn't do this. What should I be doing instead?
I have a radio button with both a label and a longer description.
The general structure of my layout is this:
<RadioGroup>
<RadioButton
id="#+id/officialBusinessRadio"
text="Official business" />
<TextView
id="#+id/officialBusinessCaption"
text="This operates under a ..." />
<!-- ... -->
</RadioGroup>
I've added an OnClickListener to the text view so that tapping on the caption selects the radio button:
officialBusinessCaption.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
officialBusinessRadio.performClick();
}
});
When I tap on the radio button itself or on its own text ("Official Business"), the radio button animates to selected and a ripple ink effect occurs. When I top on the caption text (and performClick is called), the radio button still animates to selected, but no ripple occurs.
Is there a more correct way to get this effect? That is, is there a better way to forward a click on the caption text view to the radio button? If not, is there a straightforward way to trigger the ripple effect on the radio button programmatically?
I would prefer against setting all the text using a string resource for the following reasons:
I intend on styling the label and the caption differently.
I want the radio vertically center aligned with the first line of text.
I've hit this problem by population RadioButtons in a RecyclerView. All of them are set to isClickable = false because I override their selection according to another rule, but the way to trigger the selection to true is doing:
Kotlin:
radioButton.post {
radioButton.isChecked = true
}
Java:
radioButton.post(new Runnable() {
#Override
public void run() {
radioButton.setChecked(true);
}
})
before, I was doing setChecked(true) only and it wouldn't work.

Android: change button image when a different button is clicked

Every example I could find for changing a button image shows how to change it when that button is clicked. But how would I click a toggle button, and have that click change the image of a regular button?
For a little more detail, I have two buttons, and an onCheckedChanged event:
<ToggleButton
android:id="#+id/toggleButton1"
android:text="#string/toggle" />
<Button
android:id="#+id/btn1"
android:onClick="onClick"
android:text="#string/button />
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked)
{
}
When the toggle button is pressed and the onCheckedChanged event occurs, I need the background of btn1 to be set to a new image.
on Button Action :
button.setBackgroundDrawable(getResources().getDrawable(.R.drawable.your_image));
Wow, more than a week later, there's only one response, and it's wrong. Usually there are 10 posts with the same response to every question; are these forums dead?
Guess I'll answer my own question (I actually did this for an imageButton, but I believe it is the same for a button):
btn1.setImageResource(R.drawable.your_image);

using button to switch the input panel and input preview panel in the same activity in android

I aims to click the button to switch the input panel and input preview panel in the same activity so i am doing sth like that:
In one activity,there are one EditText,one button with the text value 'preview' and one textView. Initially, the visibility of editText and button is visible, the textview is set gone value. When the button is clicked,it gets the input value from the editText and set the value to textview. Also, the visibility of editText is set gone and textview is set visible.Finally, the text of button change to 'edit'.
To do this, I have written some code like that:
public void showNewsPreview(){
txtContentPreview.setText(edtContent.getText());
edtContent.setVisibility(View.GONE);
txtContentPreview.setVisibility(View.VISIBLE);
btnPreview.setText("Edit");
}
As a result,the code has been run successfully but the view dose not change.
whether i was missed something or the workflow has problem?
Slight change:
public void showNewsPreview(){
txtContentPreview.setText(edtContent.getText().toString());
edtContent.setVisibility(View.GONE);
txtContentPreview.setVisibility(View.VISIBLE);
btnPreview.setText("Edit");
}

How to detect touch events on disabled checkbox in android

I have a layout with couple checkboxes. When one checkbox gets checked the others are set to CHECKED=true and ENABLED=false. Now I want the user to be able to tap on any of that disabled checkboxes and if he does that, one is set to enabled and checked and all the other are disabled.
The onTouchListener as well as the onClickListener doesn't seem to be called when the checkbox is set to ENABLED=false. Can anyone help?
You can not receive events on a disabled checkbox. If you put the disabled checkbox on a layout like FrameLayout, you can receive the events when you click on the layout but not in the disabled checkbox. The best way if you want to capture events on a disabled checkbox is to simulate a disabled checkbox simply, and capture a long click event to activate again, for example.
What I have done is a checkbox with white text color but beginning with grey text color and unchecked, with a boolean stopper variable which you check before on every onCheckedChanged method. Checkbox is never checked unless you change the boolean stopper variable. You can press on checkbox many times as you want and nothing happens. It only appears to be disabled but when you press a long click you unblock the boolean stopper variable and change the grey text color checkbox to white like a normal checkbox. You can change the stopper variable when you want and "disabling it again"
In color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="grey">#808080</color>
</resources>
In main.xml:
<CheckBox
android:id="#+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="text" />
In the main.java code, onCreate method:
//define a boolean stopper variable to check on event
boolean chkActivated = false;
checkbox = (CheckBox) findViewById(R.id.checkbox1);
checkbox.setTextColor(getResources().getColorStateList(R.color.grey));
checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(chkActivated){
if (isChecked) {
//Do everthing you want when checked
}else{
//Do everthing you want when unchecked
}
}else{
checkbox.setChecked(false);
Toast.makeText(Activity.this, "It is disabled. to activate press long click"), Toast.LENGTH_SHORT).show();
}
}
});
checkbox.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
chkActivated = true;
checkbox.setTextColor(getResources().getColorStateList(R.color.white));
checkbox.setChecked(true);
return true;
}
});
Hope this helps you
Usually the behavior for a situation like that is a question-mark in Android. One thing you may be able to do is to put the CheckBox from something descended from ViewGroup (One of the many layouts like FrameLayout, for example) and use setOnClickListener on it.

How to change a textview with both addTextChangedListener and RadioButton changes

I have a textview that I have linked to an edittext box so that when it is edited, it changes my textview. I have a radiobutton used in the interpretation of the textview but it has to be selected before i edit the edittext box in question.
What I would like is for the textview to be changed when i change the radiobutton AS WELL AS when i change the edittext boxes. An example is:
I have 3 Edittext boxes and 1 RadioGroup (consisting of 2 RadioButtons [but one of the buttons is selected by default]). I have 1 Textview.
I would like it so that when all edittext boxes are filled in, the textview is changed. I also want it so that when i change the radiobutton, it will change the textview again. And then i can go back and edit any of the 3 edittext boxes and it will change the textview. At the moment, i only have 1 addTextChangedListener in use at the moment, which is on the edditext box at the bottom of the screen (in the hope the user fills all the top fields in first). But the problem is that if the user changes the topmost edittext box, it doesnt change the textview because the listener is on the bottom box.
Hope that is clear.
Add a text changed listener to all the text boxes.
This listener should be the same for all of them. It simply needs to check if the other boxes have been filled.
if(
textBox1.getText().toString() != "" &&
textBox2.getText().toString() != "" &&
textBox3.getText().toString() != "") {
//All boxes have a value, so change the TextView
....
}

Categories

Resources