Search code examples
androidtextviewimagebuttonscreen-resolutiondpi

Move TextView together with pressed ImageButton


I have an ImageButton like the one in the image below, where the right button is the pressed state of the button to the left. On the button I have a TextView that I want to move along with the button when pressed by the user. The ImageButton and the TextView is in a FrameLayout.

I managed to solve this by moving the TextView when the ImageButton listener registered a button press, but on different screen size/DPI, it will move either too far or too short. What is the best way to calculate the distance to move the TextView?

enter image description here


Solution

  • Here is what i think can work.
    Put the button on the right and the edit text required in a Relative Layout. Make them a single unit.
    Inside the frame layout, put this relative layout and also another layout below(for the dark shade of the button when not pressed) and a transparent one above it(of the same dimension as the dark shade and visibility gone by default).
    When the button is pressed, set the visibility of the dark-shade layout as gone and transparent one as visible and vice-versa when released.

    The idea is to put the ImageButton and the text view in a single unit and manipulate position of the unit, instead of finding the offset of the text view ;-)