Android 액티비티를 팝업(Popup)으로 띄우기, 데이터 주고받기
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txtResult;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
txtResult = (TextView)findViewById(R.id.txtResult); }
//버튼 public void mOnPopupClick(View v){ //데이터 담아서 팝업(액티비티) 호출 Intent intent = new Intent(this, PopupActivity.class); intent.putExtra("data", "Test Popup"); startActivityForResult(intent, 1); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==1){ if(resultCode==RESULT_OK){ //데이터 받기 String result = data.getStringExtra("result"); txtResult.setText(result); } } } } |
PopupActivity.java
public class PopupActivity extends Activity {
TextView txtText;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //타이틀바 없애기 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.popup_activity);
//UI 객체생성 txtText = (TextView)findViewById(R.id.txtText);
//데이터 가져오기 Intent intent = getIntent(); String data = intent.getStringExtra("data"); txtText.setText(data); }
//확인 버튼 클릭 public void mOnClose(View v){ //데이터 전달하기 Intent intent = new Intent(); intent.putExtra("result", "Close Popup"); setResult(RESULT_OK, intent);
//액티비티(팝업) 닫기 finish(); }
@Override public boolean onTouchEvent(MotionEvent event) { //바깥레이어 클릭시 안닫히게 if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ return false; } return true; }
@Override public void onBackPressed() { //안드로이드 백버튼 막기 return; } } |
* 10 : 타이틀바 없애기
* 17-18 : Intent 에서 data 가져오기
* 25-27 : Intent로 data 전달하기
* 34-40 : 바깥 레이어를 눌러도 닫히지 않게 하기
* 43-46 : 안드로이드 백버튼 막기
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="액티비티를 팝업으로 띄우기" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="팝업" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mOnPopupClick"/> </LinearLayout>
<LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
</LinearLayout> |
popup_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#eeeeee" android:orientation="vertical" android:layout_width="300dp" android:layout_height="wrap_content">
<!-- 타이틀바 --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="Notice" android:textSize="20sp" android:textColor="#fff" android:gravity="center" android:background="#ff7a00" android:layout_width="match_parent" android:layout_height="53dp" /> </LinearLayout> <!-- //end 타이틀바 -->
<!-- Notice --> <LinearLayout android:padding="24dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txtText" android:textSize="15sp" android:textColor="#000" android:alpha="0.87" android:gravity="center" android:layout_marginBottom="3dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- Notice -->
<View android:background="#66bdbdbd" android:layout_width="match_parent" android:layout_height="1dp" />
<!-- 닫기 버튼 --> <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="확인" android:textSize="15sp" android:textColor="#ff7a00" android:padding="16dp" android:gravity="center" android:background="#00000000" android:layout_width="match_parent" android:layout_height="53dp" android:onClick="mOnClose"/> </LinearLayout> <!--// 닫기 버튼 --> </LinearLayout> |
* 팝업의 가로세로 길이는 직접 정해줍니다
AndroidMenifest.xml
<!-- 팝업 Activity --> <activity android:name=".PopupActivity" android:theme="@android:style/Theme.Dialog" /> |
* Popup으로 띄울 Activity의 Theme를 바꿔줍니다
결과
* 팝업 호출(데이터 전달) -> 팝업에서 전달받은 데이터 보여줌 , 팝업 닫기(데이터 전달) -> 데이터 받아서 보여줌
출처: https://ghj1001020.tistory.com/9 [혁준 블로그]
'프로그래밍 > Android (Java)' 카테고리의 다른 글
naverMap 을 전역 변수로 활용하기 (0) | 2019.06.07 |
---|---|
안드로이드 개발 Android GPS 정보 알아오기 (0) | 2019.06.07 |
xml 파일로 말풍선 만들기 (0) | 2019.05.27 |
DialogFragment로 Custom Dialog 만들기 (0) | 2019.05.17 |
라디오버튼 사용하기 (0) | 2019.05.17 |