0

I have an EditText that has a stringA stored. When some processes happen, the EditText is updated with a new value (stringB). I have three buttons. I want them disabled while EditText = stringA & updated to enabled when the EditText holds the stringB value.

final EditText latlongText = (EditText) findViewById(R.id.EditText_COord)

This is my EditText. By default, it just holds a string stored in XML as android:hint= "blah blahstringA"

I update it like this in the code:

   latlongText.setText(stringB);

In the onCreate, I have:

 buttonA.setEnabled(false);
 buttonB.setEnabled(false);
 buttonC.setEnabled(false);

I'm not sure where or how to correctly place the code to re-enable these buttons.

Perhaps the "clickable" attribute is also an option? I want them to enable when stringB overwrites the value currently stored as the "android:hint" string.

Thanks

GrumP
  • 1,183
  • 6
  • 19
  • 43

3 Answers3

2

have a look on this using textwatcher basically you need to use textwatchers to get to know when your edittext is changed and you can enable/disable button depeding on the text in edittext

Community
  • 1
  • 1
Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
  • Yes, see my comment below regarding onLcationChanged method. I'm not sure where to place this code. Currently giving syntax errors complaining about bracket placement. – GrumP Oct 09 '12 at 13:09
  • Here's an image: It seems to screw up all the brackets throughout the rest of the code http://i.imgur.com/64kVN.jpg – GrumP Oct 09 '12 at 13:31
  • this is not much clear from the image in on create activity itself add the code as we generally do for button onclicklistener or add your code right here you are having some problem with brackets use cntrl+shift+f to format your code then it should make the things clear – Sourabh Saldi Oct 09 '12 at 13:37
  • 1
    please check where you add this code its within oncreate might be you are adding this code after closing oncreate – Sourabh Saldi Oct 09 '12 at 13:42
2
latlongText.addTextChangedListener(new TextWatcher(){
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {   
   }   
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {    
   }
   @Override
   public void afterTextChanged(Editable s) {
        String st=latlongText.getText().toString().trim()
        if(st.equals(stringA)){
           buttonA.setEnabled(false);
           buttonB.setEnabled(false);
           buttonC.setEnabled(false);               
        }
        else if(st.equals(stringB)){
           buttonA.setEnabled(true);
           buttonB.setEnabled(true);
           buttonC.setEnabled(true);
        }
    }
});
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • Thanks, this has a typo in the first line - However, even following correcting this, I am getting errors regarding the brackets. Maybe I have it in the wrong place in my code. latlong is being set latlong.setText(stringB); This is inside method onLocationChanged(Location loc) so no sure how to apply it to my logic. – GrumP Oct 09 '12 at 13:08
  • 1
    @GrumP: ok..even i have edited my answer,thanks for pointing out! also i have not found any errors regarding brackets so i think,you have mistaken in putting lines at proper place.please check again.You don't need to worry even if it is in onLocationChanged.this method can work well. – Hiral Vadodaria Oct 09 '12 at 13:45
  • Used this code in the onCreate, worked well. I have the desired functionality now. Thanks! :) – GrumP Oct 09 '12 at 14:32
0
if(latlongText.getText().equals(stringA)){
 buttonA.setEnabled(false);
 buttonB.setEnabled(false);
 buttonC.setEnabled(false);
}
elseIf(latlongText.getText().equals(stringB)){
 buttonA.setEnabled(true);
 buttonB.setEnabled(true);
 buttonC.setEnabled(true);
}

try this code

Syn3sthete
  • 4,151
  • 3
  • 23
  • 41