본문 바로가기

ANDROID - JAVA

[ANDROID - JAVA] 원하는 크기의 팝업창, 다이얼로그 만들기

앱 서비스를 제작하다 보면 팝업창, 다이얼로그를 만들어야하는 경우가 많다!


오늘은 안드로이드 앱 내에서 원하는 크기의 팝업창, 다이얼로그를 만들기!


다양한 방법이 많지만 내가 자주 사용하는 방법은 아래와 같다.



1. manifest를 다음과 같이 수정한다.               


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com......">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Home.MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PopActivity"
            android:theme="@style/NoTitleBarDialog" />
    </application>
 
</manifest>

cs


분명히 (줄22, 23) 빨간줄이 그러질거에요 당황하지말고 아래 따라하기



2. PopActivity를 생성해준다. 그 이후 다음과 같이 추가해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class PopActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);
 
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 
        int width = (int) (display.getWidth() * 0.9); //Display 사이즈의 90% 각자 원하는 사이즈로 설정하여 사용
 
        int height = (int) (display.getHeight() * 0.9);  //Display 사이즈의 90% 각자 원하는 사이즈로 설정하여 사용
 
        getWindow().getAttributes().width = width;
 
        getWindow().getAttributes().height = height;
    }
}
cs



3. 스타일을 추가한다.             


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<resources>
 
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
 
 
    <style name="NoTitleBarDialog" parent="Theme.AppCompat.Light.Dialog">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">#07000000</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>
 
cs


4. layout 은 activity_pop의 이름으로, 내용은 알아서 작성해주세요 ㅎ


완성!


장점:

모든 기기에 디스플레이에 같은 %의 사이즈로 팝업창을 생성할 수 있음