{"id":2090,"date":"2016-07-04T11:46:47","date_gmt":"2016-07-04T11:46:47","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=2090"},"modified":"2019-06-12T12:22:33","modified_gmt":"2019-06-12T12:22:33","slug":"radiobutton-inside-listview","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/radiobutton-inside-listview.html","title":{"rendered":"RadioButton Inside ListView Example In Android Studio"},"content":{"rendered":"<p>Here\u00a0is the example of using RadioButton Inside ListView. In this example we create a list of Questions with two options yes or no. For providing these options we use Radio Buttons with Radio Group inside our list item. Firstly we create an String array for questions and then set an adapter to fill the data in the ListView. In this example we also display a Submit button and perform setOnClickListener event on Button so whenever a user click on this Button the selected answers of questions is displayed by using a Toast.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/RadioButtonInsideListViewExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2095\" src=\"\/ui\/wp-content\/uploads\/2016\/07\/RadioButton-Inside-ListView-Example.jpg\" alt=\"RadioButton Inside ListView Example\" width=\"275\" height=\"430\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/07\/RadioButton-Inside-ListView-Example.jpg 275w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/07\/RadioButton-Inside-ListView-Example-192x300.jpg 192w\" sizes=\"auto, (max-width: 275px) 100vw, 275px\" \/><\/p>\n<p>If you are new here\u00a0then first read our <a href=\"\/ui\/radiobutton\">RadioButton tutorial<\/a> and <a href=\"\/ui\/listview\">ListView tutorial<\/a>.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it RadioButtonInsideListView.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:\u00a0<\/strong><\/span>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we open xml file and add code for displaying ListView.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n&lt;!-- ListView and Submit Button--&gt;\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_above=\"@+id\/submit\"\r\nandroid:divider=\"@color\/material_blue_grey_800\"\r\nandroid:dividerHeight=\"1dp\"\r\nandroid:footerDividersEnabled=\"false\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/submit\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_alignParentBottom=\"true\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:background=\"#0f0\"\r\nandroid:text=\"Submit\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"20sp\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n&lt;!-- ListView and Submit Button--&gt;\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_above=\"@+id\/submit\"\r\nandroid:divider=\"@color\/material_blue_grey_800\"\r\nandroid:dividerHeight=\"1dp\"\r\nandroid:footerDividersEnabled=\"false\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/submit\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_alignParentBottom=\"true\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:background=\"#0f0\"\r\nandroid:text=\"Submit\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"20sp\" \/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Create a new xml file i.e list_items.xml inside layout and add the below code:In this step we create a new xml file and add the code for displaying a TextView and RadioButton\u2019s.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:orientation=\"vertical\"&gt;\r\n&lt;!-- TextView for displaying question--&gt;\r\n&lt;TextView\r\nandroid:id=\"@+id\/question\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:padding=\"@dimen\/activity_horizontal_margin\"\r\nandroid:textColor=\"#000\" \/&gt;\r\n&lt;!-- RadioGroup for grouping of RadioButton--&gt;\r\n&lt;RadioGroup\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginTop=\"10dp\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:weightSum=\"2\"&gt;\r\n\r\n&lt;RadioButton\r\nandroid:id=\"@+id\/yes\"\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginLeft=\"15dp\"\r\nandroid:layout_weight=\"1\"\r\nandroid:text=\"Yes\"\r\nandroid:textColor=\"#000\" \/&gt;\r\n\r\n&lt;RadioButton\r\nandroid:id=\"@+id\/no\"\r\nandroid:layout_width=\"0dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginLeft=\"15dp\"\r\nandroid:layout_weight=\"1\"\r\nandroid:text=\"No\"\r\nandroid:textColor=\"#000\" \/&gt;\r\n&lt;\/RadioGroup&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:\u00a0<\/strong><\/span>Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step Firstly we get the reference of Button and ListView. After that we create an String array for questions and then set an adapter to fill the data in the ListView. Finally we perform setOnClickListener event on Button so whenever a user click on Button the selected answers of questions is displayed by using a Toast.<\/p>\n<pre>package com.abhiandroid.radiobuttoninsidelistviewexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.ListView;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\nListView simpleList;\r\nString[] questions;\r\nButton submit;\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\n\/\/ get the string array from string.xml file\r\nquestions = getResources().getStringArray(R.array.questions);\r\n\/\/ get the reference of ListView and Button\r\nsimpleList = (ListView) findViewById(R.id.simpleListView);\r\nsubmit = (Button) findViewById(R.id.submit);\r\n\/\/ set the adapter to fill the data in the ListView\r\nCustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);\r\nsimpleList.setAdapter(customAdapter);\r\n\/\/ perform setOnClickListerner event on Button\r\nsubmit.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\nString message = \"\";\r\n\/\/ get the value of selected answers from custom adapter\r\nfor (int i = 0; i &lt; CustomAdapter.selectedAnswers.size(); i++) {\r\nmessage = message + \"\\n\" + (i + 1) + \" \" + CustomAdapter.selectedAnswers.get(i);\r\n}\r\n\/\/ display the message on screen with the help of Toast.\r\nToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();\r\n}\r\n});\r\n}\r\n\r\n@Override\r\npublic boolean onCreateOptionsMenu(Menu menu) {\r\n\/\/ Inflate the menu; this adds items to the action bar if it is present.\r\ngetMenuInflater().inflate(R.menu.menu_main, menu);\r\nreturn true;\r\n}\r\n\r\n@Override\r\npublic boolean onOptionsItemSelected(MenuItem item) {\r\n\/\/ Handle action bar item clicks here. The action bar will\r\n\/\/ automatically handle clicks on the Home\/Up button, so long\r\n\/\/ as you specify a parent activity in AndroidManifest.xml.\r\nint id = item.getItemId();\r\n\r\n\/\/noinspection SimplifiableIfStatement\r\nif (id == R.id.action_settings) {\r\nreturn true;\r\n}\r\n\r\nreturn super.onOptionsItemSelected(item);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span>\u00a0Create a new class CustomAdapter.java inside package and add the following code<\/p>\n<p>In this step we create a custom adapter class and extends BaseAdapter in this class. In this step we firstly set the data in the list and then perform setOnCheckedChangeListener event on RadioButton.<\/p>\n<pre>package com.abhiandroid.radiobuttoninsidelistviewexample;\r\n\r\nimport android.content.Context;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.BaseAdapter;\r\nimport android.widget.CompoundButton;\r\nimport android.widget.RadioButton;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.ArrayList;\r\n\r\n\r\npublic class CustomAdapter extends BaseAdapter {\r\nContext context;\r\nString[] questionsList;\r\nLayoutInflater inflter;\r\npublic static ArrayList&lt;String&gt; selectedAnswers;\r\n\r\npublic CustomAdapter(Context applicationContext, String[] questionsList) {\r\nthis.context = context;\r\nthis.questionsList = questionsList;\r\n\/\/ initialize arraylist and add static string for all the questions\r\nselectedAnswers = new ArrayList&lt;&gt;();\r\nfor (int i = 0; i &lt; questionsList.length; i++) {\r\nselectedAnswers.add(\"Not Attempted\");\r\n}\r\ninflter = (LayoutInflater.from(applicationContext));\r\n}\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn questionsList.length;\r\n}\r\n\r\n@Override\r\npublic Object getItem(int i) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic long getItemId(int i) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic View getView(final int i, View view, ViewGroup viewGroup) {\r\nview = inflter.inflate(R.layout.list_items, null);\r\n\/\/ get the reference of TextView and Button's\r\nTextView question = (TextView) view.findViewById(R.id.question);\r\nRadioButton yes = (RadioButton) view.findViewById(R.id.yes);\r\nRadioButton no = (RadioButton) view.findViewById(R.id.no);\r\n\/\/ perform setOnCheckedChangeListener event on yes button\r\nyes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {\r\n@Override\r\npublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {\r\n\/\/ set Yes values in ArrayList if RadioButton is checked\r\nif (isChecked)\r\nselectedAnswers.set(i, \"Yes\");\r\n}\r\n});\r\n\/\/ perform setOnCheckedChangeListener event on no button\r\nno.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {\r\n@Override\r\npublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {\r\n\/\/ set No values in ArrayList if RadioButton is checked\r\nif (isChecked)\r\nselectedAnswers.set(i, \"No\");\r\n\r\n}\r\n});\r\n\/\/ set the value in TextView\r\nquestion.setText(questionsList[i]);\r\nreturn view;\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here\u00a0is the example of using RadioButton Inside ListView. In this example we create a list of Questions with two options yes or no. For providing these options we use Radio Buttons with Radio Group inside our list item. Firstly we create an String array for questions and then set an adapter to fill the data &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/radiobutton-inside-listview.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RadioButton Inside ListView Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2095,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,68],"tags":[],"class_list":["post-2090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-archieve","category-radiobutton"],"acf":[],"psp_head":"<title>RadioButton Inside ListView Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Here is the example of using RadioButton Inside ListView. In this example we create a list of Questions with two options yes or no. For providing these options we use Radio Buttons with Radio Group inside our list item.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/radiobutton-inside-listview.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2090"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2090\/revisions"}],"predecessor-version":[{"id":2818,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2090\/revisions\/2818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/2095"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=2090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=2090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}