When user launch app then I don't want to allow user to edit in EditText. Now what I want that when user click on CheckBox then user able to edit EditText but below is my code and when I checked the checkbox it is not giving focus. how can I achieve this ?
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editText1.setFocusable(true);
editText1.setClickable(true);
editText2.setClickable(true);
editText2.setFocusable(true);
}else {
editText1.setFocusable(false);
editText1.setClickable(false);
editText2.setFocusable(false);
editText2.setClickable(false);
}
}
});
editText.setFocusableInTouchMode(true);
editText.requestFocus();
try this when checked CheckBox
txtMobile.requestFocus();
You should use EditText.requestFocus();
MethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
According to my experience with checkboxes, onClickListener provide reliable results as compared to checkedChangeListener. So I would suggest to use clickListener like this
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkbox.isChecked())
{
txtConsAcNo.setFocusable(true);
txtConsAcNo.setClickable(true);
txtMeterSrMo.setClickable(true);
txtMobile.setFocusable(true);
}else{
txtConsAcNo.setFocusable(false);
txtConsAcNo.setClickable(false);
txtMeterSrMo.setFocusable(false);
txtMeterSrMo.setClickable(false);
}
}
});
Related
I'm new to Android. How to find out how to change the text in the checkbox when checked?
For example I have a check box that is not checked and beside it, it says "I do not accept terms" and I want to change that to "I do accept terms" when the check box is checked.
Try this code:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkBox.setText("I do accept terms");
} else {
checkBox.setText("I do not accept terms");
}
}
});
Can an editText field be enabled instantly when I call editText.setEnabled(true); ?
Currently I have a code which says:
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
row1TextField = (EditText) findViewById(R.id.row1EditText);
row2TextField = (EditText) findViewById(R.id.row2EditText);
if(toggleButton.isChecked())
{
if(row1TextField.isFocused()){
row2TextField.setEnabled(false);
}
}
if(!toggleButton.isChecked())
{
if(row1TextField.isFocused()){
row2TextField.setEnabled(true);
}
}
With this code, the setEnabled on EditText doesn't reflect instantly though. For example is, when I switch between the states ON and OFF of my toggleButton, the Row2TextField isn't enabled/disabled automatically eventhough I'm on row1TextField on. I have to click the other EditText and go back to row1TextField in order to disable/enable it.
Is there a way I can let it reflect its state instantly instead of changing to different Focus in order to enable it?
you should use onCheckedChangedListender for ToggleButton
and should like this
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
row1TextField.setEnabled(true);
row2TextField.setEnabled(false);
}
else
{
row1TextField.setEnabled(false);
row2TextField.setEnabled(true);
}
}
});
In this way, you can be enable/disable instantly.
I want to make an EditText object is focusable or unfocusable according to the CheckBox's situation.But these codes are not working.I can't see an error?How to do this?
atasozuTahmin.setFocusable(false);
tahminAcKapaCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
atasozuTahmin.setFocusable(true);
}else{
atasozuTahmin.setFocusable(false);
}
}
});
Use instead:
atasozuTahmin.setFocusableInTouchMode(isChecked);
atasozuTahmin.setEditable(isChecked);
This can be acheived by setting keyListener to null
atasozuTahmin.setKeyListener(null);
I have a multiple checkbox and a button. What should I do to disable the button if none of the checkbox is check and enable the button if it's checked?
Try with this,
Button mButton=(Button)findViewById( R.id.button01);
CheckBox mCheckBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
mButton.setEnabled(true);
}else{
mButton.setEnabled(false);
}
}
});
When you enter in that view make the button disabled (in your XML), and whenever user hit any of the check-boxes manage one global variable e.g if the global count is > 1 then make the button enable in that activity.
Manage the global variable in a way that if user is turning on the check box then increment it and if he is turning off the checkbox decrease the counter.
I hope you got the concept.
Basically it is all about managing the count how many checkboxes are turned on; if more then one is turned on make the button enabled else make it disabled.
Instead of enabling and disabling the button you can use setVisibility() method for button.
In the following manner.
Button btn =(Button)findViewById( R.id.mybutton);
CheckBox checkBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
btn.setVisibility(VISIBLE);
}
else{
btn.setVisibility(GONE);
}
}
});
By using this method you can set the visibility of your view.Your button will be visible only if checkBox is cheaked otherwise your button will not be visible.Let me know it works or not for you.
Use this:
myButton.setEnabled(false);
See this question for more details.
you can disable button by using below code.
mBtn.setEnabled(false);
and can enable it later by using below code
mBtn.setEnabled(true);
mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mButton.setEnabled(isChecked);
}
});
as simple as that :)
try this
termsAndConditionsCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});
privacyPolicyCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});
What would be the correct way of receiving and sending an event when a check box gets enabled or disabled?
In C# I could just easily double click and all the code would be done for me. But in android it appears to be a bit more obscure. I thought of using the touch event handlers but then if the user has a keyboard it won't detect the change since it's not touch. I figure android should have a native event for check box state change.
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
Since CheckBox (eventually) extends View, you can use a standard OnClickListener to detect when the CheckBox is actually tapped by the user (as opposed to the ListView updates):
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( ((CheckBox)v).isChecked() ) {
// perform logic
}
}
});
In Kotlin:
checkBoxView.setOnCheckedChangeListener { _, isChecked ->
print("checked: $isChecked")
}
Try this
CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);
checkbox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (checkbox.isChecked())
{
//Perform action when you touch on checkbox and it change to selected state
}
else
{
//Perform action when you touch on checkbox and it change to unselected state
}
}
});