0

I wanna change the shape of the Toggle Button to be exactly the same as a regular button, meaning exact shape and shadows, I dont want it to look "pressed" when its toggled ON, the reason I dont just use a regular button is because I need the toggle functionality and I figure its harder to implement the toggle functionality to a regular button than changing the appearance of the toggle button to look like a regular one, Im making mi first android app so respond like you're talking to a newbie (cause thats what I am lol).

3 Answers3

1

If I understand you need it not for UI reason but for other purpose; so I think best way is use a regular button and implement onClickListener; something liek this:

    ok.setTag("likeatoggle");
    boolean pressed = context.getSharedPreferences("MY_SHARED_PREF_NAME", Context.MODE_PRIVATE).getBoolean(ok.getTag() + "_pressed", false);
    ok.setBackgroundColor(pressed ? Color.BLUE : Color.RED);
    ok.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // just an example
            boolean pressed = context.getSharedPreferences("MY_SHARED_PREF_NAME", Context.MODE_PRIVATE).getBoolean(v.getTag() + "_pressed", false);
            Toast.makeText(context, "ouch " + v.getTag() + ": " + (!pressed), Toast.LENGTH_SHORT).show();
            v.setBackgroundColor(pressed ? Color.BLUE : Color.RED);
            context.getSharedPreferences("MY_SHARED_PREF_NAME", Context.MODE_PRIVATE).edit().putBoolean(v.getTag() + "_pressed", !pressed).commit();
        }
    });

Edit: I used the code above and I was able to change color on button

Davide
  • 126
  • 8
  • I do need it for a UI, what I want is for the button to change to a color when its clicked and stay in that color until its clicked again, I tried with a regular button but after hours I finally could but it was buggy and didnt work all the time, I tried with the toggle button and it worked instantly and very easy, but the shape of the button is now what I wanted, that setTag("likeatoggle") will give the regular button the toggle functionality when clicked? – Lorenzo Rey Camejo Aug 05 '15 at 23:52
  • But what you changed is the Background color, I already did that and its not what I wanted, I wanna change the BackgroundTint of the button. – Lorenzo Rey Camejo Aug 06 '15 at 00:05
  • i think Tint is available on button from API 22 http://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button – Davide Aug 06 '15 at 00:09
1

You could create a new xml drawable resource like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked=true
           android:drawable="@drawable/something1" />

     <item android:state_checked=false
           android:drawable="@drawable/something2" />
</selector>

(something1 and something2 being drawable resources that are basically a custom button shape created by you) and changing the Toggle background attribute. Prepare your Photoshop skills man, you will need them if you try this!

StG
  • 257
  • 2
  • 11
0

I found the best solution which was dead simple, just throw some regular buttons in your layout, then go to the xml version of the layout, and change the definition of the buttons from