I am an android learner, I have a task from my course to write into the log when a checkbox is clicked.
My XML is:
<CheckBox
android:id="#+id/whippedCreamCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped Cream"
android:textSize="16sp"
android:paddingLeft ="24dp"
android:buttonTint="#008000"
android:textAppearance="?android:textAppearanceMedium" />
And corresponding java code is
public void indicateBoxChecked(View v) {
CheckBox whippedCreamCheckBox = findViewById(R.id.whippedCreamCheckBox);
boolean checked = whippedCreamCheckBox.isChecked();
Log.v(TAG, "Checkbox value is " + checked );
}
The remaining code builds, runs and functions without errors. However, in the log I cannot see the desired output. But I see
Not supplying enough data to HAL, expected position 4798584 , only
wrote 4798080
You can access the entire log here https://gist.github.com/latrociny/f318f74bbf9b9e28cc0a3a5370eaf996
Add this attribute to the CheckBox:
android:onClick="indicateBoxChecked"
and change your code to this (it is inside your activity class isn't it?):
public void indicateBoxChecked(View v) {
CheckBox whippedCreamCheckBox = (CheckBox) v;
boolean checked = whippedCreamCheckBox.isChecked();
Log.v(TAG, "Checkbox value is " + checked );
}
now whenever you click the CheckBox, the method indicateBoxChecked() will be executed.
I guess the variable TAG has been initialized.
Related
I'm creating a ListView with a custom Adapter for displaying my entries. Each row contains a checkbox, and my adapter contains the following code:
CheckBox cb = (CheckBox) v.findViewById(R.id.item_sold);
cb.setChecked(p.isSold);
setupCheckboxListener(cb, v, position);
...
private void setupCheckboxListener(CheckBox check, final View v, final int position) {
check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
System.out.println("Should become false!");
cb.setChecked(false);
} else {
System.out.println("Should become true!");
cb.setChecked(true);
}
}
});
My row XML file includes the following:
<CheckBox android:id="#+id/item_sold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_gravity="center"
android:gravity="center"
android:focusable="false"
android:clickable="false"/>
But anytime I press one of the checkboxes, check.isChecked() returns true, even if the box is unchecked. I even checked to make sure that the checkboxes were distinct and weren't picking up just the last value/etc.
Setting up the listeners inline instead of in a method doesn't seem to help, either. It's literally just the isChecked() condition that isn't working appropriately - it seems to always give me the inverse value.
Setting an onClick on the row is not acceptable in this case because I need to allow row selection for something else.
What could be causing this issue?
In my xml:
<CheckBox android:id="#+id/checkboxUpdateLessonPlanAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chkLessonPlanAll"
android:onClick="onCheckboxClicked"/>
In my java:
public void onCheckboxClicked(View view) {
//CheckBox box = (CheckBox) view;
CheckBox box = (CheckBox) findViewById(R.id.checkboxUpdateLessonPlanAll);
box.setChecked(!box.isChecked());
Log.v("qwerty", "checkbox clicked " + box.isChecked() + "!!");
}
I can see my log message in LogCat and it shows it as false when I click on the checkbox but its state doesn't change. It remains unchecked.
Why would you try to overwrite the default behavior with something like the default behavior? The checkbox toggles automatically on every click.
If you want to react on that, use the OnCheckedChangeListener.
To Make CheckBox checked or unchecked you can also use like
box.setChecked(true);
box.setChecked(false);
and to get state of CheckBox
if(box.isChecked()) {
//do something here...
} else {
//do something here...
}
In my form, I hava a CeckBox and its required to be checked to submit. If it's unchecked, I call setError() to warn the user. But, once the error is displayed, it don't disappears, no matter the checkbox is checked or not.
Other bug is that if the error text is much long, it appears incomplete. In landscape mode, it appears complete.
checkbox xml:
<CheckBox
android:id="#+id/reg_terms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Li e concordo com os Termos de Uso."
android:focusableInTouchMode="true"
android:focusable="true"
android:textColor="#000000"
android:checked="false"
/>
Validation (inside submitButtons's onClick):
if(!reg_terms.isChecked()){
reg_terms.setError("É preciso aceitar os Termos de Uso");
validation = false;
}
There isn't any "addOnFocusChangedListener" methot I could use to "dismiss" the error if user checks the box? And how could I avoid the error text to appears incomplete?
Viele danke für alle Hilfe. :D
As far as clearing the error, you can add an OnCheckedChangeListener like this:
reg_terms.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
reg_terms.setError(null);
} else {
reg_terms.setError("É preciso aceitar os Termos de Uso");
}
}
});
Personally, I would extract the string into your R.strings as well and reference it in the XML and the code.
For the text wrapping, try adding reg_terms.setMaxLines(2) to your code to allow text to fill up to 2 lines. I'm not sure if this will work, but it's worth a try.
I'm trying to code a checkbox into a help screen which is essentially a pop up view (an activity that's started by the main program) containing a ScrollView that has the help text, and OK button, and a checkbox that asks if you want the help screen to appear automatically at program start. Everything displays properly on the screen, and the checkbox toggles, when touched. However, when OK is pressed, and I test the state of the checkbox with .isChecked() I'm always getting a false. I'm sure it's something simple I've missed. XML file follows follows:
helpdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#ffffff">
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="300px">
<TextView android:text="#+id/helpView" android:id="#+id/helpView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000"/>
</ScrollView>
<Button android:id="#+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text=" OK "
android:layout_below="#id/ScrollView01"/>
<CheckBox android:id="#+id/chkNoHelp" android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Don't Display at Startup"
android:layout_below="#id/Button01" />
</RelativeLayout>
HelpBox.java:
public class HelpBox extends Activity {
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.helpdialog);
dialog.setTitle("Help");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.helpView);
text.setText(getString(R.string.help_text));
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Boolean mDisplayHelp;
setContentView(R.layout.helpdialog);
checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
if (checkBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}
Setting breakpoints on both "mDisplayHelp" lines always breaks at the 'false' branch regardless of whether the check box displays a green check or not when the OK button is pressed.
Thanks in advance.
Edit (10/10):
Its clear what I want to do is pick up information after the user exits the dialog, so I can sense the state of the checkbox and store it as a preference. For this I assume I have to test it in onDestory. So, I did that:
#Override
public void onDestroy() {
Boolean mDisplayHelp;
setContentView(R.layout.helpdialog);
checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
if (checkBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
}
However, I'm still always coming up with FALSE as a result, regardless of whether the checkbox is display checked or off. In this instance, if I don;t include the setContentView, I get a NullPointerException on the isChecked.
Why are you calling setContentView a second time? After the second time you call it, your checkbox is being reset to unchecked (the default state) and then you are immediately checking its state to set a flag.
Its not clear what your intention is here by inflating helpdialog.xml twice, once in a Dialog and once in your main Activity. It sounds like you want to use a dialog-themed activity here and only call setContentView() once in onCreate. It should behave as expected after that.
I finally figured this out, and boy was I going about things the wrong way. Chalk it up to learning curve.
What I needed to do was save the checkbox state. I needed to use SharedPreferences for that. I needed to detect when the checkbox was touched by using a listener on it and then saving the preference at that time. Upon opening the help box, I load the preference state and set the check box to that state so it shows properly based on the user's previous setting.
Finally, load the preference in onCreate at the main activity and call the showHelpBox() if the preference was so set.
In onCreate of main activity:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean mDisplayHelp = prefs.getBoolean("checkboxPref", false);
if (mDisplayHelp == false)
showHelpBox();
In help box activity pre-initialize the check box based on previous setting:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mDisplayHelp = prefs.getBoolean("checkboxPref", false);
final CheckBox ckBox = (CheckBox) dialog.findViewById(R.id.chkNoHelp);
ckBox.setChecked(mDisplayHelp);
When checkbox is touched:
ckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (ckBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkboxPref", mDisplayHelp);
// Don't forget to commit your edits!!!
editor.commit();
}
});
Hopefully others can learn from my foolish mistakes and misunderstanding.
I'm facing a problem while trying to create Checkboxes Dynamic in my application. The design works fine, and I'm able to create as many Checkboxes as i want. The Checkboxes are put into a TableRow together with a TextView so that the text is at left side of the checkbox. But my problem is, that in my activity i can get the "status" of the checkbox, whether it's checked or not.
I use the inflater to create my Checkboxes. The xml for the checkboxes:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/TableRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:paddingLeft="20dip" android:id="#+id/tv_effect" android:gravity="left" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#000" android:textSize="18dip" android:layout_width="wrap_content"></TextView>
<CheckBox android:id="#+id/cb_effect" android:layout_height="wrap_content" android:text="" android:layout_gravity="right" android:layout_width="wrap_content"></CheckBox>
The function i call to create a new tablerow containing a textview and a checkbox:
public void layoutMakeSpeakerEffect(String effectName,int effectNumber)
{
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myViewSp = linflater.inflate(R.layout.speaker_settings, null);
final TextView tv_effect = (TextView) myViewSp.findViewById(R.id.tv_effect);
tv_effect.setId(effectNumber);
tv_effect.setText(effectName);
final CheckBox cb_effect = (CheckBox) myViewSp.findViewById(R.id.cb_effect);
cb_effect.setId(effectNumber);
cb_effect.setText("");
cb_effect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(cb_effect.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": ON", Toast.LENGTH_SHORT);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": OFF", Toast.LENGTH_SHORT);
toast.show();
}
}
});
tl_speakerSettings.addView(myViewSp);
}
And as mentioned, the design works fine.
But how am I able to outside this function to get the "status" off the checkbox?
And i also need a function that could clear the checkboxes status, and another function that could enable and disable the checkboxes?
I can't seem to figure this problem out by myself.
My only idea, is to make something that checks the "cb_effects" ID and afterwards checks the status of the desired checkbox.
Use setOnCheckedChangeListener instead. It will fire the onCheckedChanged(CompoundButton buttonView, boolean isChecked) method, where you can now the state of the checkbox reading the isChecked variable.
You can make use of a global integer array that will contain as many elements as there are rows in table. Set the value of the element to 1 if checkbox is checked and 0 for unchecked. By finding which element is having 1 you can fing which of the checkboxes are checked. Hope this works for you.