i have an image button and i want to move it randomly,
i have no idea how to do that, i also tried other questions but was not able to understand,
i want my image button to move randomly on screen and it should not move out,it should first be compatible with every device.
I also tried it by making buttons in a gridlayout and then individually making them visible and invisible for 1000ms,but it is not the efficient way...
any other way to do so?
public void display(int x){
String q=score.toString();
s.setText(q);
switch (x) {
case 1: {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.setVisibility(View.VISIBLE);
b1.setEnabled(true);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score++;
}
});
}
}, 1000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
disable();
int x=random();
display(x);
}
}, 2000);
}
Instead of creating multiple buttons, you can use 1 and change its position using setX and setY on click.
For example;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.setX( Enter random number within your screen boundary );
b1.setY( Enter random number within your screen boundary );
}
}, 2000);
You'll need to calculate the boundaries of the screen and take into account that setX and setY position applies to the top left of the button's view.
For API lower than 8 you'll need to use LayoutParams as shown below, or you can also refer codes here (How can I dynamically set the position of view in Android?)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);
Related
Here I'm trying to scroll a GridView automatically top to bottom and as it comes to last item scroll it back to top from bottom using smoothScrollToPosition but its very fast. The code is :
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", homeGridView.getCount()-1).setDuration(1000).start();
homeGridView.smoothScrollToPosition(homeGridView.getCount() - 1);
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", 0).setDuration(1000).start();
homeGridView.smoothScrollToPosition(0);
}
}, 1000);
}
}, 1000);
Now I just want to know that how can I decrease speed of this scroll or something better this.
I have a TextView inside a LinearLayout. My goal is to animate the TextView height change when its text changes and need more or less lines.
I split the work this way :
Fade out the old text
Animate the TextView new height (and the parent LinearLayout)
Fade in the new text
The fade in / fade out part is easy, but I struggle for the height change animation.
Here's my simplified layout :
<LinearLayout
android:id="#+id/fragment_tooltip_tooltip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/tooltip_blue_bg"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_tooltip_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
And here is what I tried :
final int currentHeight = tvMessage.getHeight();
tvMessage.setText(toTooltip.getMessage());
tvMessage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TooltipFragment.this.tvMessage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int newHeight = tvMessage.getHeight();
ResizeAnimation resizeAnimation = new ResizeAnimation(tvMessage, tvMessage.getWidth(), tvMessage.getWidth(),
currentHeight, newHeight);
resizeAnimation.setDuration(ANIM_DURATION_TRANSITION_TEXT_RESIZE);
resizeAnimation.setInterpolator(new FastOutSlowInInterpolator());
resizeAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// No-op
}
#Override
public void onAnimationEnd(Animation animation) {
// Finally we show the new content
...
}
#Override
public void onAnimationRepeat(Animation animation) {
// No-op
}
});
tvMessage.startAnimation(resizeAnimation);
}
});
The problem with this solution is that the tvMessage.setText(toTooltip.getMessage()); line used to measure the new height expand immediately the TextView and LinearLayout, then the animation resize the view to its previous size before applying the resize the animation, producing an ugly visual effect.
I believe a TextSwitcher will not animate the height change and so is not a solution, but I didn't tried it.
The solution for my problem was to use a dummy TextView and measure it with the new text instead of using the actual TextView. This way my TextView is not resized before the animation, and I just have to set the text at the end of the animation.
See this answer for details : Getting height of text view before rendering to layout
You could use a handler that runs recursively for this, looks a bit cleaner too. So, for example, use this:
Call the function startTextAnimation()
Have a Handler animHandler = new Handler(); in your onCreate()
Then use this function:
public void startTextAnimation() {
textView.setHeight(textView.getHeight() + 1);
if (textView.getHeight < *whatheightyouwant*) {
aninHandler.postDelayed(new Runnable() {
#Override
public void run() {
startTextAnimation();
}
}, 200);
}
}
You can set parameters accordingly, it should do the job. Hope it helps.
I'm dynamically creating buttons and place them randomly on the screen. I want to remove a button when it's being clicked and create another one, but when I do that, all the other buttons (the ones bellow him to be percise) also change their position (moving up 1 row). How can I modify my code so that the other buttons wont change their position when I click a button? I want to add the new button in the same row of the one that was removed.
I'm using a Linear Layout, so when I add a button it adds it to the bottom and fill the blank row by pushing up all the rows.
In the following code I just remove the button after a few seconds:
private void createGreenButton() {
Random r = new Random();
int hideDelay = r.nextInt(1000) + 1000;
final Button greenButton = new Button(this);
greenButton.setText("button");
greenButton.setOnClickListener(greenButtonClick(greenButton));
layout.addView(greenButton, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
// Place the buttons randomly on the screen
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#SuppressLint("NewApi")
#Override
public void onGlobalLayout() {
Random r = new Random();
layout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
LinearLayout.LayoutParams layoutParams = (LayoutParams) greenButton
.getLayoutParams();
int horizontalMargin = r.nextInt(SCREEN_WIDTH
- greenButton.getWidth());
layoutParams.setMargins(horizontalMargin, 50, 0, 0);
greenButton.setBackgroundColor(Color.GREEN);
greenButton.setLayoutParams(layoutParams);
}
});
layout.requestLayout();
mHandler.postDelayed(new Runnable() {
public void run() {
((ViewManager) greenButton.getParent()).removeView(greenButton);
createGreenButton();
}
}, hideDelay);
}
I have home activity on which I have 3 buttons : Move To A , Move To B and Move To C.
On click of these buttons, I navigate to inner activity where I have 3 layouts within a HorizontalScrollView, say A, B and C which in turn consists of several views.
Now what I want is , inner activity should navigate to one of the 3 layouts based on clicked button from Home activity i.e.
If Move To A on home activity is pressed, inner activity should navigate to Layout A and so on...
Possible solution might be to use ScrollTo() but it isn't efficient with variable screen sizes.
Any help appreciated.
Edit
This doesn't work:
RelativeLayout LayoutA = (RelativeLayout) findViewById(R.id.LayoutA);
HorizontalScrollView main = (HorizontalScrollView) findViewById(R.id.scrollview);
main.scrollTo(PoCLayout.getLeft(), 0);
Even this doesn't work:
main.scrollTo(500, 0);
If main.scrollTo(500,0) does not work, try :
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(500, 0);
}
}
Edit : the working solution in the listener of the button :
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(layoutX.getLeft(), 0);
}
}
I'm not sure it will work, but you can try to get the position of your layout A, B or C calling the method getLeft() and then use scrollTo().
Example to scroll to layout A : scrollTo(a.getLeft(), 0);
You can do a little Math here
Use View.getLeft() and View.getRight() then divide calculated width per 2 to scroll to the View center
Use :
final HorizontalScrollView scrollView= (HorizontalScrollView)
int speed=1000;
findViewById(R.id.main_scrollView);
final View view = findViewById(R.id.your_view);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ObjectAnimator.ofInt(scrollView, "scrollY", (int) view.getX()).setDuration(speed).start();
}
});
main.post(new Runnable() {
#Override
public void run() {
main.scrollTo(500,500);
}
}
The question is "How do i scroll up a ScrollView to top very smoothly and slowly".
In my special case i need to scroll to top in about 1-2 seconds.
Ive tried interpolating manually using a Handler (calling scrollTo(0, y)) but that didnt work at all.
I've seen this effect on some bookreader-apps yet, so there must be a way, im sure :D.
(Text is very slowly scrolling up to go on reading without touching the screen, doing input).
I did it using object animator (Available in API >= 3) and it looks very good:
Define an ObjectAnimator:
final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(this, "scrollY", 0);
(this refers to the class extending Android's ScrollView)
you can set its duration as you wish:
animScrollToTop.setDuration(2000); (2 seconds)
P.s. Don't forget to start the animation.
In 2 seconds move the scroll view to the possition of 2000
new CountDownTimer(2000, 20) {
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (2000 - millisUntilFinished)); // from zero to 2000
}
public void onFinish() {
}
}.start();
Try the following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
ValueAnimator realSmoothScrollAnimation =
ValueAnimator.ofInt(parentScrollView.getScrollY(), targetScrollY);
realSmoothScrollAnimation.setDuration(500);
realSmoothScrollAnimation.addUpdateListener(new AnimatorUpdateListener()
{
#Override
public void onAnimationUpdate(ValueAnimator animation)
{
int scrollTo = (Integer) animation.getAnimatedValue();
parentScrollView.scrollTo(0, scrollTo);
}
});
realSmoothScrollAnimation.start();
}
else
{
parentScrollView.smoothScrollTo(0, targetScrollY);
}
Have you tried smoothScrollTo(int x, int y)?
You can't set the speed parameter but maybe this function will be ok for you
You could use the Timer and TimerTask class. You could do something like
scrollTimer = new Timer();
scrollerSchedule = new TimerTask(){
#Override
public void run(){
runOnUiThread(SCROLL TO CODE GOES HERE);
}
};
scrollTimer.schedule(scrollerSchedule, 30, 30);