Hello,
My end result is to increase the clickable area of certain views that are small. It appears that this can be done with a TouchDelegate, but in my case, there will be scenarios where one container has multiple views that I would like to increase the hit area for. Comments from this Stack Overflow question suggest this method can only be done for a single view inside a ViewGroup.
So, I've attempted to override getHitRect on a subclass of View, to increase the area. The example program below attempts to make the area 100px above and below a button clickable. The results are as follows:
- Emulator running 2.2: The area above and below is clickable, clicking that area makes toast.
- Nexus One running 2.3.6: The getHitRect method gets called, but the click method is not fired when clicking above/below the button.
- Galaxy Nexus running 4.0.4: The getHitRect method never gets called, and hence, no click event when clicking above/below the button.
My question is twofold:
1) Is there a more elegant way to increase the click area of multiple views inside one ViewGroup?
2) Just out of curiosity, how is the getHitRect method supposed to work/be used? Is it deprecated?
Here's the short example:
public class TestActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* simply create a layout with some whitespace surrounding an instance of my custom button */
LinearLayout container = new LinearLayout(this);
container.setGravity(Gravity.CENTER_VERTICAL);
container.setOrientation(LinearLayout.VERTICAL);
TestButton button = new TestButton(this);
button.setText("Make Toast");
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(TestActivity.this, "button was clicked", Toast.LENGTH_SHORT).show();
}
});
container.addView(button);
setContentView(container);
}
public static class TestButton extends Button
{
public TestButton(Context context)
{
super(context);
}
@Override
public void getHitRect(Rect outRect)
{
super.getHitRect(outRect);
/* just an example, for test purposes */
outRect.top -= 100;
outRect.bottom += 100;
Log.d("TestActivity", "getHitRect called, value: " + outRect.toShortString());
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment