Skip to main content
 

How to make Recyclerview + Share option + Clipboard Manager in Android Studio (Koltin)

How to make Recyclerview + Share option + Clipboard Manager in Android Studio (Koltin): Hello guys, Welcome back to Coding Junction. In this article, we will talk about recyclerview adapter and implicit intent type share option and If I have time then we will discuss the clipboard manager otherwise we cover it in the next article.


Our application will look like this:



Edit the activity_main.xml



Now, we will add the recyclerview in the layout file. Recyclerview is an advanced form of the listview. Nowadays we are using recyclerview for better customize and for the better user interface. First, add the recylerview in the libraries, then write the below codes in your activity_main.xml file. Remark that you want to remove the default layout from the XML file.

activity_main.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


</android.support.v7.widget.RecyclerView>


Create a new layout file 



Create a new layout file. Name it "cardview_activity". We are creating this layout file for block section in our recyclerview adapter. We are using card view for creating separate blocks in our recyclerview adapter, it looks good and the user interface also improves. In the card_view, we will add the dummy text and a dummy image, which we will replace in our "adapter class".So, add the card view in your project libraries and then paste the below codes in your new layout files.

cardview_activity.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        card_view:cardCornerRadius="5dp">


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#fffee6"
            android:baselineAligned="false"
            android:padding="5dp">

        <TextView
                android:id="@+id/txvTitle"
                android:layout_width="355dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:text="Title"
                android:textColor="#616161"
                android:textSize="18sp"
                android:textStyle="bold"/>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <ImageView
                    android:id="@+id/imgShare"
                    android:layout_width="30dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:src="@android:drawable/ic_menu_share"
            />

            <ImageView
                    android:id="@+id/imgCopy"
                    android:layout_width="30dp"
                    android:layout_height="match_parent"
                    android:src="@mipmap/action_name"
            />

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>


Create a model class for recyclerview

Now, for the recyclerview, we need to implement two class in our project. First class is "model class" and the second class is an "adapter", So, the model is used to show the data on the user screen. In the below code block, you will notice that I have mention list of data, which I want to show in the user screen. The first data is "Coding Junction' and the second data is "Please subscribe", here you can show any data, you want to show. So, create a new class called "Model' and then paste below codes to your model class.

Model.kt

1
2
3
4
5
6
7
8
class Model(var title:String)

object Status {
    var status = listOf<Model>(
        Model("Coding Juction"),
        Model("Please Subscribe")
    )
}

Create an Adapter class

As I mention earlier, we want to create two class first is the "model" class and the second class is the "Adapter" class. We have discussed the model class earlier and now lets discussed the adapter class. We use an adapter for the for binding the data and show the data on the user screen, its bound the recyclerview and cardview with the data, which we have mentioned in the model class. So, just copy below codes and paste it into in your adapter class.

Adapter.kt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Adapter(var context: Context, private val status:List<Model>): RecyclerView.Adapter<Adapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.MyViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.cardview_activity, parent, false)
        return MyViewHolder(view)

    }

    override fun getItemCount(): Int {
        return status.size

    }

    override fun onBindViewHolder(holder: Adapter.MyViewHolder, position: Int) {
        val qwerty = status[position]
        holder.setData(qwerty,position)

    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private var model: Model? = null
        private var currentPosition:Int = 0

        init{
            itemView.setOnClickListener {
                Toast.makeText(context,"Clicked",Toast.LENGTH_SHORT).show()
            }
        }

        fun setData(model: Model?,pos:Int) {
            itemView.txvTitle.text = model!!.title

            this.model = model
            this.currentPosition = pos
        }

    }
}


Edit the MainActivity class

Now, we will add the data of the adapter class and model class in our main activity and also we will mention the orientation and layout of the card view which we have mentioned earlier. We are basically getting all the data, we have mentioned earlier. Here I have used a linear layout, but you can use a grid layout in your file.

MainActivity.kt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class MainActivity : AppCompatActivity() {

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

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerview.layoutManager = layoutManager

        val adapter = Adapter(this,Status.status)
        recyclerview.adapter = adapter
    }
}


Share option and Clipboard Manager


So, Now we will add the share option in our adapter class, inside the "init" block of "MyViewHolder" class. As I have mentioned earlier that, share option is a type of implicit intent. In the "Implicit Intent", we don't know that, where to send data, so we use the label as a key. And we use content copy button to copy the text and paste it into any another place. Content copy button was handle by Clipboard manager and we use "getSystemService" to call the clipboard manager.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 itemView.imgShare.setOnClickListener {
                val share = Intent(Intent.ACTION_SEND)
                share.type = "text/plain"
                share.putExtra(Intent.EXTRA_TEXT , model!!.title)
                it.context.startActivity(Intent.createChooser(share, "label"))
            }

            itemView.imgCopy.setOnClickListener {
                val Clipboard = it.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val myClip = ClipData.newPlainText("label" , model!!.title)
                Clipboard.primaryClip = myClip
                Toast.makeText(it.context,"Text_Copied",Toast.LENGTH_SHORT).show()
            }


Conclusion

So, at last, I will conclude that the recyclerview and share option is very simplest as well as very important for Android applications. You can also make money from this type of application. Adding the main screen and Ads to your application will look nicer. You can make this type of application for a major event like New Year, Diwali and Holi etc. And publish it to play store, and do some APO(App store optimization), share your application in WhatsApp and earn money from it. If you like this article, don't forget to share with your developer friends and please visit again. Thankyou

Comments

Popular posts from this blog

How to Create Calculator in Android Studio Using Kotlin

Hi Friends, calculator app is very easy to create in the android studio using Kotlin language as a language. In this article, I will write " How to Create Calculator in Android Studio Using Kotlin ".In this calculator app, I will design a beautiful user interface which you can publish in the play store. Generally, people only write about the logic of the app, but here I will write logic with an elegant user interface. So, let jump into the android studio. Let get started with calculator android app: Step 1: Create a new project in the android studio. Now, give the name of the application. Make sure you check the box of Kotlin support. Step 2: Select the API Level as per your wish. Select the Empty Activity in the project, And click next. Then, click finish and your project will be loaded. Now, for designing a beautiful User Interface. We will use Linear layout in our project. In the layout, there will be seven horizontal linear layouts. A

How to make a 2D Game in Android Studio Using Koltin

2D Game in Android Studio Using Koltin Hello guys, There are many games in the Play Store, which are developed by the much big company. If you want to develop, that type of games, then you surely move to Unity, which is a platform to developed 3D games. But if don't want to develop 3D games, then you can try android studio to develop 2D games. I completely agree that android studio is not for game development, but if you started with game development in the android studio, it will be very easy to develop an application in the android studio. In this tutorial post, we will learn " How to make a 2D Game in Android Studio Using Koltin ".We will use kotlin language for this tutorial. Our game will look like this after development: So, Let get started with the tutorial: Create a new project in Android Studio Create an empty project in the android studio. Name your application. Then select the Kotlin support. After that, select the API l

How to make image slider by view flipper in the android studio(kotlin)

Hi Guys, you have seemed professional android application which usually having image slider, So, in this, we will learn " How to make image slider by view flipper in the android studio(kotlin) ". activity_main.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" xmlns:app= "http://schemas.android.com/apk/res-auto" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" android:orientation= "vertical" > <ViewFlipper android:id= "@+id/v_flipper" android:layout_width= "match_parent" android:layout_height= "220dp" /> </LinearLayout> MainActivity.kt 1 2