• Home
  • About Us
  • Amazon Affiliate Disclaimer
    • Cookie Policy
    • Terms of Use
  • tech and gaming
  • World Tech
  • Sitemap

goto11.net

Tech gear reviews and guide

  • Home
  • About Us
  • Amazon Affiliate Disclaimer
    • Cookie Policy
    • Terms of Use
  • tech and gaming
  • World Tech
  • Sitemap

Android: Create a toggle button with image and no text –

January 2, 2021 by tuio

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:

  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.

  1. 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.

  1. 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.

Give here a description of the image

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.

Give here a description of the image

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

Filed Under: World Tech

About tuio

Recent Posts

  • Logitech Gaming Software User Guide 2021
  • Surface Pro won’t connect to the iPhone’s hotspot
  • Are you sure you want to leave this page?
  • Learn To Use Multiple Apps On Your Windows
  • The 13 Best Cell Phone Companies List in 2021
  • Windows 10 laptop battery drains very fast after update? Apply these tips
  • Best Free and paid parental control apps for iPad 2020
  • Divi by Elegant Themes Review 2020
  • How to boot Apple Mac M1 in Recovery Mode [Definitive Guide]
  • Android: Create a toggle button with image and no text –
  • PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT (in windows 10) –
  • How to Transfer Files Between Two Computers Using LAN Cable
  • Best Remote Cell Phone Spy Software Without Target Phone
  • Best Ways To Tackle Windows 10 Num Lock Issues With Ease
  • Python Machine Learning Third Edition: Book Review

Copyright © 2023 ยท GoTo11.net