eclipse - How to create a custom toggle button with an image on it that also scales correctly for each density in android -


Here's what I'm trying to do:

  • Create a toggle button.
  • The background color of the toggle button when one stops, then there will be a color.
  • The toggle button should have an image focus which is centered on everything.
  • The toggle button may be in 3 states, on, off, on, on, running, in which each case has more sensitivity for example and I can show how something in the button
  • I have been dialed for MDPI at exactly the button dimension diode and it is scaled for all the other density.
  • I want that the image be focused to stretch the image using the appropriate density image of the center.

    What would be the most obvious approach to all this, at least what would be the most favorable for the number of headaches and equipment? Is the whole button making background an image best? For a state and for each one density, using a button for shutting down and overlaying the image, there should be a better way on it and control the background color property for the on / off states? Probably making an XML in the "Drawable" folder is an option.

    Even one of the two states to handle the click-through toggle button The onClick method is required, for this reason I will argue that it is not more complex to create a regular button that handles the visibility of toggling in its own on-one click method.

    Since you want the option of using pictures I will use an image button for example.

    XML for an image button with custom color:

      & lt; ImageButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + ID / toggle button" Android: layout_marginTop = "152dp" Android: layout_centerHorizontal = "true" Android: src = "@drawable / state 0 "Android: onClick =" onToggleClick "Android: background =" @color / red "/>   

    The color properties should be defined in a new XML value file (see)

    The onClick method looks like this in the example to reverse the view of the button. Created for yourself:

      ontoggleClick on Public Zero (see View) {toggleState = (toggleState + 1)% 3; Switch (togglestate) {Case 0: Toggle ButtonSenset Image Resource (ArduoAvate.Astate); ToggleButton.setBackgroundColor (0xffff0000); break; Case 1: Toggle. Settlement Resources (Arduvel.State1); ToggleButton.setBackgroundColor (0xff00ffff); break; Case 2: Toggle. Settlement Resources (ArduWable.State 2); ToggleButton.setBackgroundColor (0xffff00ff); break; }}   

    Please do not harsh the colors as hex values ​​in your final project, create variables for them.

    For reference, hex color is in the form of 0xAARRGGBB

    For Comment:

    If you want to add text and an image to the same button Android, It supports a button that will have an icon on the left side of a button:

      & lt; Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: text = "@ string / button_text" Android: drawableLeft = "@drawable / button_corean" ... /> Source: http://developer.android.com/guide/topics/ui/controls/button.html   

    But then you will not be able to switch the image with the method ImageButton class

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -