0

I have the following layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:text="Height" 
        android:layout_height="wrap_content" 
        android:id="@+id/buttonHeight" 
        android:layout_width="wrap_content"
        android:layout_weight="15"
        android:onClick="OnClickHeight">
    </Button>
    <Button 
        android:text="Width" 
        android:layout_height="wrap_content" 
        android:id="@+id/buttonWidth" 
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/buttonHeight"
        android:layout_weight="1"
        android:onClick="onClickWidth">
   </Button>
 </LinearLayout>
 <ListView android:id="@android:id/list"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:dividerHeight="1px"
 android:drawSelectorOnTop="false"/>
 </RelativeLayout>

I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:

 public void onClickWidth(View v) 
 {
   // does stuff
 }

However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!

I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?

lost_bits1110
  • 157
  • 1
  • 5
  • 18

5 Answers5

0

set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>
comeGetSome
  • 1,913
  • 19
  • 20
0

Can you tell us what are you try to do? And why do you need ListActivity?

Sorry for edditing:P

You could take a look here: How to handle ListView click in Android

Community
  • 1
  • 1
Cata
  • 11,133
  • 11
  • 65
  • 86
  • I need to show a list of some data, which is why I use ListActivity. I use this with a class that extends BaseAdapter to populate this list. – lost_bits1110 May 26 '11 at 18:48
  • The above link is not relevant for me, as I know how to detect clicks on the ListView, however it is the Button that is not detecting the onClick. – lost_bits1110 May 26 '11 at 18:55
0

put OnClicklistener within adapter itself .

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • unfortunately this did not work - I tried putting OnClickListener in the constructor of the adapter, and also tried it in the getView method, but no luck. – lost_bits1110 May 26 '11 at 20:02
0

Rather then using the onClick stuff in the layout, have you tried something like this:

Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        //do stuff
    }
});

Then do the same for buttonHeight.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • Hi, thanks for the suggestion but yes I've tried this as well, i.e. I placed this in the OnCreate of my ListActivity class, but still no luck. I also tried 'implement OnClickListener' on my ListActivity class together with overriding the OnClick method, this doesn't work either :( – lost_bits1110 May 26 '11 at 19:35
0
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_land);

            Button btn = (Button) findViewbyid(R.id.buttonWidth);

            btn.setOnClickListener(this);
}
public void onClickWidth(View v) 
{
      if(v == btn){
            //your code...
      }
}
Mayur Bhola
  • 735
  • 5
  • 17