not clickable button in ListView



  • ListView consists of pictures and buttons. Clickable is just a picture, there's nothing going on with the button. How do you make the click all over the element, that's the button and the picture at the same time? my_item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:descendantFocusability="blocksDescendants"
    >
    

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_toLeftOf="@+id/imageArrow"
    android:layout_toStartOf="@+id/imageArrow"
    />

    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageArrow"
    android:layout_alignTop="@+id/button"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/r_arr" />

    MainActivity.java

     public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    ListView listView = (ListView)findViewById(R.id.listView);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2" };
    MyCustomAdapter adapter = new MyCustomAdapter(this, values);
    setListAdapter(adapter);
    }

    @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
    if (id == R.id.action_settings) {
        return true;
    }
    
    return super.onOptionsItemSelected(item);
    

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_SHORT).show();
    }
    }

    MyCustomAdapter.java

    public class MyCustomAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;

    static class ViewHolder {
    public TextView myButton;
    public ImageView image;
    }

    public MyCustomAdapter(Activity context, String[] names) {
    super(context, R.layout.my_item, names);
    this.context = context;
    this.names = names;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.my_item, null);
    // configure view holder
    ViewHolder viewHolder = new ViewHolder();
    viewHolder.myButton = (Button)rowView.findViewById(R.id.button);
    viewHolder.image = (ImageView) rowView.findViewById(R.id.imageArrow);
    viewHolder.myButton.setFocusable(false);

        rowView.setTag(viewHolder);
    
    }
    
    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names[position];
    String abs=Integer.toString(position + 1);
    holder.myButton.setText(s+ " number " + abs);
    
    
    holder.image.setImageResource(R.drawable.r_arr);
    return rowView;
    

    }
    }



  • Add for Button and ImageView atribut: android:clickable="false"He's saying "active" elements (sniffs, switches, etc.) on default, and he's making clicks intercepted.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2