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.
Related
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 .
I'm generating a number of buttons programmatically (some to increase a value while others to decrease a value) that I disable when the value they adjust either reaches a maximum or minimum. (i.e. The 'increase' button is disabled when the maximum is reached while the 'decrease' button is disabled when the minimum is reached.) In addition to disabling, I'm also setting the button state to 'pressed' in order to visually indicate that the limit value has been reached and the button no longer functions.
My button onClickListener for the 'increase' buttons look like this:
increase.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do some stuff
// here I enable the corresponding 'decrease' button because once you increase you can then decrease
setEnabled(decrease, true);
// if the maximum value is obtained, I disable the 'increase' button because you can't go higher
if (value == maximum) {
setEnabled(increase, false);
}
}
});
The corresponding 'decrease' onClickListeners are similar.
Because these button are changing their own state, I need to use a Handler to adjust Button.setEnabled() and Button.setPressed() after a small delay, so that the user's finger doesn't get in the way (see https://stackoverflow.com/a/28788780/852795). Here is the setEnabled(final Button b, final boolean set) method that is used above:
private void setEnabled(final Button b, final boolean set) {
Handler refresh = new Handler(Looper.getMainLooper());
refresh.postDelayed(new Runnable() {
#Override
public void run() {
b.setEnabled(set);
b.setPressed(!set);
}
}, 250); // delayed post required to make pressed button accept setPressed()
}
Everything works great, with one small exception: once a user presses the 'increase' button enough times to reach the maximum the button will be disabled and the 'pressed' state will be set to true, however, if it's then pressed one more time, the 'pressed' state will be turned off. Functionally, the button is disabled. I know this because the onClickHandler is not invoked. Why then is my button losing it's 'pressed' state? How can I stop this from happening?
Update: Looking at https://stackoverflow.com/a/8693444/852795 I tried plugging into the onTouchListener() in order to 'intercept' the touch that's turning off the pressed state. However, this, too, is not invoked because, I would imagine, the button is disabled.
One way to handle this would be to use a StateListDrawable as the background of the button and set the Disabled state to use the same drawable as the Pressed state. This way you could avoid having to set the Pressed state in code.
ie
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_pressed" /> <!-- pressed -->
<item android:state_enabled="false" android:drawable="#drawable/btn_pressed"/> <!-- disabled (same as pressed) -->
<item android:state_enabled="true" android:drawable="#drawable/btn_normal"/>
</selector>
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.
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);
Can I set some message to appear like a "tooltip" for a TextView or Button?
There's no concept of "hovering" in a touch screen, but you could set a LongClickListener for your View, and have a Toast appear after a long press. Something like this:
Toast viewToast = Toast.makeText(this, "My View Tooltip", Toast.LENGTH_SHORT);
View myView = (View)findViewById(R.id.my_view);
myView.setOnLongClickListener(new OnLongClickListener() {
#Override
public void onLongClick(View v) {
viewToast.show();
}
});
EDIT: After reading your comment, you should just use the hint attribute in your EditText XML layout:
<EditText
android:hint="My tip here" />
-First set a textview with your hint and set it to invisible.
-Create an animation xml with alpha animation,specify how long you would like to display(at the end set the animation to zero alpha so that it remains invisible) and put it in res->anim folder
-Inside your onCreate and onClick methods of view that need tooltip
set the text view to visible
Hook the animation(like
R.anim.tooltip) to this text view
-Use boolean flags and allow the user to switch off the tool tips in menu.
I'll leave the code specifics to you. You find them easily in stackoverflow.