<h3><span style="color: #000080;"><strong>Checked TextView</strong></span></h3>
<p><strong><span style="color: #008000;">CheckedTextView</span></strong> is an extension of normal <strong><span style="color: #008000;">TextView</span></strong> that has a checkbox along with some text. It is mainly used in a <span style="color: #008000;"><strong>ListView </strong></span>where we want to show which item is selected or not. <strong><span style="color: #008000;">Checkmark </span></strong>attribute is used to provide a graphic or a drawable to CheckedTextView.</p>
<p>Download <strong><span style="color: #008000;">GITHUB</span> </strong>code from <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://github.com/arunk7839/CheckedTextViewExp"><strong>Here</strong></a></span>.</p>
<p><span class="embed-youtube" style="text-align:center; display: block;"><amp-youtube data-videoid="ZuGVJdicKdA" data-param-rel="1" data-param-showsearch="0" data-param-showinfo="1" data-param-iv_load_policy="1" data-param-fs="1" data-param-hl="en-US" data-param-autohide="2" data-param-wmode="transparent" width="1200" height="675" layout="responsive"><a href="https://www.youtube.com/watch?v=ZuGVJdicKdA" placeholder><amp-img src="https://i.ytimg.com/vi/ZuGVJdicKdA/hqdefault.jpg" alt="YouTube Poster" layout="fill" object-fit="cover"><noscript><img src="https://i.ytimg.com/vi/ZuGVJdicKdA/hqdefault.jpg" loading="lazy" decoding="async" alt="YouTube Poster"></noscript></amp-img></a></amp-youtube></span></p>
<p> ;</p>
<h3><span style="color: #000080;"><strong>Creating New Project</strong></span></h3>
<p><strong><span style="color: #0000ff;">1.</span></strong> In Android Studio, go to <span style="color: #008000;"><strong>File </strong><strong>⇒</strong><strong> New Project</strong></span> and fill all the details required to create a new project. When it prompts to select a default activity, select <span style="color: #008000;"><strong>Blank Activity</strong></span> and proceed.</p>
<p><strong><span style="color: #0000ff;">2. </span></strong>Open <span style="color: #008000;"><strong>build.gradle</strong></span> and add <span style="color: #008000;"><strong>recyclerView</strong> </span>dependency.</p>
<p><span style="color: #008000;"><strong>com.android.support:recyclerview-v7:26.1.0</strong></span> and rebuild the project.</p>
<p><span style="color: #0000ff;"><strong>Build.gradle</strong></span></p>
<pre><strong>dependencies {</strong>

<span style="color: #008000;"><strong>// RecyclerView</strong></span>
<strong> implementation</strong> <span style="color: #0000ff;"><strong>'com.android.support:recyclerview-v7:26.1.0'</strong></span>

}</pre>
<p><strong><span style="color: #0000ff;">3. </span></strong>In <span style="color: #008000;"><strong>activity_main.xml</strong></span> I have added one <span style="color: #008000;"><strong>RecyclerView</strong></span> with the following basic properties.</p>
<p><span style="color: #0000ff;"><strong>activity_main.xml</strong></span><br />
<code></code></p>
<pre><strong><;?xml version="1.0" encoding="utf-8"?>;</strong>
<strong><;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</strong>
<strong> xmlns:tools="http://schemas.android.com/tools"</strong>
<strong> android:layout_width="match_parent"</strong>
<strong> android:layout_height="match_parent"</strong>
<strong> tools:context="com.example.lenovo.checkedtextviewapp.MainActivity">;</strong>

<strong> <;android.support.v7.widget.RecyclerView</strong>
<strong> android:id="@+id/recycler_view"</strong>
<strong> android:layout_width="match_parent"</strong>
<strong> android:layout_height="match_parent">;</strong>

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

<strong><;/RelativeLayout>;</strong>

</pre>
<p> ;</p>
<h3><span style="color: #000080;"><strong>Writing the Adapter Class</strong></span></h3>
<p>After adding the RecyclerView widget, let’s start writing the <strong><span style="color: #008000;">adapter</span> </strong>class to render the data.</p>
<p><strong><span style="color: #0000ff;">4. </span></strong>Create an layout xml named <span style="color: #008000;"><strong>row_item.xml</strong></span> with the below code. This layout file shows a single row in recycler view .</p>
<p><span style="color: #0000ff;"><strong>row_item.xml</strong></span><br />
<code></code></p>
<pre><strong><;?xml version="1.0" encoding="utf-8"?>;</strong>
<strong><;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</strong>
<strong> android:layout_width="match_parent"</strong>
<strong> android:layout_height="wrap_content">;</strong>


<strong> <;CheckedTextView</strong>
<strong> android:id="@+id/simpleCheckedTextView"</strong>
<strong> android:layout_width="match_parent"</strong>
<strong> android:layout_height="wrap_content"</strong>
<strong> android:layout_marginLeft="5dp"</strong>
<strong> android:layout_marginRight="5dp"</strong>
<strong> android:layout_marginTop="18dp"</strong>
<strong> android:checked="false"</strong>
<strong> android:gravity="center_vertical"</strong>
<strong> android:padding="8dp"</strong>
<strong> android:textSize="18sp" />;</strong>

<strong><;/LinearLayout>;</strong>

</pre>
<p><strong><span style="color: #0000ff;">5. </span></strong>Now create a class named <span style="color: #008000;"><strong>SingerAdapter.java</strong></span> and add the below code.</p>
<ul>
<li><strong><span style="color: #0000ff;">onCreateViewHolder() </span>: </strong>Inflates <span style="color: #008000;"><strong>row_item.xml</strong>.</span></li>
<li><span style="color: #0000ff;"><strong>onBindViewHolder()</strong></span> <strong>: </strong>Set singersname In checkedTextView and apply <strong><span style="color: #008000;">onClickListener</span></strong> on each item of recyclerView.</li>
<li><strong><span style="color: #0000ff;">getItemCount() </span>: </strong>Retuns the array <strong><span style="color: #008000;">length.</span></strong></li>
</ul>
<p><span style="color: #0000ff;"><strong>SingerAdapter.java</strong> </span><br />
<code></code></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre><strong>package com.example.lenovo.checkedtextviewapp;</strong>

<strong>import android.content.Context;</strong>
<strong>import android.support.v7.widget.RecyclerView;</strong>
<strong>import android.view.LayoutInflater;</strong>
<strong>import android.view.View;</strong>
<strong>import android.view.ViewGroup;</strong>
<strong>import android.widget.CheckedTextView;</strong>
<strong>import android.widget.Toast;</strong>


<strong>public class SingerAdapter extends RecyclerView.Adapter<;SingerAdapter.MyViewHolder>; {</strong>

<strong> private String[] singersName;</strong>
<strong> Context context;</strong>


<strong> public SingerAdapter(Context context, String[] booknames) {</strong>
<strong> this.singersName = booknames;</strong>
<strong> this.context = context;</strong>
<strong> }</strong>

<strong> public class MyViewHolder extends RecyclerView.ViewHolder {</strong>
<strong> CheckedTextView simpleCheckedTextView;</strong>

<strong> public MyViewHolder(View view) {</strong>
<strong> super(view);</strong>
<strong> simpleCheckedTextView = (CheckedTextView) view.findViewById(R.id.simpleCheckedTextView);</strong>

<strong> }</strong>
<strong> }</strong>

<strong> @Override</strong>
<strong> public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {</strong>
<strong> View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);</strong>
<strong> return new MyViewHolder(itemView);</strong>

<strong> }</strong>

<strong> @Override</strong>
<strong> public void onBindViewHolder(final MyViewHolder holder, int position) {</strong>

<strong> holder.simpleCheckedTextView.setText(singersName[position]);</strong>

<span style="color: #008000;"><strong> // perform on Click Event Listener on CheckedTextView</strong></span>
<strong> holder.simpleCheckedTextView.setOnClickListener(new View.OnClickListener() {</strong>
<strong> @Override</strong>
<strong> public void onClick(View v) {</strong>
<strong> Boolean value = holder.simpleCheckedTextView.isChecked();</strong>
<strong> if (value) {</strong>
<span style="color: #008000;"><strong> // set check mark drawable and set checked property to false</strong>
</span>
<strong> holder.simpleCheckedTextView.setCheckMarkDrawable(R.drawable.check_ic);</strong>
<strong> holder.simpleCheckedTextView.setChecked(false);</strong>
<strong> Toast.makeText(context, "un-Checked", Toast.LENGTH_LONG).show();</strong>
<strong> } else {</strong>
<span style="color: #008000;"><strong> // set check mark drawable and set checked property to true</strong></span>

<strong> holder.simpleCheckedTextView.setCheckMarkDrawable(R.drawable.check);</strong>
<strong> holder.simpleCheckedTextView.setChecked(true);</strong>
<strong> Toast.makeText(context, "Checked", Toast.LENGTH_LONG).show();</strong>
<strong> }</strong>
<strong> }</strong>
<strong> });</strong>

<strong> }</strong>

<strong> @Override</strong>
<strong> public int getItemCount() {</strong>
<strong> return singersName.length;</strong>
<strong> }</strong>
<strong>}</strong>
</pre>
<p><strong><span style="color: #0000ff;">6. </span></strong>Now open <span style="color: #008000;"><strong>MainActivity.java</strong></span> and write the below code.</p>
<p><span style="color: #0000ff;"><strong>MainActivity.java</strong></span><br />
<code></code></p>
<pre><strong>package com.example.lenovo.checkedtextviewapp;</strong>

<strong>import android.support.v7.app.AppCompatActivity;</strong>
<strong>import android.os.Bundle;</strong>
<strong>import android.support.v7.widget.DividerItemDecoration;</strong>
<strong>import android.support.v7.widget.LinearLayoutManager;</strong>
<strong>import android.support.v7.widget.RecyclerView;</strong>

<strong>public class MainActivity extends AppCompatActivity {</strong>

<strong> private RecyclerView recyclerView;</strong>
<strong> private SingerAdapter mAdapter;</strong>

<strong> String[] singersName = {"Mohammad Rafi", "Lata Mangeshkar", "Sonu Nigam", "Kishore Kumar",</strong>
<strong> "Sreya Ghoshal ","Asha Bhosle","Udit Narayan","Alka Yagnik"};</strong>

<strong> @Override</strong>
<strong> protected void onCreate(Bundle savedInstanceState) {</strong>
<strong> super.onCreate(savedInstanceState);</strong>
<strong> setContentView(R.layout.activity_main);</strong>

<strong> recyclerView = (RecyclerView) findViewById(R.id.recycler_view);</strong>

<strong> mAdapter = new SingerAdapter(getApplicationContext(), singersName);</strong>


<span style="color: #008000;"><strong> // vertical RecyclerView</strong>
 </span><strong> RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());</strong>
 
<strong> <span style="color: #008000;">// horizontal RecyclerView</span></strong>
<span style="color: #008000;"><strong> // RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);</strong>
</span><strong> recyclerView.setLayoutManager(mLayoutManager);</strong>

 <span style="color: #008000;"><strong>// adding inbuilt divider line</strong></span>
<strong> recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));</strong>

<strong> recyclerView.setAdapter(mAdapter);</strong>

<strong> }</strong>
<strong>}</strong>
</pre>
<p><span style="color: #0000ff;"><strong>Now when you run the app it will look like this:</strong></span></p>
<p><img class="alignnone wp-image-202" src="https://c1ctech.com/wp-content/uploads/2018/03/Screenshot_2018-03-08-14-00-521-2267742709-1580997100338.png" alt="Screenshot_2018-03-08-14-00-52[1]" width="469" height="704" />

