Development issue/problem:
Is it possible to create a shift key in Android that contains an image but no text?
Ideally it should look like this:
I’ve seen similar messages where the answer is to change the background, but I want to keep the Holo Light layout and just change the text with images.
I need to be able to change the source of the image by programming,
Do you have any idea how I could do that?
If this doesn’t work, is there a way to create a normal on/off button?
How can I solve this problem?
Solution 1:
- Can I replace the switchable text with an image?
No, we can’t, although we can hide the text by replacing the standard style of the toggle, but this still doesn’t give us the toggle you want, because we can’t replace the text with an image.
- How to make a normal shift key.
Create an ic_toggle file in your reloadable/drawable folder
Here are @drawable/ic_slide_switch_on and @drawable/ic_slide_switch_off the images you create.
Then create another file in the same folder, give it the name ic_toggle_bg.
Now add your own theme (if you don’t have the styles.xml file in your res/values/folder).
This will create a custom change key for you.
- How to use
Use a custom style and background according to your wishes.
Solution 2:
ToggleButton inherits from TextView, so you can define the renderings to be displayed on all 4 text frames. You can use this function to display the desired icon above the text and hide the actual text.
The result, compared to a normal toggle button, looks like this.
The second option is to use ImageSpan to effectively replace the text with an image. The appearance is a bit better because the icon is in the right place, but cannot be implemented directly with the xml layout.
You make a regular ToggleButton.
Then set the text per program
ToggleButton = (ToggleButton) findViewById(R.id.toggleButton3);
ImageSpan imageSpan = new ImageSpan (this, android.R.drawable.ic_menu_info_details);
SpannableString content = new SpannableString(X);
content.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setTextOn(content);
button.setTextOn(content);
button.setTextOff(content) ;
The result is in the middle here – the symbol is directly below it because it takes up the space of the text.
Solution 3:
create toggle_selector.xml in res/drawable mode
set the selector switch to the operating mode switch
Pay attention: To remove the text I used in the code above.
textOff=
textOn=
Solution 4:
I know it’s a little late, but for those who are interested, I’ve created a custom component that is essentially a button for switching images, the image can have states as well as a background.
https://gist.github.com/akshaydashrath/9662072
Good luck!
Related Tags:
toggle button background android,android custom toggle button example,android studio toggle button onclick,toggle image on click android,android toggle button,android toggle button set image programmatically,android custom toggle button animation,sliding toggle button in android,heart toggle button android,toggle button with text='' android,android custom switch button with images,image toggle button in android,android toggle button example