I want to implement a button that keeps its state after I clicked it and until I click another part of the screen. I don't know how to implement this. I've seen the methods setSelected and setPressed. Do you know if I have to use any of them? Thanks!
You can use ToggleButton with style of regular Button and state checked same as click. Then you can control checked state with setChecked(boolean checked) method.
Related
A user defined method to change Color of the layout on the basis of button clicked. And, also keep track of what button is clicked.
Like, if a user clicks any button, then the Id of that button should be saved in some variable and then the method will be called to change the colour of a specific button.
The problem is that I know how to change the colour of a button. It can be "user1.setBackgroundColor(Color.RED);"
But don't know how it can be implemented by using a function that takes a single variable...
If you have already decided which colors to use, I think the simplest way is to do layout.setBackgroundColor(getColor(R.color.new_color)); in the click listener of the button.
You can make your activoty implements View.OnClickListener.
So whenever the user click on a button, the method onClick will be called and the view that was clicked will be the argument of that method. You can call view.getId () to find out which button was clicked or you can use view.setBackground (Color.parseColor ("#999999")) to change the button background.
I have a RelativeLayout. In that I create several ToggleButton views. The user can set those ToggleButtons on and off.
When the user opens the Activity's OptionMenu I want all those ToggleButtons to become OFF. To do this I am setting programmatically the ToggleButtons to OFF in the onPrepareOptionsMenu code.
I have also a PopupMenu registered to a Imagebutton. I want also when the users opens the PopupMenu by clicking the Imagebutton all the ToggleButtons to become OFF. So, I am turning the togglebuttons to off in the Imagebutton's setOnClickListener code.
My issue is that the updates to the Togglebuttons' state (to Off) are shown only after the OptionsMenu or the PopupMenu is closed. Instead I want all the ToggleButtons become Off as soon the user opens the menus. I thought I have to use some OnFocusChangeListener on some view. I tried to use it on the Activity's top layout but it doesn't work.
How could I get the result I want?
I found the solution myself by overriding the onWindowFocusChanged method, like this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
//code to turn off togglebuttons goes here
}
}
what differences between the setOnClickListener(...) and the setOnCheckedChangeListener() int checkbox? And how can i listen the state changed?
... setOnCheckedChangeListener()... And how can i listen the state changed?
You pretty much answered your own question: use setOnCheckedChangeListener() to be notified for checked state changes.
"On checked change" fires when the state changes, which could be due to user clicking, or because your program marks the checkbox as checked. The click listener fires only when the user clicks on the checkbox. Depending on what you need you may need to use one or the other.
All of View (textView, imageView, editText ,etc.) have some similar attribute like onClickListener. but every item has their own specific attribute too.
for example CheckBox has onCheckChangeListener or editText has onTextChangeListener.
With OnCheckedChangeListener you receive an event whenever the checked status changes, even when done in code by using .setChecked().
Depending on what you are doing this can lead to unexpected behavior (for example when you have a checkbox in a listview, the view is recycled and the checkbox state is modified programmatically, it looks exactly the same way as if the user had clicked it).
Therefore, when you are writing code that is supposed to react to a user who clicked the checkbox you should use OnClickListener.
I have an idea, when i click the CheckBox, the state of CheckBox can maintain the original state.
mCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBox.setChecked(mCheckBox.isChecked());
}
});
I am using .setOnCheckedChangeListener to check a change on Radio group with three buttons. as per name it will detect if a change in radio buttons is detected. Hence clicking on the already selected item wont invoke anything.
is there any alternative that will work even for the selected radio , ignoring that it is already selected
You question is a bit confusing. But as far as I understood, onCheckedChanged fires automatically when your activity is loaded first time provided that you have set listener in onCreate method.
You can implement on onClick but then you have to take care of both events. As onClick will fire first and then onCheckedChange will fire. And in OnCheckedChange you can detect whether the Radio button changed or not by using a temporary variable.
Hope this answers your question.
What's the difference between setOnClickListener() & setOnCheckedChangedListener() for checkbox?
I am working with both listener but i could not find any difference,they both gives same answer.Please solve this query
thank you
regards
Ayudh
We use setOnClickListener() for Perform action on clicks, depending on whether it's now checked or not whereas setOnCheckedChangedListener() method used for checkbox change listener -- that is where we handle the changes in the checkbox state.
setOnClickListener() is a listener that can be used mostly with all kinds of view(widgets) for performing action on Click while setOnCheckedChangedListener() is used only to change the state of checkbox and radio buttons. You should prefer setOnCheckedChangedListener() for changing the state of checkbox and perform some action.
Google uses onClickListener in this tutorial, even when they manipulate widgets that support the change listener.