Monday, 4 March 2013

Android ListView with RadioGroup and RadioButton.Part-1

Part-1

Till Now we have use ListView with the following widgets
1.TextView
2.ImageView
3.Button

Today i am going to use RadioGroup. 
Note *- It can be achieved by GridView but ListView is the optimal widget to use. 

Screen Shots:- List of RadioGroup with dynamically added RadioButton























Its very simple to show ListView like this.

Steps:-)

1)
res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />

</RelativeLayout>


Step2) listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="0dp" />

    <RadioGroup
        android:id="@+id/radio_group1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

Step3) Option.java
src/Option.java

package com.matrix.question;

public class MatrixOption {
        public String title;
        public MatrixOption(){
            super();
        }
       
        public MatrixOption( String title) {
            super();
            this.title = title;
        }
    }

Step 4) MainActivity.java

package com.list.radiogroup;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
     private ListView listView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       Option weather_data[] = new Option[]
                {
                    new Option (""),
                    new Option (""),
                    new Option (""),
                    new Option (""),
                    new Option ("")

                   /* new Option ("Heading1"),
                    new Option ("Heading2"),
                    new Option ("Heading3"),
                    new Option ("Heading4"),
                    new Option ("Heading5")*/
                };
        RadioGroupAdapter adapter = new RadioGroupAdapter (this,
                        R.layout.matrix, weather_data);
                listView1 = (ListView)findViewById(R.id.list);
                listView1.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
}

Step 5) Adapter
RadioGroupAdapter.java

package com.list.radiogroup;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RadioGroupAdapter extends ArrayAdapter<MatrixOption> {

    Context context;
    int layoutResourceId;
   Option data[] = null;

    public RadioGroupAdapter(Context context, int layoutResourceId,
            MatrixOption[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RadioHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RadioHolder ();
            holder.txtTitle = (TextView) row.findViewById(R.id.heading);
            holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
            final RadioButton[] rb = new RadioButton[5];
            for(int i=0; i<5; i++){
                rb[i]  = new RadioButton(context);
//                rb[i].setButtonDrawable(R.drawable.single_radio_chice);
                rb[i].setId(i);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight=1.0f;
                params.setMargins(15, 0, 5, 10);
                holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
            }
            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;
    }

    static class RadioHolder {
        TextView txtTitle;
        RadioGroup group;
        int position;
    }
}



Download full Source code Click ListWithRadioGroup

Feel free if any one have any doubt.



7 comments:

  1. hi i m new to android. When I scroll my listview then radio button checked position has changing.

    ReplyDelete
  2. Hello bro i am in a problem same as britto and please urgently tell us solution or give solution by your present code of listviewwithradiobutton please

    ReplyDelete
  3. Hello please give some solution

    ReplyDelete
  4. scroll in item size 15 ramdon position

    ReplyDelete
  5. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my

    vision, keep sharing..
    Mobile App Development Company
    Mobile App Development Company in India
    Mobile App Development Companies

    ReplyDelete
  6. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Mobile Marketing Services
    Mobile marketing companies
    Fitness SMS

    ReplyDelete