3

I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying

mf=(ToggleButton)findViewById(R.id.mf);
 if(mf.isEnabled()==true)
 {
 mf.setTextSize(13);
 }
 else
 mf.setTextSize(8);
asked Sep 1, 2011 at 8:41
8
  • I think that code its ok. Where do you change the size? In the main activity or in other thread/class ? Commented Sep 1, 2011 at 8:44
  • main activity .. code 's not running Commented Sep 1, 2011 at 8:45
  • 1
    You can use if (mf.isEnabled()) instead of if(mf.isEnabled()==true). Commented Sep 1, 2011 at 8:52
  • Its true, the problem is that call. The correct method is mf.isChecked() Commented Sep 1, 2011 at 8:54
  • @muffinbubble nope.. it makes no difference.. same result! Commented Sep 1, 2011 at 8:56

3 Answers 3

15

Your code has to be called each time you click on your button. so use :

toggleButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 if (toggleButton.isChecked()) {
 toggleButton.setTextSize(13.0f);
 } else {
 toggleButton.setTextSize(8.0f);
 }
 }
});

You can set OnClickListner with a easy method. In your .xml put the option

android:onClick="nameOfMethod" 

And then in your code:

public void nameOfMethod(View v){
}

Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )

marimaf
5,4203 gold badges53 silver badges70 bronze badges
answered Sep 1, 2011 at 9:02
Sign up to request clarification or add additional context in comments.

5 Comments

Or if you have already a function called when you click on your button, put the code inside this function :)
I assumed that @ross put the code inside onclick but good apreciation
I assumed the same too. I guess we shouldn't!
Yeah, I tried it and it works on my phone, so I thought he might have not put it insind onClick.
Nice addition Aracem, +1 Zoleas.
4

I put the solution here:

 mf=(ToggleButton)findViewById(R.id.mf);
 if(mf.isChecked())
 {
 mf.setTextSize(13);
 }
 else
 mf.setTextSize(8);

Use isChecked() instead of isEnabled()

answered Sep 1, 2011 at 8:57

1 Comment

Need more information. Logs if a error ocurs... check if R.id.mf ist ok
3

You need to do some debugging.

Firstly:

if(mf.isEnabled()==true) 

can be

if (mf.isEnabled()) 

Does mf.setTextSize(13) on it's own work as expected?

Add some logging:

 if (mf.isEnabled())
 {
 // Add some logging, is this line reached correctly?
 mf.setTextSize(13);
 }
 else
 // Add some logging, is this line reached correctly?
 mf.setTextSize(8);

Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.

answered Sep 1, 2011 at 8:58

2 Comments

yes.. mf.setTextSize(13) works.. but neither do isEnabled or isChecked do .. and isEnabled alwaz is true.... so it sets the size to 13px
Zoleas' answer is probably correct, you need to set toggleButton.setOnClickListener.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.