Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Use Multiple TouchDelegate
    text
    copied!<p>i have two ImageButtons, each inside a RelativeLayout and these two RelativeLayouts are in another RelativeLayout, i want to set TouchDelegate for each ImageButton. If normally i add TouchDelegate to each ImageButton and it's parent RelativeLayout then just one ImageButton works properly, Another one doesn't extend it's clicking area. So PLease help me on how to use TouchDelegate in both ImageButtons. If it's not possible then what can be a effective way to extend the clicking area of a view? Thanks in advance ........</p> <p>Here is my xml code:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout android:id="@+id/FrameContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;RelativeLayout android:layout_alignParentLeft="true" android:id="@+id/relativeLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;RelativeLayout android:layout_alignParentLeft="true" android:id="@+id/relativeLayout1" android:layout_width="113dip" android:layout_height="25dip"&gt; &lt;ImageButton android:id="@+id/tutorial1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:src="@drawable/tutorial" /&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:layout_alignParentLeft="true" android:id="@+id/relativeLayout2" android:layout_width="113dip" android:layout_height="25dip" android:layout_marginLeft="100dip"&gt; &lt;ImageButton android:id="@+id/tutorial2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:src="@drawable/tutorial" android:layout_marginLeft="50dip" /&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p></p> <p>My Activity class :</p> <pre><code>import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.view.TouchDelegate; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; public class TestTouchDelegate extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View mParent1 = findViewById(R.id.relativeLayout1); mParent1.post(new Runnable() { @Override public void run() { Rect bounds1 = new Rect(); ImageButton mTutorialButton1 = (ImageButton) findViewById(R.id.tutorial1); mTutorialButton1.setEnabled(true); mTutorialButton1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 1", Toast.LENGTH_SHORT).show(); } }); mTutorialButton1.getHitRect(bounds1); bounds1.right += 50; TouchDelegate touchDelegate1 = new TouchDelegate(bounds1, mTutorialButton1); if (View.class.isInstance(mTutorialButton1.getParent())) { ((View) mTutorialButton1.getParent()).setTouchDelegate(touchDelegate1); } } }); //View mParent = findViewById(R.id.FrameContainer); View mParent2 = findViewById(R.id.relativeLayout2); mParent2.post(new Runnable() { @Override public void run() { Rect bounds2 = new Rect(); ImageButton mTutorialButton2 = (ImageButton) findViewById(R.id.tutorial2); mTutorialButton2.setEnabled(true); mTutorialButton2.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 2", Toast.LENGTH_SHORT).show(); } }); mTutorialButton2.getHitRect(bounds2); bounds2.left += 50; TouchDelegate touchDelegate2 = new TouchDelegate(bounds2, mTutorialButton2); if (View.class.isInstance(mTutorialButton2.getParent())) { ((View) mTutorialButton2.getParent()).setTouchDelegate(touchDelegate2); } } }); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload