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);
Related
Actually, I use a ListView and when I use setClickable(false) I have the animation as if I clicked on a button you see? The animation that shows you click. Which is not normal, I think, basic.
And when I use setClickable(true) I no longer have the animation, as well as if I use
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And i would like to use the OnClickListener but I think it would be better for the user to see that he can click, so to have the animation when you click.
So, I'd like to see when the user clicks on an item in the list, it does the action I want (I'll add that later) but let's imagine a Toast but it displays the effect as if you click on a button. The effect i got if i use setClickable(false) (the default setting).
That's the ripple effect !
In the row's layout of the ListView just add:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
This will add the Ripple effect. If you want to show it on top of the other views, use the forground attribute:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
add this foreground:?attr/selectableItemBackground to your view attribute, it should work
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 .
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.
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.
I am new to Android and was going through the button documentation. I was wondering if the system knows which view I clicked on. Like this button.
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
For the callback function selfDestruct, the documentation says the view passed into the function is the one that is clicked on. So I defined my "selfDestruct" function as followed
public void selfDestruct(View view)
{
view.setVisibility(1);
}
So when I clicked on the button, it should have been set to invisible. But it didn't. What did I do wrong? Or I have to explicitly pass the button "view" to the function in case that system doesn't know which one I clicked on.
view.setVisibility(View.GONE); try using this method.
view.setVisibility(View.INVISIBLE) , you can use this too, but the space occupied by the view, will not be gone out of screen. it will be just invisible.
view.setVisibility(1); that "1" is the same as View.FOCUSABLES_TOUCH_MODE or View.FOCUS_BACKWARD
What you want to use is view.setVisibility(View.GONE);
here is the view options