{"id":18757,"date":"2022-11-01T21:22:40","date_gmt":"2022-11-01T15:52:40","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/"},"modified":"2022-11-01T21:22:40","modified_gmt":"2022-11-01T15:52:40","slug":"solved-display-a-toast-after-clicking-an-item-in-a-recyclerview","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/","title":{"rendered":"[Solved] Display a toast after clicking an item in a RecyclerView"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-73624097\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"73624097\" data-parentid=\"73494035\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><strong>According to this question this is the best way to implement clicking on the item of RecyclerView<\/strong><\/p>\n<p>Firstly create a file <code>values\/ids.xml<\/code> and put this in it:<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;resources&gt;\n    &lt;item name=\"item_click_support\" type=\"id\" \/&gt;\n&lt;\/resources&gt;\n<\/code><\/pre>\n<p>then add the code below to your source<\/p>\n<h2>for Java<\/h2>\n<p>Copy the following helper class to your project<\/p>\n<pre><code>public class ItemClickSupport {\n    private final RecyclerView mRecyclerView;\n    private OnItemClickListener mOnItemClickListener;\n    private OnItemLongClickListener mOnItemLongClickListener;\n    private View.OnClickListener mOnClickListener = new View.OnClickListener() {\n        @Override\n        public void onClick(View v) {\n            if (mOnItemClickListener != null) {\n                \/\/ ask the RecyclerView for the viewHolder of this view.\n                \/\/ then use it to get the position for the adapter\n                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);\n                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);\n            }\n        }\n    };\n    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {\n        @Override\n        public boolean onLongClick(View v) {\n            if (mOnItemLongClickListener != null) {\n                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);\n                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);\n            }\n            return false;\n        }\n    };\n    private RecyclerView.OnChildAttachStateChangeListener mAttachListener\n            = new RecyclerView.OnChildAttachStateChangeListener() {\n        @Override\n        public void onChildViewAttachedToWindow(View view) {\n            \/\/ every time a new child view is attached add click listeners to it\n            if (mOnItemClickListener != null) {\n                view.setOnClickListener(mOnClickListener);\n            }\n            if (mOnItemLongClickListener != null) {\n                view.setOnLongClickListener(mOnLongClickListener);\n            }\n        }\n\n        @Override\n        public void onChildViewDetachedFromWindow(View view) {\n\n        }\n    };\n\n    private ItemClickSupport(RecyclerView recyclerView) {\n        mRecyclerView = recyclerView;\n        \/\/ the ID must be declared in XML, used to avoid\n        \/\/ replacing the ItemClickSupport without removing\n        \/\/ the old one from the RecyclerView\n        mRecyclerView.setTag(R.id.item_click_support, this);\n        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);\n    }\n\n    public static ItemClickSupport addTo(RecyclerView view) {\n        \/\/ if there's already an ItemClickSupport attached\n        \/\/ to this RecyclerView do not replace it, use it\n        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);\n        if (support == null) {\n            support = new ItemClickSupport(view);\n        }\n        return support;\n    }\n\n    public static ItemClickSupport removeFrom(RecyclerView view) {\n        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);\n        if (support != null) {\n            support.detach(view);\n        }\n        return support;\n    }\n\n    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {\n        mOnItemClickListener = listener;\n        return this;\n    }\n\n    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {\n        mOnItemLongClickListener = listener;\n        return this;\n    }\n\n    private void detach(RecyclerView view) {\n        view.removeOnChildAttachStateChangeListener(mAttachListener);\n        view.setTag(R.id.item_click_support, null);\n    }\n\n    public interface OnItemClickListener {\n\n        void onItemClicked(RecyclerView recyclerView, int position, View v);\n    }\n\n    public interface OnItemLongClickListener {\n\n        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);\n    }\n}\n<\/code><\/pre>\n<h2> Usage: <\/h2>\n<p>just call it from activity \/ fragment like the following<\/p>\n<pre><code>ItemClickSupport.addTo(mRecyclerView)\n        .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {\n    @Override\n    public void onItemClicked(RecyclerView recyclerView, int position, View v) {\n        \/\/ do it\n   Toast.makeText(v.getContext(), \"Item is clicked\", Toast.LENGTH_SHORT).show();\n\n    }\n});\n<\/code><\/pre>\n<p>(it also supports long item click)<\/p>\n<p>Source<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Display a toast after clicking an item in a RecyclerView <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] According to this question this is the best way to implement clicking on the item of RecyclerView Firstly create a file values\/ids.xml and put this in it: &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt; &lt;resources&gt; &lt;item name=&#8221;item_click_support&#8221; type=&#8221;id&#8221; \/&gt; &lt;\/resources&gt; then add the code below to your source for Java Copy the following helper class to your project &#8230; <a title=\"[Solved] Display a toast after clicking an item in a RecyclerView\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\" aria-label=\"More on [Solved] Display a toast after clicking an item in a RecyclerView\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1508,452,1799,4175],"class_list":["post-18757","post","type-post","status-publish","format-standard","hentry","category-solved","tag-adapter","tag-android","tag-android-recyclerview","tag-toast"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] According to this question this is the best way to implement clicking on the item of RecyclerView Firstly create a file values\/ids.xml and put this in it: &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;resources&gt; &lt;item name=&quot;item_click_support&quot; type=&quot;id&quot; \/&gt; &lt;\/resources&gt; then add the code below to your source for Java Copy the following helper class to your project ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T15:52:40+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Display a toast after clicking an item in a RecyclerView\",\"datePublished\":\"2022-11-01T15:52:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\"},\"wordCount\":85,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"adapter\",\"android\",\"android-recyclerview\",\"toast\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\",\"name\":\"[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T15:52:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Display a toast after clicking an item in a RecyclerView\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb","og_description":"[ad_1] According to this question this is the best way to implement clicking on the item of RecyclerView Firstly create a file values\/ids.xml and put this in it: &lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt; &lt;resources&gt; &lt;item name=\"item_click_support\" type=\"id\" \/&gt; &lt;\/resources&gt; then add the code below to your source for Java Copy the following helper class to your project ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T15:52:40+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Display a toast after clicking an item in a RecyclerView","datePublished":"2022-11-01T15:52:40+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/"},"wordCount":85,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["adapter","android","android-recyclerview","toast"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/","url":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/","name":"[Solved] Display a toast after clicking an item in a RecyclerView - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T15:52:40+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-display-a-toast-after-clicking-an-item-in-a-recyclerview\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Display a toast after clicking an item in a RecyclerView"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18757"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18757\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}