Pages

Wednesday, September 19, 2012

Layout Inflator Service

Layout Inflator Service

If you want to customize any layout/View then you have to use "Layout Inflator Service"

we are learn that by listview example

If you want to change the default layout of the list view then you have to use BaseAdapter + Layout Inflator service.

Now, You have to perform following Step:-

1) Take the list view in res/main.xml file as follows:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
   

2) Take another layout xml in res/row.xml which contain layout for each cell(row) in listview as follows:


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

                    android:id="@+id/imageView1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/ic_action_search" />

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_toRightOf="@id/imageView1"
             >
                            android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                />
                            android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                

                android:id="@+id/btnDelete"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:gravity="center"
                android:layout_alignParentRight="true"
    

3) create another class file MyBaseAdapter.Java which extend BaseAdapter class.

  • Create Constructor with Parameterized Context & ArrayList)

    override fillowing  methods of BaseAdapter getCount(),getItem(int position),getItemId(int position),getView(final int position, View convertView, ViewGroup parent)


    public class MyBaseAdapter extends BaseAdapter {
    // This is the Array of the int to get the image from the res/drawable folder
    int image[] = {R.drawable.i,R.drawable.j,R.drawable.k,R.drawable.l,R.drawable.m,R.drawable.n,R.drawable.o,       R.drawable.p,R.drawable.q,R.drawable.r,R.drawable.s,R.drawable.t,R.drawable.u,R.drawable.v,R.drawable.w};

        Context context;
        ArrayList alList;
    //This is the constructor to get the Activity Context & the information which you want to display in the ListView
        public MyBaseAdapter(Context context , ArrayList alList){
            this.alList= alList;
            this.context=contex;
        }
    //This method tell that how many row Base Adapter should create
        @Override
        public int getCount() {
            return alList.size();
        }
        @Override
        public Object getItem(int position){
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    //This is the inner class to hold the whole object of cell(row)of ListView
        private class Hold{
            ImageView iv;
            TextView tv1,tv2;
            Button btnDelete;
        }
    //This method create a cell(row) view & return to the ListView
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Hold hold;
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater)                      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row, null);
                hold = new Hold();
                hold.iv = (ImageView) convertView.findViewById(R.id.imageView1);
                hold.iv.setFocusable(true);
                hold.tv1 = (TextView) convertView.findViewById(R.id.textView1);
                hold.tv2 = (TextView) convertView.findViewById(R.id.textView2);
                hold.btnDelete=(Button) convertView.findViewById(R.id.btnDelete);
                convertView.setTag(hold);
            }
            else
            {
                hold = (Hold) convertView.getTag();
            }
            hold.iv.setImageResource(image[position]);
            hold.tv1.setText(alList.get(position).getName());
            hold.tv2.setText(alList.get(position).getCity());
            hold.btnDelete.setText("Delete");
            return convertView;
        }
    }
    // the above method first check view is null then & then only create new view
    otherwise it will recycle the view which is already created & get that object by tag

    4) Now,bind Listview with its object in MainActivity.java as follow:

    public class MainActivity extends Activity {
        ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            listView = (ListView) findViewById(R.id.listView1);
            ArrayList data = new ArrayList();
            data.add(new Data("Hardik", "Surat"));
            data.add(new Data("Jignesh", "Ahmedabad"));
            data.add(new Data("Umang", "Ahmedabad"));
            data.add(new Data("Dashrath", "Rajkot"));
            data.add(new Data("Amit", "Kadi"));
            data.add(new Data("Bhautik", "Surat"));
            data.add(new Data("Archit", "Baroda"));
            data.add(new Data("Maulik", "Gandhinagar"));
            MyBaseAdapter adapter = new MyBaseAdapter(this, data);
            listView.setAdapter(adapter);
        }
    }

    OutPut:-



     



No comments:

Post a Comment

Must Comment