728x90
안녕하세요. 오늘은 floating layout 를 사용해보도록 하겠습니다.
floating layout 라이브러리를 통해서, 다른 앱 위에서도 창을 띄워볼수 있습니다.
gradle 설정
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'com.github.HamedTaherpour:floating-layout-android:1.1.5'
}
Mainfest 설정
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application>
<service android:name="io.hamed.floatinglayout.service.FloatingService"/>
</application>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import io.hamed.floatinglayout.callback.FloatingListener;
import io.hamed.floatinglayout.FloatingLayout;
public class MainActivity extends AppCompatActivity {
private FloatingLayout floatingLayout; // floating layout 선언
private FloatingListener floatingListener = new FloatingListener() {
@Override
public void onCreateListener(View view) {
Button btn = view.findViewById(R.id.btn_close);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
floatingLayout.destroy();
}
});
} // floating layout 내부 레이아웃 설정
@Override
public void onCloseListener() {
Toast.makeText(getApplicationContext(), "Close", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_open:
if (!isNeedPermission())
showFloating();
break;
case R.id.btn_permission:
if (isNeedPermission())
requestPermission();
break;
}
} // 버튼 클릭 이벤트
private boolean isNeedPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this);
} // 다른 앱 위에서 작동이 가능하게 허용
private void requestPermission() {
Intent intent = new Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName())
);
startActivityForResult(intent, 25);
} // overlay permission 요청
private void showFloating() {
floatingLayout = new FloatingLayout(this, R.layout.sample_layout);
floatingLayout.setFloatingListener(floatingListener);
floatingLayout.create();
} // floating layout 실행
}
다른 앱 위에서 작동되기 위해서 권한 설정이 우선적으로 필요하다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/app_name"
android:textColor="@color/black"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_logo" />
<Button
android:id="@+id/btn_open"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:onClick="onClick"
android:text="Open"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title" />
<Button
android:id="@+id/btn_permission"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Check For Permission"
android:textSize="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="@color/purple_700"
app:layout_constraintTop_toBottomOf="@id/btn_open" />
</androidx.constraintlayout.widget.ConstraintLayout>
sample_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/root_container"
android:layout_width="280dp"
android:layout_height="290dp"
android:layout_gravity="center"
android:background="@color/purple_200">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center|top"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<Button
android:id="@+id/btn_close"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:background="@color/purple_200"
android:text="close"
android:textColor="@color/black" />
</FrameLayout>
</FrameLayout>
'안드로이드 자바' 카테고리의 다른 글
[Java][Android] 계산기 (0) | 2022.05.05 |
---|---|
[Java][Android] RatingBar 사용해보기 (0) | 2022.04.21 |
[Java][Android] GradientView를 통해 그라데이션 색상 구현 사용해보기 (0) | 2022.04.17 |
[Java][Android] Toast message 커스텀 하기 (0) | 2022.04.15 |
[Java][Android] 별점 만들기 (CustomView 만들기) (0) | 2022.04.05 |