Android Toggle Button State Set Texview Text
i have a toggle button which when set to the on position sets the hint to one of my textview to 'kg' . the initial hint of the text view is 'st' which should be shown if the
Solution 1:
unitToggle.setOnClickListener(new OnClickListener() {
publicvoidonClick(View v) {
StringBuffer result = new StringBuffer();
if(tw1.getHint().toString().equals("kg"))
tw1.setHint("st");
else
tw1.setHint("kg");
Solution 2:
The main reason for the said problem is the logic which has not yet been implemented.
When you click the button for the first time it sets the text to "kg" which it will set always on any number of click. since you have written the statement
tw1.setHint("kg");
inside your onClick() method without keeping the state of the button. emphasized text.
In order to make it correct use a boolean flag and change its state on each click and set the text based on the flag value.
The best way to do it is to use ToggleButton which has the inbuilt on/off states so you don't need to have your on boolean flag and set the hint based on the button state.
Solution 3:
Try
private boolean on=false;
unitToggle.setOnClickListener(new OnClickListener() {
publicvoidonClick(View v) {
StringBuffer result = new StringBuffer();
if(on){
tw1.setHint("kg");
on = true;
}else{
tw1.setHint("st");
on = false;
}
Post a Comment for "Android Toggle Button State Set Texview Text"