Why does my listview only respond when it's text is clicked?

I have a custom listview. The problem is, however, the only part of the listview that is clickable is the text (nowhere to the right/ empty space). I have also added some of my java code.

This is the xml file I use in the arrayAdapter:

?xml version="1.0" encoding="utf-8"?>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text"
    android:maxLines="1"
    android:ellipsize="end"
android:textSize="50sp">

</TextView>

MainActivity.java

    public class MainActivity extends AppCompatActivity {
static ArrayList<String> note;
ListView listView;
static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
static  Set<String> set;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

setTitle("Notely");
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {





        notes.add("");

        SharedPreferences sharedPreferences =  getSharedPreferences("mypackage", Context.MODE_PRIVATE);

        if (set == null) {

            set = new HashSet<String>();

        } else {

            set.clear();

        }

        set.addAll(notes);
        arrayAdapter.notifyDataSetChanged();

        sharedPreferences.edit().remove("notes").apply();
        sharedPreferences.edit().putStringSet("notes", set).apply();

        Intent i = new Intent(getApplicationContext(), EditNote.class);
        i.putExtra("noteId", notes.size() - 1);
        startActivity(i);






    }
});


ListView listView = (ListView) findViewById(R.id.listView);

 SharedPreferences sharedPreferences = this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);

set = sharedPreferences.getStringSet("notes", null);

notes.clear();

if (set != null) {

    notes.addAll(set);

} else {

    notes.add("Example note");
    set = new HashSet<String>();
    set.addAll(notes);
    sharedPreferences.edit().putStringSet("notes", set).apply();

}


arrayAdapter = new ArrayAdapter(this, R.liststyle, notes);

listView.setAdapter(arrayAdapter);

listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,  long id) {

        Intent i = new Intent(getApplicationContext(), EditNote.class);
        i.putExtra("noteId", position);
        startActivity(i);


    }

});



 listView.setOnItemLongClickListener(new  AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final  int position, long id) {

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Are you sure?")
                .setMessage("Do you want to delete this note?")
                .setPositiveButton("Yes", new  DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        notes.remove(position);

                        SharedPreferences sharedPreferences =         MainActivity.this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);

                        if (set == null) {

                            set = new HashSet<String>();

                        } else {

                            set.clear();

                        }

                        set.addAll(notes);
                        sharedPreferences.edit().remove("notes").apply();
                        sharedPreferences.edit().putStringSet("notes", set).apply();
                        arrayAdapter.notifyDataSetChanged();

                    }
                })
                .setNegativeButton("No", null)
                .show();

        return true;
    }
});



}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement


return super.onOptionsItemSelected(item);
}
}

Answers


To react to selections in the list, set an OnItemClickListener to your ListView. like below, for more about ListView

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),
      "Click ListItem Number " + position, Toast.LENGTH_LONG)
      .show();
  }
}); 

Hi you can use onItemclicklistener and if you look alternate solution then you can click on main layout on adapter.

listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) {

} });


Set the TextView width to match_parent


Need Your Help

Find Columns in range

excel vba vsto excel-interop

Is it possible to know which columns are present in a range of given Columns

How can I prevent bitwise OR combinations of enum values?

c# enums flags

I know that you can use FlagsAttribute to instruct the compiler to use bitfields for an enumeration.