[Android] Activity를 팝업으로 띄우기

Study/Android Studio 2021. 2. 17. 16:43 Posted by meanoflife
반응형

Activity를 팝업으로 띄우기

 

INDEX.

  1. Activity 속성 변경

  2. 타이틀(ActionBar 영역) 제거하기

  3. 팝업 사이즈 크기 조정

  4. 팝업 밖 선택시 닫힘 방지

  5. 결과 리턴(옵션)

 

 

1. Activity 속성 변경

 

Activity를 팝업으로 띄우려면 AndroidManifest.xml파일에서 Activity의 속성을 변경해 주면 됩니다.

1
2
3
4
5
6
7
8
    <application
    
        ...{생략}
    
        <activity
            android:name="com.onemoresg.parallelworld.PopupActivity"
            android:theme="@android:style/Theme.Dialog" />
    </application>
cs

  - android:theme="@android:style/Theme.Dialog"  // 팝업으로 선언

 

 

2. 타이틀(ActionBar 영역) 제거하기

 

저의 경우, 자동으로 생성되는 ActionBar영역을 사용하지 않기 위해 Activity의 속성을 다음과 같이 사용하곤 합니다.

1
2
3
        <activity
            android:name="com.onemoresg.parallelworld.PopupActivity"
            android:theme="@style/Theme.Design.NoActionBar" />
cs

[소스] AndroidManifest.xml

 

 

  - android:theme="@style/Theme.Design.NoActionBar"  // ActionBar 미사용

 

그런데, theme를 두번 사용할 수가 없습니다.

이를 처리하기 위해 .java 소스에서 ActionBar의 영역을 제거합니다.

1
2
3
4
5
6
7
8
9
public class PopupActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature( Window.FEATURE_NO_TITLE );
        setContentView(R.layout.activity_main);
    }
}
cs

 

  - requestWindowFeature( Window.FEATURE_NO_TITLE );  // 타이틀 미사용

    ※ 주의!! setContentView(R.layout.activity_chr_create); 보다 먼저 선언해야 합니다.

 

3. 팝업 사이즈 크기 조정

 

Activity를 생성하면, 기본적으로 width와 height가 match_parent로 선언되어 있습니다.

width와 height를 조정하여 팝업사이즈의 크기를 조정합니다.

1
2
3
4
5
6
7
8
9
10
<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="300dp"
    android:layout_height="200dp"
 
    android:background="#CCCCCC"
    tools:context=".PopupActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
cs

 

 

4. 팝업 밖 선택시 닫힘 방지

 

AndroidManifest.xml에서 Activity의 속성만 변경하면 팝업으로 호출이 됩니다.

그런데, 호출된 팝업 영역 밖을 클릭하면, 자동으로 팝업이 닫히게 됩니다.

이를 방지하기 위해 .java 소스에 onTouchEvent를 Override합니다.

1
2
3
4
5
6
7
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if( event.getAction() == MotionEvent.ACTION_OUTSIDE ) {
            return false;
        }
        return true;
    }
cs

 

 

5. 결과 리턴

 

Activity간의 데이터 전달은 Intent객체를 이용하면 됩니다.

그런데, startActivityForResult(~)로 호출한 팝업은 결과를 리턴할 필요가 있습니다.

이 때, 결과를 리턴하는 방법입니다.

1
2
3
4
5
6
7
8
9
10
11
        // [예] 버튼
        Button btnYes = findViewById( R.id.btnAlertYes );
        btnYes.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra( "EXIT"true );
                setResult( RESULT_OK, intent );
                finish();
            }
        });
cs

 

- setResult()를 이용하여 결과를 리턴합니다.

 

The End.

반응형