Friday, February 22, 2019

Drag and drop view in android example

We want a view float on phone screen can be drag to any where.
Create a service, declare in to Manifest.xml.

<service
            android:name=".myservice"
            android:enabled="true"
            android:exported="false" />

On top of service, declare variables.
TextView mView;
private WindowManager wm;
int vimoix = 0;
int vimoiy = 0;
WindowManager.LayoutParams myParams;
SharedPreferences prefs;
Bên trong onCreare(), copy vào các dòng sau.
mView = new TextView(this);
mView.setText("This is example text!");
          mView.setTextColor(Color.parseColor("#ffffff"));
          mView.setGravity(Gravity.CENTER);
          mView.setTextSize((float) 13.7);
          mView.setTypeface(null, Typeface.BOLD);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
          int vn = prefs.getInt("vi", 0);
          int vn2 = prefs.getInt("vi2", 0);
myParams = new WindowManager.LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                 ro, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                
                 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                         | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                       
                 /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
                 PixelFormat.RGBA_8888);
          myParams.gravity = Gravity.TOP | Gravity.CENTER_VERTICAL;
          myParams.x = vn;
          myParams.y = vn2;
myParams.setTitle("Load Average");
wm.addView(mView, myParams);
try {
     mView.setOnTouchListener(new View.OnTouchListener() {
     WindowManager.LayoutParams paramsT = myParams;
     private int initialX;
     private int initialY;
     private float initialTouchX;
     private float initialTouchY;
     private long touchStartTime = 0;
     int dem = 0;
     long duration;
     static final int MAX_DURATION = 500;

     @Override
     public boolean onTouch(View v, MotionEvent event) {
                         
     switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
     touchStartTime = System.currentTimeMillis();
     dem = dem + 1;
     initialX = myParams.x;
     initialY = myParams.y;
     initialTouchX = event.getRawX();
     initialTouchY = event.getRawY();
                                
     break;
case MotionEvent.ACTION_UP:
long time = System.currentTimeMillis() - touchStartTime;
duration = duration + time;
if (dem == 2) {
if (duration <= MAX_DURATION) {
if (event.getX() < mView.getWidth()&event.getY() > 0) {
Intent dialogIntent = new Intent(getApplicationContext(),
MainActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                         startActivity(dialogIntent);
}
}
dem = 0;
duration = 0;
break;
}

case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
wm.updateViewLayout(v, myParams);
vimoix = myParams.x;
vimoiy = myParams.y;

final SharedPreferences.Editor edit = prefs.edit();
          edit.putInt("vi", vimoix);
     edit.putInt("vi2", vimoiy);
     edit.commit();
     break;
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}

Add this to destroy.
@Override
     public void onDestroy() {
          super.onDestroy();
         
          if (mView != null) {
               ((WindowManager) getSystemService(WINDOW_SERVICE))
                          .removeView(mView);
               mView = null;
          }
          SharedPreferences prefs = PreferenceManager
                     .getDefaultSharedPreferences(this);
          final SharedPreferences.Editor edit = prefs.edit();
          edit.putInt("vi", vimoix);
          edit.putInt("vi2", vimoiy);
          edit.commit();
     }
Our view now can drag to any where on screen. If it stop, we get new position and use
SharedPreferences to save,next time reopen it stay in dragged position. In ACTION_UP we count if has 2 touch will open new class, here is MainActivity. You can change to one time.

If want view can show on lock screen, change the fifth line LayoutParams.WRAP_CONTENT in myParams = new WindowManager.LayoutParams to LayoutParams.TYPE_SYSTEM_ERROR.