안드로이드
안드로이드 공유하기 예제 Kotlin
Moimeme Futur
2021. 12. 16. 13:58
실행화면
프로젝트 구조
Code - Example
activity_main.xml
공유하기 버튼 을 만든다.
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#BBCCDD"
android:text="공유하기"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
공유하기 기능을 구현한다.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initShareButton()
}
private fun initShareButton() {
val shareButton = findViewById<AppCompatButton>(R.id.btnShare)
shareButton.setOnClickListener {
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(
Intent.EXTRA_TEXT,
"https://devgeek.tistory.com" // 전달하려는 Data(Value)
)
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, null))
}
}
}