I want to achieve this : user check on a unchecked checkbox, a toast displayed, the checkbox then become disable..
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setChecked(false);
}
}
});
the output failed, because they execute on the same time, even I put the male.setChecked(false) outside.. I can't recall there's something to run something 1st, then other thing.. is it thread? really cant remember
If you want to achieve: "user check on a unchecked checkbox, a toast displayed, the checkbox then become disable..",you should try this code:
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setEnabled(false); // disable checkbox
}
}
});
Try this....
- Use the setEnabled(false) on the CheckBox
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setEnabled(false); // Will Disable checkbox
}
}
});
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
//male.setVisibility(View.GONE);//disappear your check box
male.setEnabled(false);//disable your check box
}
}
});
Try to use .onClickListener(View.OnClickListener) with implemented View.OnClickListener. In body of implemented method you will check if the CheckBox is checked or not and set them.
First , Register Your CheckBox to OnCheckedChangeListener.
chk.setOnCheckedChangeListener(this);
then, Overrides it's Method
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
if (chk.isChecked())
{
chk.setEnabled(false);
}
}
Related
I want that when I click first time on this check box it should change the image from my Image View and when I click again on same checkbox it should change the image in Image View with another Image.
I Tried:-
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setBackgroundResource(R.drawable.saveimage);
}else{
myImage.setBackgroundResource(R.drawable.deleteimage);
}
}
});
It's done:-
Take globally (boolean switchStatus = false);
if(switchStatus == true){
dialog.dismiss();
image1.setImageResource(R.drawable.icon_delete);
switchStatus = false;
}else{
dialog.dismiss();
image1.setImageResource(R.drawable.icon_checkmark);
switchStatus = true;
}
There is nothing wrong with your code apart from the lack of using setchecked method. When you check your chechbox , call checkbox.setchecked(true) and when you unckeck it, call checkbox.setchecked(false);
Because your image is in Drawable I think you should try this:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
myImage.setImageDrawable(getResources().getDrawable(R.drawable.saveimage,getApplicationContext().getTheme()));
}else{
myImage.setImageDrawable(getResources().getDrawable((R.drawable.deleteimage,getApplicationContext().getTheme()));
}
}
});
I have a requirement where a checkbox is displayed for a specific setting. When the user taps on the checkbox, I want to display an alert dialog. The checkbox should then only change if the user taps on the confirm button (or similar).
My point is that the OnCheckedChanged listener only fires after the checkbox has changed state, whereas I want to listen for the click before it changes state.
you can use onTouchListener and intercept ACTION_DOWN event for showing alert.
on users choice change checked state of you checkbox programatically.
example:
checkbox.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//show alert
return true; //this will prevent checkbox from changing state
}
return false;
}
});
then call checkbox.setChecked(true); or checkbox.setChecked(false); as user selects yes or no.`
Use this:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==true){
//Show your alert
}
}else if(isChecked==false){
//Show your alert
}
}
});
If would suggest to use an OnTouchListener on the checkbox. If your condition is fulfilled then you can call checkBox.performClick().
You can try it;
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// do anything
// perform logic
}
}
});
You can set onClickListener and set checkbox's checked state accordingly.
myCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// createYourDialog
// onPositiveButtonClicked
myCheckBox.setChecked(true);
// onNegativeButtonClicked
myCheckBox.setChecked(false);
// yourDialog.show();
}
});
I am having a list view (say 10 items with check boxes), I want to restrict the user to select i items (i.e.,i=1 or 2 or 3 or 4...).
Say if i=3, the user can only select 3 check boxes from the list, he will be not able to select the 4th check box. And if we scroll the list view the items will get reset to the old values as we know that can be handle by using Boolean state in the model class.
How can I restrict the user to select particular items from the list view?
Here is my listener:
final int i=3;
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
model.setChecked(isChecked);
if (isChecked) {
GlobalCounter++;
} else {
GlobalCounter--;
}
if (GlobalCounter>i) {
buttonView.setChecked(false);
model.setChecked(false);
}
}
});
You can do it by using
boolean checked = ((CheckBox) view).isChecked();
if(!checked){
//perform action here if needed
}
but in your case you have to manage no of checked boxes 3 information
Do this
final int i=3;
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (GlobalCounter>i) {
buttonView.setChecked(false);
return;
}
model.setChecked(isChecked);
if (isChecked) {
GlobalCounter++;
} else {
GlobalCounter--;
}
}
});
Here is my answer, for those who want to add particular selections in the list view.
Inside Adapter class on onClickListener:
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
model.setChecked(isChecked);
if (isChecked) {
count++;
} else {
count--;
}
Log.d("ttt", "c="+count+" mc="+max_count);
if (count>max_count) {
buttonView.setChecked(false);
model.setChecked(false);
count=max_count;//count will not exceed the limit.
Toast.makeText(context, "You can only select "+max_count, Toast.LENGTH_SHORT).show();
}
}
});
Initialize the int count=0,max_count=0; global to the adapter class, while setting the adapter pass the max_selection count to the adapter class.
Here it is for the value 3:
final ComboAdapter adapter=new ComboAdapter(getApplicationContext(), R.layout.custom_items, arr_list,3);
lv_combo.setAdapter(adapter);
I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!
How do you implement this onTouchEvent? It should fire when the user checks or unchecks the CheckBox widget.
CheckBox checkBox = new CheckBox(activity);
checkBox.setText("Don't present me information again.");
checkBox.onTouchEvent(.....);
The CheckBox widget (and any other widget that extends CompoundButton) has a method setOnCheckedChangeListener, which is the bit you're lacking (you probably don't want to use onTouchEvent in this case).
This example should replace the final line of code in your snippet:
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
// do some stuff
}
}
});
what you need in your case is this
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Toast.makeText(yourActivity.this,"CheckBox is Checked ..",3000).show();
}
else{
Toast.makeText(yourActivity.this,"CheckBox is Unchecked ..",3000).show();
}
}
});