The button should move to the middle of the screen when clicked. But every time I get different position of button in different devices. And I don't know how can I fix it. This code what I use:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
float middleScreen = metrics.xdpi;
final TranslateAnimation animation = new TranslateAnimation(0, -middleScreen, 0, 0);
animation.setDuration(3000);
animation.setFillAfter(true);
buttonToNextView.setAnimation(animation);
First of all, what
DisplayMetrics.xdpigave us is theexact physical pixels per inch of the screen in the X dimensionwhich is not the number of pixels on the x-axis.So, we should use half of
widthPixelsandheightPixels(based on screen orientation) to achieve the middle of the screen on the x-axis.Notice that to place the button exactly at the middle of the screen, you need to subtract half of its width from the
middleScreenvalue.