Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How set Padding when an button is touched but the pointer is out of my button?

Posted on November 14, 2022

Solution 1 :

Look at the below code.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns_android="http://schemas.android.com/apk/res/android">
    <item android_drawable="@drawable/button_pressed"
          android_state_pressed="true" />
    <item android_drawable="@drawable/button_focused"
          android_state_focused="true" />
    <item android_drawable="@drawable/button_default" />
</selector>

the attribute state_pressed or the state_focused should do the job.
see the following link for more guidance

Solution 2 :

UPDATE

I can do it with this code:

private Rect rect;    // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    }
    if(event.getAction() == MotionEvent.ACTION_MOVE){
        if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
            // User moved outside bounds
        }
    }
    return false;
}

Problem :

I use a XML file to display a button with 2 states (normal, pressed). How can I
find out when the button is touched but the cursor is out?

I want button effect like this

button not touched

button touched

button touched but finger hasn’t been lifted

Here is the XML file I use:

<selector xmlns_android="http://schemas.android.com/apk/res/android">
    <item android_state_pressed="true" android_drawable="@drawable/button_press" />
    <item android_drawable="@drawable/button_nopress" />
</selector>

And here is my Java class:

button = findViewById(R.id.b1);
button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    button.setPadding(0, 55, 0, 0);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    button.setPadding(0, 0, 0, 0);
                }
                return false;
            }
        });            

And here is my button with my code:

button touched but finger hasn’t been lifted

Comments

Comment posted by Lucas

It’s not working.

READ  [ANSWERED] android - App crashes when there is no internet even though I have added conditions to it
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress