I have a strange requirement. I have multiple rows having checkbox each. Now i need to delete the row as soon as the check box is checked. I have done this so far but its not working.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox checkbox = new CheckBox(this);
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
delete();
}
});
}
Thanks In Advance ...
This is not a good design. The user does not expect a checkbox to delete an item from the list. However, if you really want to do it, use an OnCheckedChangeListener:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChange(CompoundButton checkbox, boolean checked) {
delete();
}
}
Related
I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) radioButton.setChecked(false);
else radioButton.setChecked(true);
}
});
I have made a solution for it using set selected attribute of the radio button.
final RadioButton radioButton = findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?
You are generating a endless loop. That will cause stackoverflow error.
Because you are changing radio button checked status inside onCheckedChanged.
If you need changing check status then do it in click of a button.
Don't change check status inside onCheckedChanged
Perhaps you need to change status of RadioButton for some condition. For that you will do following.
public class MainActivity extends AppCompatActivity {
RadioButton radioButton = null;
CompoundButton.OnCheckedChangeListener listener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO: 8/6/2018 your logics
}
};
radioButton.setOnCheckedChangeListener(listener);
changeStatus(radioButton, true);
}
private void changeStatus(RadioButton radioButton, boolean status){
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(status);
radioButton.setOnCheckedChangeListener(listener);
}
}
When you need to change status then call
changeStatus(radioButton, true);
I have ListView of each item with text view and a check box
when clicking on each item I'm changing the check box state true/false to display selected items in a list.
listSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Helper.hideKeyBoard(getApplicationContext(), view);
checkBox = (CheckBox) view.findViewById(R.id.chkbox_value);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
else{
checkBox.setChecked(true);
}
}
});
I tried using runaable also :
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
}
});
it is working on Marshmallow correctly but not working on Nougat!
Note : when I click on the second item the first item tick is showing in nougat.
Inorder to display list of selected items u need to maintain checkbox state
I have found solution to this issue if you are still facing :
Replace your below code :
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
}
});
To my code & see if it works:
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
checkBox.jumpDrawablesToCurrentState();
}
});
The flow of your code is going like this:
You are clicking a checkbox -> it detects it is clicked and it check itself -> You are checking is it is checked or not -> If its checked then you are using setChecked(false) which unchecks your checkbox.
Remove following code and it should work:
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
else{
checkBox.setChecked(true);
}
Use this listener to check if a checkbox is selected or not and do an action correspondingly.
checkBox setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
);
In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE); however it just ruines the format of the xml.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
Try this :
rCM.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});
I've made a sample and it works for me just take a look at this code :
I've declared those variables as global
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate() I've got this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton for the use that you want, I recommend you to use CheckBox cause you can only click ONE and this you have to check if the first RadioButton is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
By the way if you want to use this code :
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView when you start the Activity and you'll have to put any RadioButton (if you want) android:checked="true" or false if you want to be showed or not the TextView, but with this code you won't get the click event on the RadioButtons.
I have an expandable listview and I have check box for both parent and child. The requirement is that if I check the checkbox of parent , then the check box of children should also get selected.Here is the code
holder.editCheck
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
COuld any one help me with this
Choose the one you want:
The first one will apply the state of the parent checkbox to the child one.
parent_checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
children_checkbox.setChecked( parent_checkbox.isChecked() );
}
});
Or:
The second will only apply the state if the is checked. Ie. if the parent is unchecked, child keep its previous state.
parent_checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (parent_checkbo.isChecked()) children_checkbox.setChecked( true );
}
});
I have a custom listview with one checkbox and two textviews. I'm using a custom adapter to present the list view. The problem is that the onItemclick function doesn't work for the checkbox if I focus on the checkbox directly, it works only when the list is focused first and then checkbox.I want my listview to work similar to checkedtextview where the checkbox gains focus by default.Can anybody suggest me a workaround for this problem? Thank you.
Try to add a click listener to your view returned with the getView() method as follows:
view.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
/// checkboxview.clearFocus()
/// checkboxview.requestFocus()
}....
EDIT. onclicklistener for checkbox:
CheckBox chckKill = new CheckBox(context);
chckKill.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
///// your code here
}
}
});
Add this code to list view activity and change according to your specification.
View v;
LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.listview, null);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.check_box);
checkBox.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//Action
}
});