Create a share Button with Material Design 3

Create a share Button with Material Design 3

Share Button

Share Button is just a button in your application which can be used to share
information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, videos, links, etc. 

 


In this article, we are going to create a simple Share Button.


Steps

Step 1: Create a New Project.

Step 2 : Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. 
Below is the code for the activity_main.xml file. 


<?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">
<com.google.android.material.button.MaterialButton
android:id="@+id/share_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Share"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3 : Working with the MainActivity.kt file


Go to the MainActivity.kt file and add the code given below.

package com.example.androidappcodingblog

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.material.button.MaterialButton

class ShareButton : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val bt = findViewById<MaterialButton>(R.id.share_bt)
bt.setOnClickListener {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, "this is my")
startActivity(Intent.createChooser(shareIntent, "Share via"))
}
}
}

OUTPUT




















Thanks for reading!😊😊😊😊😊😊
If you enjoyed reading , Show some Love by
 clapping 👏 on this post!
If you have any suggestion share with me.


Post a Comment

0 Comments