Possible to enable EditText instantly when its disabled - android

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.

Related

How to get Focus on EditText after checked CheckBox?

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);
}
}
});

onCheckedChanged does not seem to work

I have a checkbox. It is set to true or false depending on if a task is done or not (its a manual change). When the task is done I want the textview label to change frmo not done to done and vice versa. So I have the following code. When they click the checkbox the onCheckedChanged method does get fired off. It chooses sets the string depending on if it is true or false correctly. But then it just exits. I get no error in the logs or on the screen but when I step through the program after it sets the string in the onCheckedChanged method it just exits the getView method completely. I cant understand what is going wrong. Theres a small problem in the first couple lines that the logic for setting if the box is true or false is not entirely correct but thats fine I can fix that no problem. I just cant understand why I cant update the label after clickign the checkbox. Any help would be great.
final CheckBox statusView = (CheckBox)convertView.findViewById(R.id.statusCheckBox);
//statusView.setChecked(true);
if(toDoItem.getStatus().toString().compareTo(ToDoItem.Status.DONE.toString()) == 0)
statusView.setChecked(true);
else
statusView.setChecked(false);
// TODO - Must also set up an OnCheckedChangeListener,
// which is called when the user toggles the status checkbox
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
}
});
TextView statusLabel = (TextView)convertView.findViewById(R.id.StatusLabel);
statusLabel.setText(statusLabelValue);
You will have to change the textview's text in the listener:
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
((TextView)(convertView.findViewById(R.id.StatusLabel))).setText(statusLabelValue);
}
});

Android Switch Status

Good morning, I just started studying Android application development, using Android Studio. My first experiment consisted in creating 3 EditText, 1 AnalogClock and 1 Switch, with "Slide Me!" as text.
With this code:
public void Slider(View v) {
int pri,sec,som;
EditText txt_pri = (EditText)findViewById(R.id.Primo);
EditText txt_sec = (EditText)findViewById(R.id.Secondo);
EditText txt_som = (EditText)findViewById(R.id.Somma);
AnalogClock Orologio = (AnalogClock)findViewById(R.id.analogClock);
pri=Integer.parseInt(txt_pri.getText().toString());
sec=Integer.parseInt(txt_sec.getText().toString());
som = pri + sec;
txt_som.setText(""+som);
Orologio.setVisibility(View.INVISIBLE);
}
which is the only thing I added, other than deleting the "Hello World!" default textbox, I am able to read two numbers and put their sum in the third EditText, while making the analog clock disappear.
What I would like to do next would be to check the switch status and do
the sum, the disappearing clock and changing "Slide Me!" to something else, if switching on;
erasing the sum EditText, the reappearing of the clock and put "Slide Me!" back, if switching off;
but I have no idea where to start from.
Thank you in advance for any help!
Ciao, Lupo
By Switch do you mean toggle? To handle toggle use the following code..
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
//set text here
} else {
// set anything else here
}
}
Or do this
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});

Check state of togglebutton android

Im new to android and I don't get the code on the developers website about this really, I just want to check if the state of the toggle button is on or off. I have the following scenario
if (isCheckedToggleButton?)
{
// do something
}
else
{
// do something else
}
And a method as the guide suggests
public void onToggleClicked(View view)
{
boolean on = ((ToggleButton)view).isChecked();
if(on) {
return;
} else {
}
}
So I just want to see if the toggle button is on or off so I can decide whether to execute the code inside the if or the else. Unfortunately the method provided by the android guide is a void so it doesn't return a boolean. How can I still check the state?
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lift"
android:textOff="Uit"
android:textOn="Aan"
android:id="#+id/switchLift" android:layout_below="#id/btnEindpunt"
android:layout_alignLeft="#+id/btnEindpunt"/>
Use it like this
myToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
// Do something
else
// Do the other thing
}
});
Change
ToggleButton myToggleButton = ((ToggleButton) findViewById(R.id.switchLift));
to
Switch myToggleButton = ((Switch) findViewById(R.id.switchLift));
Also change isChecked to something else like mIsChecked or inside the listener use YourClassName.this.isChecked for changing its value. There is already a local variable with same name.

Android disable button when checkbox not checked

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));
}
}
});

Categories

Resources