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.
Related
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.
On a view I have a LinearLayout which I want to collapse when I click on the EditText below and then to expand when we unfocus.
<EditText
android:layout_width="1000dp"
android:layout_height="43dp"
android:layout_centerVertical="true"
android:textSize="14.4sp"
android:gravity="center_vertical"
android:hint="#string/hint_query"
android:layout_toEndOf="#+id/searchImage"
android:id="#+id/searchBoxText"
android:background="#null"
android:layout_marginStart="16dp"
android:inputType="text"
tools:ignore="Autofill" />
I have a onFocusChangeListener that I implemented thanks to Android-Annotations.
#FocusChange
void searchBoxText(EditText searchBoxText) {
Log.d("change focus", "focus has changed with " + searchBoxText.hasFocus());
if (!searchBoxText.hasFocus()) {
if(upperView != null)
upperView.setVisibility(View.VISIBLE);
} else {
if(upperView != null)
upperView.setVisibility(View.GONE);
}
}
And I got a touchListener on the parent which throws :
searchBoxText.clearFocus();
when we click out of the EditText.
The tablet I aim this code for support as the maximum API 24.
My problem is that this code works perfectly in API 28 but not on API 24 where it throws the onFocusChange twice and I didn't find any reason as why it does it or any way to make it work.
I think what happens is:
The user leaves the first item (so focus is going off from first item so focus is "Changing" so the event onFocuseChange trigers.
Then we get the focus on the selected seconed item so again "focusCahnged" so again the event onFocuseChange called.
so you can do :
public void onFocusChange(View v, boolean b) {
if (v.hasFocus()){
v.performClick()); // or any other logic you need
}
}
then it will do what you need only once (when v will get the focus) .
good luck :-)
You can add isFocusable and FocusableInTouchMode in xml for another view
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...
}
i have apk where on start i show one layout and on the top of is radio buttonst which should on check change layout its something like on start is shown layout PC and on click it should change to Playstation or Xbox
in the evening i will post source code, but now how to make it work ? i found few solutions but nothing work
Why not create new actitivies for different layout files? However try this maybe it will help you.
CB_Event.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
setContentView(R.layout.YOURfirstLayout);
} else {
setContentView(R.layout.YOURSecondLayout);
}
}
});
In this example i used CheckBox you can use RadioButton too.
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.