0

Here's what i'm trying to do:

  • Create a toggle button.
  • The toggle button's background color will be one color when on, one color when off.
  • The toggle button will have an image centered over everything.
  • The toggle button may have 3 on states, off, on, on, on, wherein each on state is for greater sensitivity for example and I may want to show that in the button some how.
  • I'd like the button dimensions dialed in exactly for mdpi and have it scale for all other densities.
  • I want the image centered on the button to stretch too using the appropriate density image of course.

What would be the cleanest approach to do all of this, least number of headaches and most compatible for devices? Is making the entire button background an image best? One for the on state and one for the off for each density. Would using a button and overlaying an image onto it be a better approach and control the buttons background color property for the on/off states? Maybe creating an XML in the "drawable" folder is an option.

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72

2 Answers2

0

Android has a pretty specific way to deal with the various pixel-densities of android phones which is to provide a separate bitmap for each pixel-density-category you wish to support (extra-high, high, medium..)

Please view this page for how to support multiple pixel densities with your images. http://developer.android.com/training/multiscreen/screendensities.html

camlunt
  • 48
  • 9
  • Hi cam9. Thank you for the reply. I completely understand the link you sent. I've brushed up several articles like this already. I really want an answer that describes the best approach as I indicated in the last paragraph. There's many ways to do all of this putting the topic of density aside. – Steve Tremblay Oct 09 '14 at 01:23
0

Even the built in two-state toggle button requires an onClick method to handle click events, for that reason I would argue that it is no more complex to have a regular button which handles visual toggling in an onClick method of its own.

Since you want the option of using images I will use an ImageButton for the example.

The XML for an ImageButton with a custom color:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/toggleButton"
    android:layout_marginTop="152dp"
    android:layout_centerHorizontal="true"
    android:src="@drawable/state0"
    android:onClick="onToggleClick"
    android:background="@color/red"/>

Color assets should be defined in a new XML values file ( see http://developer.android.com/guide/topics/resources/more-resources.html#Color)

The onClick method to handle toggling the visuals of the button looked like this in the example I built for myself:

public void onToggleClick(View view){
    toggleState = (toggleState+1)%3;
    switch(toggleState){
        case 0:
            toggleButton.setImageResource(R.drawable.state0);
            toggleButton.setBackgroundColor(0xffff0000);
            break;
        case 1:
            toggleButton.setImageResource(R.drawable.state1);
            toggleButton.setBackgroundColor(0xff00ffff);
            break;
        case 2:
            toggleButton.setImageResource(R.drawable.state2);
            toggleButton.setBackgroundColor(0xffff00ff);
            break;
    }
}

Please don't hardcode the colors as hex values, make variables for them, in your final project.

For reference hex colors are in the form of 0xAARRGGBB

To the comment:

If you would like to pair text and an image on the same button android supports that in XML. It would look something like this to have an icon to the left of a button:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
source: http://developer.android.com/guide/topics/ui/controls/button.html

But then you wouldn't be able to switch the image with the method of the ImageButton class.

camlunt
  • 48
  • 9
  • Thanks cam9. ImageButton was one option I was considering. My only concern was I also need to overlay text on top of the button somewhere above or below the image. I of course don't want to burn the text into the image so that I can internationalize text strings. Is there a recommended way to overlay text? FrameLayout? – Steve Tremblay Oct 10 '14 at 20:23
  • Can you explain why the method won't work to switch images? What's your opinion on using FrameLayout to overlay the text? – Steve Tremblay Oct 10 '14 at 22:55
  • The edited answer uses a Button with a drawableLeft element to set a drawable to the left of text. The first answer uses an ImageButton which has an image asset referenced by "src" which can be changed with the method setImageResource. Maybe you can swap drawables of a Button with a different method though. Don't do something silly like overlaying text with a FrameLayout thats very hacky: put it on the button with XML. – camlunt Oct 12 '14 at 17:01
  • So it looks like ImageButton won't allow an image plus text through its XML. Button will but I need the image centered both ways and the text at the upper right corner. If I can't do that, what are my options? Seems like the lesser of all evils is to use FrameLayout and stuff an ImageButton and TextView within it. I would love to avoid FrameLayout, i'm just not seeing the flexibility I need using a default control. – Steve Tremblay Oct 13 '14 at 21:22
  • Steve it sounds like you need to do a little more googling and research. I've given you a lot to work with here. Tell me what you get as far as adding a text tag to an ImageButton. – camlunt Oct 14 '14 at 00:56
  • Thanks for your time cam9. Appreciate the help. It blows me away how difficult this is when it should be so easy. Looking at the link below just makes me shake my head. Mass confusion, mass answers, some guesses. Google really needs to clean up their act. At any rate i'll be trying all of the suggestions in that link and hopefully one of them pans out for me. Looks like I can definitely avoid FrameLayout which makes me happy. Thanks again. http://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton – Steve Tremblay Oct 14 '14 at 18:56