I am attempting move a Button around in an Android app I'm making (Java). Under certain circumstances in onResume(), I get margin information about the button, then attempt to place the button at the left edge of the screen with that margin as a slight gap so it looks nice. See below.
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) button.getLayoutParams(); button.setX(lp.leftMargin);
However, apparently the information in my xml layout file gets read AFTER I programmatically set the button position. So, my button is getting placed exactly in the middle of the screen (as stated in xml file) PLUS 8 dp to the right. I need a way to reorder these positioning commands -- Anyone know how to queue up a dynamic view placement in onResume so it delays just enough to occur immediately after the button is done being placed by my layout file?
Found it!! I guess it was a duplicate question, I couldn't find it searching before:
https://stackoverflow.com/a/24035591/14116101
Related
I want to apply the same ripple drawable properly on multiple buttons. But it's not happening because, the ripple effect is only getting applied on the last button. I have also used gradient drawable inside the ripple drawable. That works fine on every button. It just the ripple effect is not getting applied on any button except the last button. It is also not working on api level 8. I know that this method doesn't work on apis before api level 21. But I haven't found any tutorial on dynamic button design.
I'm doing everything inside the on-create event. I can't write the codes outside of the on-create event and also can't use any XML for this particular situation I'm in.
Here's the output:
Here's the code:
android.graphics.drawable.GradientDrawable gd_btone = new android.graphics.drawable.GradientDrawable();
gd_btone.setCornerRadius(4);
gd_btone.setStroke(2, Color.parseColor("#ffffff"));
gd_btone.setColor(Color.parseColor("#232323"));
android.graphics.drawable.RippleDrawable ripdr = new android.graphics.drawable.RippleDrawable(new android.content.res.ColorStateList(new int[][]{new int[]{}}, new int[]{ Color.parseColor("#888888")}), gd_btone, null);
button1.setBackground(ripdr);
button2.setBackground(ripdr);
button3.setBackground(ripdr);
button4.setBackground(ripdr);
Also, can anyone help me to make this piece of code back-compatible with api level 8 and how to set margin on this drawable?
I'm not a Android developer so I don't know much about it.
What I see is that the dripple effect is applied to the last button, even when the other buttons are clicked. Because of this, I am thinking that the RippleDrawable class does not support the kind of behavior you are looking for; a single instance can only be bound to one button at a time.
I would suggest simply creating four separate RippleDrawable instances.
Disclaimer: I have not worked with RippleDrawables, but this is purely based on the output you've provided and personal experience with Android development.
I am trying to show a fragment with right to left animation, as though it is coming from the right side of the screen but my code does exactly the opposite.
flBackground.animate().setDuration(250).x(0).translationX(0).alpha(1).start();
flBackground here is the id of the root view in my fragment.
I played with 'translationX' method. I gave the screen width in pixels, but my fragment got off screen. Is there a way in ViewPropertyAnimation that I can set initial positioning of a view to animate from?
I only want to use ViewPropertyAnimation, not other Animation objects. Other ones are quite big and I want simple code for my project.
You can use FragmentTransaction.setCustomAnimations(). It's the best way.
Example:
getFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.slide_right, R.animator.slide_left)
.add(R.id.list_fragment_container, YourFragment.instance())
.commit()
More: https://developer.android.com/reference/android/app/FragmentTransaction#setCustomAnimations(int,%20int,%20int,%20int)
So I have a question about best practice for dynamically creating and sizing buttons within a ViewPager, based on a changing external state.
Essentially, I have a scrolling view and I want my ViewPager to contain a number of buttons of a specific size and count depending on what part of the scrolling view is currently visible.
My question is about deciding the best implementation of this feature, I see two options: would it be simpler to
Constantly create and scale new buttons whenever the scrolling view moves
Make the viewpager itself contain a scrollview and fill it with all of the pre-scaled buttons on app startup. Then, whenever the user scrolls the main scrollview the viewpager's scrollview (which contains the buttons) will scale programatically
Any ideas on which would be the simpler and more robust system?
Not much of an answer but I will leave a comment for ya! Basically you can do it either way, both aren't to difficult to accomplish, however I would probably go the dynamic route because it will always scale correctly. Doing a set amount will only work until devices become larger, or if you are targeting tablets or tvs then it will start to become extremely messy in the xml file for the layout. Dynamically adding also gives you far more control and saves time later on, you can simply change a number and have 100 more then going through and manually adding even 10. Hope this helps!
I need to implement TextView widget in cocos2d-android-1 and don't know how please help
TextView textView = null;
textView.setText(R.string.billing_not_supported_message);
addChild(textView);
This code not working because addChild needs node. Please help really need.
I completely agree that the android version of cocos2d needs some serious 'standard UI' features adding to it.
At the moment, your only real option is to take a similar approach to my previous answer here
ANDROID:How to open web page in class extends CCLayer
whereby you have a layout which will put a textview on the screen (or you construct it yourself in your handler), and you use a handler from the activity which your scene is running in to show/hide it.
It's clunky and horrible but it works. In my field designer app i faced the same problem, but i also had to have a custom background to the text field, which resized with the text field, that had a rough edge, and the text view had to fall inside that rough edge so all the text was visible on the main bit of the background.
i achieve that using this same technique, but i created a layout xml file so that i had control over how the textview and it's background were displayed.
(note to show/hide the textview i had to give its root layout a constant ID and check for if that ID existed, and was visible, as when people touched outside it, i needed to make it vanish)
I want to programatically scroll a View. So far, I have found two possibilities :
scrollTo(x,y) : the top left hand corner of the View corresponds to the point (x,y) of what must be drawn in the View. The problem is that, after the call, it is impossible to manually scroll the View to have it displays what is above y or what is at the left of x.
setScrollY(y) : seems to be the function I'm looking for. But it is only available for API above level 14. And my application is supposed to work with API level 8.
Is there another function which could do what I want to do?
Thanks in advance for the time you will spend trying to help me.
Is your View in a ScrollView? If not, you will need to handle events yourself to manually scroll the View after you use code to set the scroll location. It sounds like you want both manual and programmatic control, so you'll need to use a scrolling container or handle touch events yourself.