Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add title, subtitle and Image in List View

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 5.74k
    Comment on it

    In the below example I have created  a  ListView in ListView I have added title, subtitle and image. Here first I have added ListView in activity_main.xml layout, then I have created new list_item.xml layout here I have created RelativeLayout, In RelativeLayout I have added ImageView and two TextView. In next step I have created two class first is RowData and second is MyAdapter. MyAdapter extend with BaseAdapter, after then next step: In MainActivity I have used final string method. You can see below program it will clearly describe  you "How to add title, subtitle and Image in List View".

    Step(1)activity_main.xml-

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

    Step(2)-Created a new list_item.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/icon"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/image"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/icon"
            android:paddingBottom="10dp"
            android:textColor="#CC0033"
            android:textSize="16dp" />
    
        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/icon"
            android:paddingLeft="10dp"
            android:textColor="#3399FF"
            android:textSize="14dp" />
    </RelativeLayout>

    Step(3)- Created a new RowData class

    public class RowData {
        private int imageId;
        private String title;
        private String subtitle;
    
        public int getImageId() {
            return imageId;
        }
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
        public String getSubtitle() {
            return subtitle;
        }
        public void setSubtitle(String subtitle) {
            this.subtitle = subtitle;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    
    }

    Step(4)- Created MyAdapter class-

    public class MyAdapter extends BaseAdapter {
    
        Context context;
        List<RowData>rowData;
    
        public MyAdapter(Context context, List<RowData> items) {
            this.context = context;
            this.rowData = items;
        }
    
        /*private view holder class*/
        private class ViewHolder {
            ImageView imageView;
            TextView txtTitle;
            TextView txtDesc;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();
                holder.txtDesc = (TextView) convertView.findViewById(R.id.subtitle);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
                holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            RowData rowItem = (RowData) getItem(position);
    
            holder.txtDesc.setText(rowItem.getSubtitle());
            holder.txtTitle.setText(rowItem.getTitle());
            holder.imageView.setImageResource(rowItem.getImageId());
    
            return convertView;
        }
    
        @Override
        public int getCount() {
            return rowData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return rowData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return rowData.indexOf(getItem(position));
        }
    }
    

    Step(5)-MainActivity-

    public class MainActivity extends AppCompatActivity {
    
    
        public static final String[]titles =new String[]{"GOOGLE","FACEBBOK","TWITTER"};
    
        public static final String[]subtitle=new String[]{"google is large company","Facebook is a socialsite networking","Twitter is indian company"
        };
        public static final Integer[] images = { R.drawable.one,
                R.drawable.two, R.drawable.three };
    
        ListView list;
        List<RowData> rowData;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    
            rowData =new ArrayList<RowData>();
            for(int i=0;i<titles.length;i++){
                RowData data =new RowData();
                data.setImageId(images[i]);
                data.setSubtitle(subtitle[i]);
                data.setTitle(titles[i]);
                rowData.add(data);
    
    
            }
    
            list = (ListView) findViewById(R.id.list);
            MyAdapter adapter = new MyAdapter(this, rowData);
            list.setAdapter(adapter);
    
    
        }
    
    }
    

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: