I am trying to finding a way to get onClick event on radio button. I know that there is way to get a selected radio button value like this:
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
But I have a requirement, that when user click/select radio button by touch on a screen then do some logic. So while using this way when i choose a radio button which user previous selected then radioButton.setOnCheckedChangeListener is calling. It is little bit difficult to distinguish that who is calling onCheckedChanged event from code(when show previous selection) or user himself click on screen.
Can someone tell me how i can find onClick on touch event on radioButton(not radioGroup)
Just set onClick in XML file like this :
android:onClick="Button1"
After this, in your java file :
OnClickListener yourRadiolistener = new OnClickListener (){
public void onClick(View v) {
//Do whatever you want to do
}
};
RadioButton Button1 = (RadioButton) findViewById(R.id.yourFirstRadioButton);
Button1.setOnClickListener(yourRadiolistener);
try this to get click listener to your radio button
RadioButton radioButton = (RadioButton) findViewById(R.id.yourRadioButton);
radioButton.setOnClickListener(radio_listener);// set listner to your radio button
than create a new radio_listener using below code
OnClickListener radio_listener = new OnClickListener (){
public void onClick(View v) {
//perform your action here
if(v==radioButton){
//perform your action here
}
}
};
you can get the id of each view that has trigered the checkchange event so get the id inside the
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.getId()==firstButton)
compare the id and apply further logic.
Alternatively, you can set the click handler directly in XML:
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler" />
</RadioGroup>
Your activity must then implement the onClick handler:
public class MainActivity extends AppCompatActivity {
...
public void onClickHandler(View view) {
public void onClick(View view) {
switch (view.getId()) {
case R.id.radioButton1:
...
break;
case R.id.radioButton2:
...
break;
}
}
}
}
Here are the official docs: https://developer.android.com/guide/topics/ui/controls/radiobutton.html
Related
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);
}
});
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();
}
});
my problem is following:
I want indentify (the keyevent) when the user click in the mute/unmute button and so change the satus of toggle button.
One image to understand better: https://lh6.googleusercontent.com/-yHjyvtCiyac/Ul6uQz_Ga2I/AAAAAAAAAiY/Ug06zyq7_Vg/w619-h81-no/volumebar.jpg
How do this?
Thanks.
Sorry for my english. I use Google Translator for do this >.<
Update: The mute/unmute button in XML file :
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/audiobtn"
android:textOn=""
android:textOff="" />
Last Update: The problem was solved using the BroadcastReceiver class. Thanks for the help!
For Toggle Button
add this line in toggle button xml
android:onClick="onToggleClicked"
Now in your activity class set this listener as
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// set image for on state
} else {
// set image for off state
}
}
More info for Toggle button can be find here
For simple button
Step 1 Create object on your button
Button muteBtn=(Button)findViewById(R.id.mute_btn);
Step 2 Create the click listener of this button and change it to pressed/non pressed state using a boolean which stores its state like this.
boolean muteBtnSelection;
muteBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (muteBtnSelection) {
muteBtnSelection=false;
// set yout image for mutebtn_pressed
muteBtn.setBackground(R.drawable.mutebtn_pressed);
} else {
muteBtnSelection=true;
// set yout image for mutebtn_not_pressed
muteBtn.setBackground(R.drawable.mutebtn_not_pressed);
}
}
});
in this way you can toggle between pressed and non pressed state.
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"/>
From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the following can be used:
cb=(CheckBox)findViewById(R.id.chkBox1);
cb.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");
}
}
However, I am unable to work out the logic on how to do the above for a radiogroup.
Here is the xml for my RadioGroup:
<RadioGroup android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
Question: Do i need to setup another listener, or will the listener already there also "register" this group?
Also, should the listener be set up on the RadioGroup or the RadioButton?
This is how you get the checked radiobutton:
// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
To use the listener, you do this:
// This overrides the radiogroup onCheckListener
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
// Changes the textview's text to "Checked: example radiobutton text"
tv.setText("Checked:" + checkedRadioButton.getText());
}
}
});
It should be something like this.
RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
}
}
});
Based on the checkedId, you would know which of the radiobutton has been clicked and then use your above code to figure out if its checked or unchecked. This is homework. ;)
Using Switch is better:
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
switch (id) {
case -1:
Log.v(TAG, "Choices cleared!");
break;
case R.id.chRBtn:
Log.v(TAG, "Chose Chicken");
break;
case R.id.fishRBtn:
Log.v(TAG, "Chose Fish");
break;
case R.id.stkRBtn:
Log.v(TAG, "Chose Steak");
break;
default:
Log.v(TAG, "Huh?");
break;
}
}
});
//Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
If you want to see which radio Button is checked or selected in the radio group then use the following:
//1. declare the radio group and the radio Button in the java file.
RadioGroup radiobtn;
RadioButton radio;
Button btnClick;
//the radio is the element of the radiogroup which will assigned when we select the radio button
//Button to trigger the toast to show which radio button is selected of the radio group
//2. now define them in the java file
radiobtn = findViewById(R.id.radiobtn);
btnClick = findViewById(R.id.btnClick);
//we are instructing it to find the elements in the XML file using the id
//3. Now Create an on Click listener for the button
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selectedId = radiobtn.getCheckedRadioButtonId();
//we are defining the selectId and then we are fetching the id of the checked radio button using the function getCheckedRadioButton()
radio = findViewById(selectedId);
//now the radioButton object we have defined will come into play, we will assign the radio to the radio button of the fetched id of the radio group
Toast.makeText(MainActivity.this,radio.getText(),Toast.LENGTH_SHORT).show();
//we are using toast to display the selected id of the radio button
//radio.getText() will fetch the id of the radio Button of the radio group
}
});