Android

Android] 액티비티를 팝업 창 모드로 출력하기

김코식 2022. 5. 7. 22:46

액티비트를 팝업 창 모드로 출력하기

 

1. AndroidMainfest에 activity 추가와 테마 설정

<activity android:name=".PopActivity"
    android:theme="@style/Theme.AppCompat.Dialog"/>

2.activity_main.xml

메인 액티비티에 팝업 창 출력을 위한 버튼 생성

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/popupBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="팝업 화면 띄위기"
        android:textSize="30dp"
        />

</LinearLayout>

3. MainActivity.java

package com.example.popup;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button popBtn = (Button) findViewById(R.id.popupBtn);
        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,PopActivity.class);
                startActivity(intent);
            }
        });
    }
}

메인 액티비티의 버튼의 클릭 이벤트 설정

(인텐트를 사용하여 액티비티 변환)

 

4. activity_popup.xml

팝업 창 출력을 위한 액티비티 설정 (택스트, 버튼 이미지 등),

레이아웃 크키 설정을 통해 팝업 창 크기 설정

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/popText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="팝업 테스트"
        android:textSize="30dp" />

    <Button
        android:id="@+id/outBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="확인"
        android:textSize="30dp"
        android:layout_gravity="center"/>
</LinearLayout>

 

5. PopActivity.java

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

위의 코드를 통해 타이틀 바 제거 (setContentView 이전에 선언)

*(기본 액티비티 사용시 - requestWindowFeature(Window.FEATURE_NO_TITLE); 사용)

package com.example.popup;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class PopActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 타이틀 바 제거
        setContentView(R.layout.activity_popup);



        Button outBtn = (Button) findViewById(R.id.outBtn);
        outBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PopActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}

 

6. 동작


- 간단한 계산 후 결과를 팝업 창으로 받아 출력하기

 

1.activity_main.xml

숫자1, 숫자2, 연산자를 입력받는 EditText 와 계산을 위한 Button 생성

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/firEdit"
            android:hint="첫번 째 숫자를 입력하십시오"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/secEdit"
            android:hint="두번 째 숫자를 입력하십시오"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/opEdit"
            android:hint="연산자를 입력하십시오(+,-,*,/)"/>

    </LinearLayout>


    <Button
        android:id="@+id/popBtn"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="계산하기"
        android:textSize="30dp"
        />

</LinearLayout>

 

2. MainActivity.java

intent.putExtra를 이용하여 팝업 액티비티로 데이터 넘기기,

연산자의 입력 값을 받아 연산하기 위한 if문 작성

package com.example.popup;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    String fir;
    String sec;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText firEdit = (EditText)findViewById(R.id.firEdit);
        EditText secEdit = (EditText)findViewById(R.id.secEdit);
        EditText opEdit = (EditText)findViewById(R.id.opEdit);


        Button popBtn = (Button) findViewById(R.id.popBtn);
        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(opEdit.getText().toString().equals("+")){
                    fir = firEdit.getText().toString();
                    sec = secEdit.getText().toString();
                    double result = Double.parseDouble(fir)+Double.parseDouble(sec);
                    Intent intent = new Intent(MainActivity.this,PopActivity.class);
                    intent.putExtra("data",result);
                    startActivity(intent);
                }
                else if(opEdit.getText().toString().equals("-")){
                    fir = firEdit.getText().toString();
                    sec = secEdit.getText().toString();
                    double result = Double.parseDouble(fir)-Double.parseDouble(sec);
                    Intent intent = new Intent(MainActivity.this,PopActivity.class);
                    intent.putExtra("data",result);
                    startActivity(intent);
                }
                else if(opEdit.getText().toString().equals("*")){
                    fir = firEdit.getText().toString();
                    sec = secEdit.getText().toString();
                    double result = Double.parseDouble(fir)*Double.parseDouble(sec);
                    Intent intent = new Intent(MainActivity.this,PopActivity.class);
                    intent.putExtra("data",result);
                    startActivity(intent);
                }
                else if(opEdit.getText().toString().equals("/")){
                    fir = firEdit.getText().toString();
                    sec = secEdit.getText().toString();
                    double result = Double.parseDouble(fir)/Double.parseDouble(sec);
                    Intent intent = new Intent(MainActivity.this,PopActivity.class);
                    intent.putExtra("data",result);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(),"입력 값을 확인하십시오",Toast.LENGTH_SHORT).show();
                }

            }
        });

    }
}

 

3. activity_popup.xml

결과 값을 반환하기 위한 TextView 생성

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/popText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="결과 : "
        android:textSize="30dp" />

    <TextView
        android:id="@+id/resText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="값"
        android:textSize="30dp" />

</LinearLayout>


    <Button
        android:id="@+id/outBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="확인"
        android:textSize="30dp"
        android:layout_gravity="center"/>
</LinearLayout>

 

4. PopActivity.java

package com.example.popup;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class PopActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 타이틀 바 제거
        setContentView(R.layout.activity_popup);


        Intent intent = getIntent();
        Double res = intent.getDoubleExtra("data",0);
        String result = String.valueOf(res);

        TextView resText = (TextView)findViewById(R.id.resText);
        resText.setText(result);


        Button outBtn = (Button) findViewById(R.id.outBtn);
        outBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PopActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}

 

5. 결과