와챠의 우당탕탕 코딩 일기장
[안드로이드] 7장 연습문제 5번(랜덤 위치에 토스트 띄우기) 본문
반응형
5번
버튼을 클릭하면 임의의 위치에 이미지가 들어간 토스트가 나오는 프로젝트를 작성하시오.

activity_main.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
tools:context=".MainActivity" | |
android:gravity="center"> | |
<Button | |
android:id="@+id/btn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="토스트 보이기" | |
android:textSize="20dp" | |
android:backgroundTint="#cccccc" | |
android:textColor="#000000"/> | |
</LinearLayout> |
toast.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:id="@+id/imgDog" | |
android:layout_width="100dp" | |
android:layout_height="100dp" | |
android:src="@drawable/dog" /> | |
</LinearLayout> |
MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mytest | |
import android.content.Context | |
import android.os.Bundle | |
import android.view.Gravity | |
import android.view.View | |
import android.view.WindowManager | |
import android.widget.Button | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
class MainActivity : AppCompatActivity() { | |
lateinit var btn : Button | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
title = "7장 연습문제 5번" | |
btn = findViewById<Button>(R.id.btn) | |
btn.setOnClickListener { | |
// 토스트 만들기 | |
var toast = Toast(this@MainActivity) | |
// 토스트에 들어갈 뷰 만들기, 이때 연결될 뷰 설정 | |
var toastView = View.inflate(this@MainActivity, R.layout.toast, null) | |
// 토스트 뷰 연결 | |
toast.view = toastView | |
// 토스트 랜덤 위치에 출력하기 | |
var display = (getSystemService(Context.WINDOW_SERVICE) as | |
WindowManager).defaultDisplay | |
var xOffset = (Math.random() * display.width).toInt() | |
var yOffset = (Math.random() * display.height).toInt() | |
toast.setGravity(Gravity.TOP or Gravity.LEFT, xOffset, yOffset) | |
// 토스트 보이기 | |
toast.show() | |
} | |
} | |
} |
강아지 사진으로 하니까 진짜 강아지가 여기저기 뛰노는 거 같아서 넘 귀여웠다.
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 직접 풀어보기 10-1(여러 액티비티 전환) (0) | 2021.04.11 |
---|---|
[안드로이드] 7장 연습문제 6번(라디오박스로 선택한 동물 대화상자에 보이기) (0) | 2021.04.10 |
[안드로이드] 직접 풀어보기 7-1(메뉴 이용해서 사진 회전, 변환) (0) | 2021.04.09 |
[안드로이드] 그냥 연습(CircleIndicator3, Fragment) (0) | 2021.04.03 |
[안드로이드] 6장 연습문제 6번 (0) | 2021.04.03 |
Comments