I have this ListView that just needs to show data. So I don't want to make it clickable.
I have done this in one of my projects, and the sample bellow works fine.
Basically, you just need:
- a data source (ArrayList, for example)
- a ListView Widget
- an ArrayAdapter, in order to bind the data source with the ListView
MainActivity.java
package com.sample.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data source
String[] arrayData = {"Debian", "Ubuntu", "Kali", "Mint"};
// ListView Widget from a Layout
ListView listView = (ListView) findViewById(R.id.mainListView);
// an ArrayAdapter (using a layout as model for displaying the array items)
ArrayAdapter aa = new ArrayAdapter<String>(this, R.layout.main_list_item_layout, arrayData);
// binding the ArrayAdapter with the ListView Widget
listView.setAdapter(aa);
}
}
activity_main.xml
--->Make sure that the ListView Widget is using wrap_content for layout_width and layout_height
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.listview.MainActivity">
<ListView
android:id="@+id/mainListView"
android:layout_width="wrap_content" <---- HERE
android:layout_height="wrap_content" <---- HERE
/>
</LinearLayout>
main_list_item_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="20sp"
>
</TextView>
That's it: a ListView not clickable, just presenting data on the screen.
ListView.setOnClickListener(null);
OR addandroid:focusable="false" android:focusableInTouchMode="false"
OR add in the layoutandroid:listSelector="@android:color/transparent"
– Refuge