Skip to content

Commit

Permalink
set gravity for layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamed committed Aug 5, 2019
1 parent 83b6584 commit aad6211
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "com.hamedtaherpour.floatinglayout"
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.hamedtaherpour.floatinglayout;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Expand All @@ -16,8 +21,15 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

floatingLayout = new FloatingLayout(this, R.layout.floating_layout, this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
// ask for setting
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 25);
}
}

floatingLayout = new FloatingLayout(this, R.layout.floating_layout, new FLGravity(Gravity.LEFT | Gravity.CENTER, 50, 0), this);
button = findViewById(R.id.btn_run);
button.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

<Button
android:id="@+id/btn_run"
android:layout_width="wrap_content"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="@color/colorAccent"
android:padding="10dp"
android:text="@string/app_name"
android:text="Show"
android:textColor="@android:color/white"
android:textSize="25sp" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hamedtaherpour.floatinglayout;

import java.io.Serializable;

public class FLGravity implements Serializable {

private int gravity;
private int x;
private int y;

public FLGravity(int gravity, int x, int y) {
this.gravity = gravity;
this.x = x;
this.y = y;
}

public void setGravity(int gravity) {
this.gravity = gravity;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public int getGravity() {
return gravity;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import android.support.annotation.LayoutRes;
import android.view.View;

import java.io.Serializable;


public class FloatingLayout implements FloatingLayoutPresenter.IView {

public interface CallBack extends Serializable {
public interface CallBack {
void onClickListener(int resourceId);

void onCreateListener(View view);
Expand All @@ -23,8 +21,12 @@ public interface CallBack extends Serializable {

public FloatingLayout(Context mContext, @LayoutRes int resource, CallBack callBack) {
this.callBack = callBack;
presenter = new FloatingLayoutPresenter(mContext, callBack, resource, this, null);
}

presenter = new FloatingLayoutPresenter(mContext, callBack, resource, this);
public FloatingLayout(Context mContext, @LayoutRes int resource, FLGravity flGravity, CallBack callBack) {
this.callBack = callBack;
presenter = new FloatingLayoutPresenter(mContext, callBack, resource, this, flGravity);
}

public void create() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ public class FloatingLayoutPresenter {
public @LayoutRes
int resource;
private IView mView;
private FLGravity flGravity;

public FloatingLayoutPresenter(Context mContext, FloatingLayout.CallBack callBack, int resource, IView mView) {
public FloatingLayoutPresenter(Context mContext, FloatingLayout.CallBack callBack, int resource, IView mView,FLGravity flGravity) {
this.mContext = checkNull("Context can not be null", mContext);
this.callBack = callBack;
this.resource = resource;
this.mView = mView;
this.flGravity = flGravity;
}

public void onCreate() {
Intent intent = new Intent(mContext, FloatingLayoutService.class);
intent.putExtra(FloatingLayoutService.LAYOUT_RESOURCE, resource);
if (callBack != null)
intent.putExtra(FloatingLayoutService.RECEIVER, new DownloadReceiver(new Handler()));
if (flGravity != null)
intent.putExtra(FloatingLayoutService.FL_GRAVITY, flGravity);

mContext.startService(intent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class FloatingLayoutService extends Service implements View.OnClickListen

public static final String LAYOUT_RESOURCE = "layout-resource";
public static final String RECEIVER = "receiver";
public static final String FL_GRAVITY = "flgravity";
public static final int ACTION_ON_CLICK = 3265;
public static final int ACTION_ON_CREATE = 5874;
public static final int ACTION_ON_CLOSE = 3625;
Expand All @@ -40,6 +41,7 @@ public class FloatingLayoutService extends Service implements View.OnClickListen
int resource;
private @IdRes
int ROOT_CONTAINER_ID;
private FLGravity flGravity;

@Override
public IBinder onBind(Intent intent) {
Expand All @@ -60,6 +62,7 @@ public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
resource = intent.getIntExtra(LAYOUT_RESOURCE, 0);
receiver = (ResultReceiver) intent.getParcelableExtra(RECEIVER);
flGravity = (FLGravity) intent.getSerializableExtra(FL_GRAVITY);

onDestroyView();
createView();
Expand Down Expand Up @@ -98,17 +101,11 @@ private void createView() {
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mFloatingView = layoutInflater.inflate(resource, null);

params.gravity = Gravity.CENTER | Gravity.CENTER;
params.x = 0;
params.y = 0;
if (flGravity != null)
setGravity(params);
mWindowManager.addView(mFloatingView, params);

rootContainer = mFloatingView.findViewById(ROOT_CONTAINER_ID);
Expand Down Expand Up @@ -150,6 +147,12 @@ public boolean onTouch(View v, MotionEvent event) {
receiver.send(ACTION_ON_CREATE, null);
}

private void setGravity(WindowManager.LayoutParams params) {
params.gravity = flGravity.getGravity();
params.x = flGravity.getX();
params.y = flGravity.getY();
}

private void setOnClickToView(View view) {
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
Expand Down

0 comments on commit aad6211

Please sign in to comment.