Dynamically change width of a view with animation - java

I have a case where have to dynamically change width of a button placed alongside another button in a horizontal linear layout. Button width change depend upon certain condition.
I have already tried many of solutions posted at stackoverflow, but the animation is never smooth. Lags on low-end devices. Some time the text within button seems to be sliding in which I don't want.
Here is what I want to achieve, pls see here how smoothly button width changes in google messages just want to achieve same animation.
Just for reference i've already top voted solutions here, for example Resizing layouts programmatically (as animation)

Related

clickable areas over an image in android studio (java)

I've inherited an Android Studio project with a challenging goal. Basically, the idea is that the app displays an image which is the map of a building (museum). When a user clicks on a room, the app displays the name of that room, and then they have some other options to do things. This works fine as long as we design the app for use one a single device and don't ever plan to use it on anything else, which of course is silly. I have been able to get the map image to be responsive to different size screens just fine. The problem I have is with the buttons, which were drawn over portions of the map during design.
I have used ConstraintLayout, but it isn't doing what I need. For one, the buttons don't really move the way I would expect. For example, when I run the app on a tablet, the image resizes fine, but the buttons are all clustered in one quadrant; they didn't really move with the image resize the way I expected.
The second issue is the button size. Even if the buttons did move when on a larger screen (let's just assume they did), the buttons themselves don't resize. I have tried WRAP_CONTENT, but then it sizes it larger than I want it even in design. Setting it to 0dp just makes a 1 pixel button (not sure why). Is there some way to get the button size to grow in proportion to the image growth when responding to different screen sizes? In other words, in the layout design, I want to be able to specify the button dimensions so that they cover the exact portion of the map image I want, but then when the image grows, I want the button to grow a proportional amount, so that it mostly stays lined up with the room to which it corresponds.
I have read about using an overlay image with colored areas as a workaround, but this won't be practical in some room images where there will be in excess of 20 clickable areas.
I hope that explanation is clear enough. Thanks in advance for any help.
The buttons (there are many of them) are defined in the layout like this:
<Button
android:id="#+id/LateGothicHallBtn"
android:layout_width="46dp"
android:layout_height="102dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="221dp"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="#+id/FirstFloorMap"
app:layout_constraintEnd_toEndOf="#+id/FirstFloorMap" />
And then there is a listener for when the button is clicked:
Button LateGothicHallBtn = findViewById(R.id.LateGothicHallBtn);
LateGothicHallBtn.setOnClickListener(this);
If you need it the ImageView code is this:
<ImageView
android:id="#+id/FirstFloorMap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:contentDescription="#string/first_floor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/gui2" />
Is that what you need?
In your position, I would advise you to look at libraries that have the functionality you need, for example, AndroidImageMap. If necessary, you can download them, insert them into your project as a module and change the code. Also check these questions, I think it will make it clearer: clickable area of image, How to make certain areas in an image clickable in android
That's not that hard to achieve, you need to override onMeasure in the parent ViewGroup that has the image in the background, and knowing its original size you get the scale to the new dimension you wanna fit then you multiply all coordinates by it: the background image and all buttons l,t,r,b in a similar way AbsoluteLayout works
I myself also prefer to override onDraw and onTouchEvent and draw/click the buttons in the same component, but you can make it work with the android.Button
I can provide more help if you add your code

Draggable overlay that can go offscreen?

I have created an overlay menu that is a rectangle.
It displays on top of everything else while active. All other apps, the home screen, etc.
If you click anywhere outside of this rectangle your click hits
whatever is visible.
If you click the red part it allows you to drag the menu around the
screen. Clicking the green part performs some function.
I have created this rectangle from inflating layout xml. The rectangle itself is a relativeLayout and the green and red parts are both imageView children.
Since I'm unable to move the relativeView off screen and since the background needs to be clickable my only solution was to calculate margins and apply them to the relative view to imitate the desired effect.
However, the code to calculate these margins is almost 50 lines and fairly complex and hard to understand at a glance if someone else needs to refactor. I can post the code but I don't believe it's relevant to the actual problem's context.
Is there a much simpler way to do this that I'm missing? Is my approach wrong?
I hope this simple project may help you Android Floating Widget

View's Properties not changing with Property Animation

I'm trying to animate a floating action button to a new location and have it be clickable there, but when I animate it via
listOptionsButton.animate().xBy(-200);
I can't click it in the new location that it goes to.
I thought that using this kind of animation would move the view itself and not just the rendered pixels.
Can someone explain what's happening and help me find a way to move my button and be able to click it at the start and end locations?
I fixed my issue. It turns out that I animated the button outside of the parent layout and even though I used property animation, the button wasn't reachable because it was outside of the bounds of the parent.
I don't know how to elaborate further, because I honestly don't fully understand why this was an issue, but I ended up making a compound view that took up my entire screen so it wasn't an issue in the end.

Eclipse android widget moving

I am designing my activity screen using eclipse but whenever I move an object e.g. a button it snaps around and I find it hard to position things where i want them.
Is there some setting I can change as as soon as I add a second button it snaps the other button to another position it is so annoying!
Also if I have an image button why can't i resize this, even with a normal button if i try to make it larger it just fits to the text and wont make the button bigger or it just flicks to a random place on the activity?
thanks
Probably you're trying to place components freely in the View but you're not considering the LayoutManager behavior. If you try to position your component in a LinearLayout, for example, the components will be placed following its rules, and these components will not stay where you drop them.
Try to understand better how the layouts works on Android. But for now, the AbsoluteLayout or RelativeLayout may be what you're looking for.
About the components size, you'll need to understand better how to use layout properties for these components. See the question How to size buttons for more information.

Is it possible to manually scroll a gallery based on a touch event captured elsewhere on the screen

In my Android app I have a gallery which contains some TextViews. I realized that some users don't understand that they can scroll that gallery although the previous and next TextViews are partially visible. So I put an ImageView with a horizontal arrow under the gallery.
Now the users try to drag the arrow. Is it possible to redirect the touch event from the ImageView to the gallery so that dragging it would scroll the gallery?
Thanks in advance,
Fri
The simplest thing (if you don't mind scrolling one item at a time) is to detect the direction of the touch event on the arrow and then dispatchSetSelected(boolean selected) on the gallery... I believe that will work. after the user does that once, they will probably intuit that they can then just drag the gallery. Of course if you want to measure the touch event speed and do the calculation for how far to scroll based on that, you can do that too... but that seems like a lot of unnecessary trouble.

Categories