Hello friends, in today’s article, if you want to make any subject Movable/Draggable in android-studio, then you can stay with this article.
Many times it happens that we implement a subject in our Android application, then it is very important for us to track it, otherwise, that subject covers any part of us, which causes us a lot of trouble, such as floating.
What happens is that the action bar is rotated or opened and the application is covered from somewhere, this causes a lot of trouble to the user, so today I am going to show you all how to make the floating action outside Movable / Draggable. I have told about it in this article
How to Create Movable/Draggable any Subject in android studio
I am going to take floating action bar in this case, I am going to show you how to make floating action bar Movable/Draggable in this project!
try this code
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
float dX;
float dY;
int lastAction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View dragView = findViewById(R.id.draggable_view);
dragView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
break;
default:
return false;
}
return true;
}
}
and try this XML Code
<ImageButton
android:id="@+id/draggable_view"
android:background="@mipmap/ic_launcher"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The first given code has to be implemented inside the Java file and you have to implement the second file inside the XML, after that your subject will be draggable.
- How to Force an App Update Using the Google Play In-App Update API: A Step-by-Step Guide
- Android Studio – Movable/Draggable Floating Action Button
- How to Add Rate me Dialog in Android App | Get Better Ranking No. 1
- How to Create a Shayari App in Android Studio | Free Source Code
- How to Implement Unity Ads in Android Studio Projects Banner or Interstitial Ads