Search code examples
androidlistviewandroid-listviewlistadapter

Change the selected item font size in listview android


In my list view inflator i have an image and a textview. When the user selects the any item in the listview, I want to increase the font size of the text in the textview. I am using a custom adapter. Can I do this in getview method ? And also is it possible to do this without calling notifydatasetchanged() always ?


Solution

  • You can use setTag() and getTag().

    setTag() in your adapter's getView()

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        convertView=  getLayoutInflater().inflate(R.layout.layoutfile, null);
        TextView mTextViewNumber= (TextView) convertView.findViewById(R.id.TvId);
        ImageView mImageView= (ImageView) convertView.findViewById(R.id.ImgId)
        mTextViewNumber.setText("Hi hello");
        mImageView.setBackgroundResource(R.x.xxxx);
        convertView.setTag(mTextViewNumber, R.id.TvId);
        convertView.setTag(mImageView, R.id.ImgId);
        return convertView;
    }
    

    Change the Textsize by getTag() OnItemClickListener().

    mListView=(ListView) findViewById(R.id.list_call_log_listview);
    mListView.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {
            TextView mTextView= (TextView) view.getTag(R.id.TvId);
            mTextView.setTextSize(20);
            ImageView mImageView= (ImageView) view.getTag(R.id.ImgId);
            mImageView.setBackgroundResource(R.x.xxxx);
        }
    
    });