Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an item in a list view non clickable in Android

How to make the items in a list view not click able. i got topics and items in a list view but the view is same for both topics and items. the items are click able but the topic is not click able. how to achieve this

the list will look like

Topic
item
Topic
item
item

topic. click able(false) did not work, please help

like image 838
learner Avatar asked Sep 12 '11 15:09

learner


People also ask

How do you make a view not clickable on Android?

To make your relativelayout block touches and clicks for all of its children you simply need to set the onTouchListener, like this: YOUR_RELATIVE_LAYOUT. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // ignore all touch events return true; } });

How do I edit list view in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };

How do you customize a list view with an example app?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above activity_main.


4 Answers

Don't know if you still need it, but you can implement your own Adapter and override the method isEnabled(int position). Depending on the ViewType of the item you will return true or false.

like image 177
Filip Majernik Avatar answered Oct 06 '22 00:10

Filip Majernik


Sharing my experience, the following did the trick (view refers to the list item view):

view.setEnabled(false); view.setOnClickListener(null); 
  • enabling by overriding the method didn't work as the method was never invoked.
  • setting focusable to false didn't work as well.
like image 41
AlikElzin-kilaka Avatar answered Oct 05 '22 23:10

AlikElzin-kilaka


To make the items in a list non-clickable, you have to make the adapter return false on its isEnabled method for the items in the list. An easy way to instantiate an adapter and override isEnabled can be done in the following way:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0) {
    @Override
    public boolean isEnabled(int position) {
        return false;
    }
};
like image 32
Daniel Jonsson Avatar answered Oct 05 '22 23:10

Daniel Jonsson


This is the correct answer:

I've found a lot of comments saying that

setEnabled(false)
setClickable(false)
setFocusable(false)

would work, but the answer is NO

The only workaround for this approach is doing:

view = inflater.inflate(R.layout.row_storage_divider, parent, false);
view.setOnClickListener(null);
like image 29
cesards Avatar answered Oct 05 '22 22:10

cesards