10

I am seeking to make two buttons have a thin glow around them, kinda pulsating-fading effect

glowlike effect around button example image

The buttons have a background drawable image, and i just wanna have a glowing effect around it to make it kinda resonate with the music playing

I have already searched around multiple threads, and they were either just about an imageView, or upon pressed, or a Bitmap, so not necessarily what I am seeking

Here is my Button XML:

<Button
android:id="@+id/playEasyMode_Button"
android:layout_width="145dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/play_easy_button"
android:onClick="playEasyMode"
android:layout_margin="15dp"/>

Please dont try suggesting using background drawables upon pressed and whatnot, this is not what I am looking to do. I wanna set such effect either programatically or in the XML


edit: Object Animator offloat relative layout error image edit2: ok so as i have almost achieved the effect I want thanx to Aman Verma below, here is my updated XML right now:

<RelativeLayout
    android:id="@+id/relative_layout_id"
    android:layout_width="160dp"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:background="#FFC107"
    android:alpha=".1"
    android:layout_gravity="center_horizontal"
    android:layout_margin="15dp">

    <Button
        android:id="@+id/playEasyMode_Button"
        android:layout_width="145dp"
        android:layout_height="40dp"
        android:layout_marginLeft="8dp"
        android:background="@drawable/play_easy_button"
        android:onClick="playEasyMode"
        android:layout_marginTop="8dp"/>

</RelativeLayout>

and this is the java code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home_screen_layout);

    //glowing button effect
    RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relative_layout_id);

    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(relativelayout, "alpha", .5f, .1f);
    fadeOut.setDuration(300);
    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(relativelayout, "alpha", .1f, .5f);
    fadeIn.setDuration(300);

    final AnimatorSet mAnimationSet = new AnimatorSet();

    mAnimationSet.play(fadeIn).after(fadeOut);

    mAnimationSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mAnimationSet.start();
        }
    });

    mAnimationSet.start();
}

It is currently animating BOTH the button AND the yellow color behind in the relative layout I need to set the button as is without being affected by the realtive layout animation

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Cataster
  • 3,081
  • 5
  • 32
  • 79

2 Answers2

4

What you can do is you can make a relative layout as a background like this-

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#FFC107"
        android:alpha="0.1"
        android:layout_marginBottom="30dp">


        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"


            />

    </RelativeLayout>

i have set the alpha to 0.1 initially in relative layout.

now in activity you can write the animation of fadein and fadeout-

ObjectAnimator fadeOut = ObjectAnimator.ofFloat(relativelayout, "alpha", .5f, .1f);
        fadeOut.setDuration(300);
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(relativelayout, "alpha", .1f, .5f);
        fadeIn.setDuration(300);

        mAnimationSet = new AnimatorSet();

        mAnimationSet.play(fadeIn).after(fadeOut);

        mAnimationSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mAnimationSet.start();
            }
        });

        mAnimationSet.start();

EDIT 3:

Update you layout like this copy and paste the code : change the color accordingly.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp">

        <RelativeLayout
            android:id="@+id/relative_layout_id"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_centerInParent="true"
            android:background="#FFC107"
            android:alpha="0.1">


        </RelativeLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#000000"
            android:layout_centerInParent="true"

            />

    </RelativeLayout>
Aman Verma
  • 3,155
  • 7
  • 30
  • 60
  • this is for imageView though...does it work for button? also...its a ripple effect and thats not what i want honestly :/ what i want is an outer glow effect, that constantly fadesin then out fade in fade out...etc – Cataster Aug 16 '17 at 14:44
  • is the button in your xml supposed to be my current button? so do i delete all the other attributes and just leave the two attributes you have? but wouldnt that mean the current background image i have for the button would be gone? – Cataster Aug 17 '17 at 17:23
  • Which background image? can u update the whole XML file in question – Aman Verma Aug 17 '17 at 17:37
  • android:background="@drawable/play_easy_button" – Cataster Aug 18 '17 at 01:58
  • You can use your button just wrap it inside the layout like I did and set the animation to the layout ... As answered.. – Aman Verma Aug 18 '17 at 02:01
  • ok i have figured it out and now its set :D; one more thing...what am i suppose dto put in relativelayout in parameter of the offloat function of object animator? – Cataster Aug 18 '17 at 02:32
  • um, it gives an error. its actually this RelativeLayout but it still gives error :/ – Cataster Aug 18 '17 at 14:11
  • it says "expression expected"; check the original question for a link to error image – Cataster Aug 18 '17 at 18:09
  • see... first you have to define the layout in oncreate like - RelativeLayout yourlayout = (RelativeLayout)findViewById(R.id.yourlayoutIDinXML); then use this in ObjectAnimator fadeOut = ObjectAnimator.ofFloat(yourlayout, "alpha", .5f, .1f); fadeOut.setDuration(300); ObjectAnimator fadeIn = ObjectAnimator.ofFloat(yourlayout, "alpha", .1f, .5f); fadeIn.setDuration(300); – Aman Verma Aug 18 '17 at 18:14
  • ok so i had to still create relative_layout_id in the xml in the relative layout manually, but yes!!! this is awesome! i am getting the effect!!! the button is flashing back and forth :D...although, how can i make it so that only the yellow tint flashes but the button stays still as is? – Cataster Aug 18 '17 at 18:51
  • Did u set the animation to the button or the relativelayout...if u set to the button the button moves and if u set to the layout then layout moves and button stays still – Aman Verma Aug 18 '17 at 18:56
  • nope its set to just the relative layout...since its only applying the animation to relative layout id in the code you gave me...also, the xml relative id that its getting is set to the relative lyout not the button: – Cataster Aug 18 '17 at 19:01
  • But You can adjust the animation and its value as per your requirement – Aman Verma Aug 18 '17 at 19:05
  • plz see the question now, i added the new updated code – Cataster Aug 18 '17 at 19:09
  • I thought that could be helpful for constant glowing. Take a look at this Button I designed. The shadow looks more realisitic: https://stackoverflow.com/questions/68583069/how-to-put-shadow-with-gradient – Ole Pannier Jul 30 '21 at 11:33
1

Step 1: Create a "glowing" drawable to put behind your button and put them both in a Framelayout. You'll need to put some padding around your button to be able to see the drawable behind it.

Step 2: Create an animation resource file like this and adjust the numbers to your likings.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fillAfter="true">

    <scale
        android:fromXScale="1"
        android:toXScale="0"
        android:fromYScale="1"
        android:toYScale="0"
        android:duration="400"
        android:pivotX="50%"
        android:pivotY="50%"
        />

    <scale
        android:startOffset="700"
        android:fromXScale="0"
        android:toXScale="1"
        android:fromYScale="0"
        android:toYScale="1"
        android:duration="400"
        android:pivotX="50%"
        android:pivotY="50%"
        />
</set>
Lincoln White
  • 639
  • 2
  • 12
  • 29
  • hmm...can you emphasize more on how i would "put behind your button and put them both in a Framelayout. " in your step1? and what kinda glowing drawable? like basically the image link i provided? – Cataster Aug 16 '17 at 18:21