Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:23:35+00:00 2026-06-07T04:23:35+00:00

I have created a ListView containig CheckedTextView. I’m feeding the elements in this ListView

  • 0

I have created a ListView containig CheckedTextView.
I’m feeding the elements in this ListView via a custom ArrayAdapter.
the list is displayed fine, but the checkbox is not responding when I select the list item

Code:

MainActivity class:

public class MainActivity extends ListActivity {

    ArrayList<String> m_list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncHandler(this).execute();
    }


    public class AsyncHandler extends AsyncTask {

        Context context;

        public AsyncHandler(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            getList();
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            // super.onPostExecute(result);
            setListAdapter(new ElementAdapter(context, m_list));
        }

        private void getList() {
            m_list = new ArrayList<String>();
            m_list.add("Ele 1");
            m_list.add("Ele 2");
            m_list.add("Ele 3");
            m_list.add("Ele 4");
        }
    }

}

Adapter class

public class ElementAdapter extends ArrayAdapter implements OnClickListener {

    ArrayList<String> items = new ArrayList<String>();
    Context context;
    CheckedTextView tvElement;

    public ElementAdapter(Context c, ArrayList<String> elements) {
        super(c, R.layout.row, elements);
        this.items = elements;
        this.context = c;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.row, parent, false);
        tvElement = (CheckedTextView) view
                .findViewById(R.id.ctvElements);
        tvElement.setText(items.get(position));
        tvElement.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.ctvElements:
            if(!tvElement.isChecked()){
                tvElement.setChecked(true);
                tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
            }else{
                tvElement.setChecked(false);
                tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
            }
            notifyDataSetChanged();
        }
    }
}

Row layout:

<?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="horizontal">

    <CheckedTextView android:id="@+id/ctvElements"
        android:paddingLeft="20dip" android:paddingRight="20dip"
        android:paddingTop="10dip" android:paddingBottom="10dip"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background"
        android:focusable="false" />

    <!-- <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:focusable="false" /> <TextView 
        android:id="@+id/tvElement" android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="Element" android:textSize="25sp" /> -->

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:23:37+00:00Added an answer on June 7, 2026 at 4:23 am

    If you only want to add checkmarks to each row then use ListView.setChoiceMode(). The checkmarks will respond to a click anywhere in the row without adding any onClickListeners or custom Adapters.

    Add this to your onCreate:

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    

    Then use the built-in ArrayAdapter:

        @Override
        protected void onPostExecute(Object result) {
            // super.onPostExecute(result);
            setListAdapter(new ArrayAdapter<String>(context, R.layout.row, m_list));
        }
    

    You can use the default checked row layout with android.R.layout.simple_list_item_checked.

    Otherwise if you want some customization the ListView rows change row.xml to this:

    <CheckedTextView android:id="@android:id/text1"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:layout_width="fill_parent"
            android:checkMark="@android:drawable/checkbox_off_background"
            android:gravity="center_vertical" 
            android:paddingLeft="20dip" 
            android:paddingRight="20dip"
            android:paddingTop="10dip" 
            android:paddingBottom="10dip"
            />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI Guyz i have created a sectioned custom listview using baseadapter its working fine
We have created custom listview by using the Base adapter. List view contains Text
I have created a custom listview using adapter. Each row of the list view
I have created an xml file like this: <?xml version=1.0 encoding=utf-8?> <ListView xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent
I have created a specific List which exists out of the following elements to
I have created a ListView which contains a list of items. Each item in
I have a ListView than contains a list of image created from web link
I have created a custom listview that is populated using a row.xml file. Each
I have a listview that contain several EditTexts created dynamically according to the number
I have create a listview of Contact numbers , the list item contains the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.