If you have an ImageButton declared in XML, just put it in a LinearLayout that also contains a TextView and set the onClickListener to LinearLayout . The structure will look like
<LinearLayout ... > <LinearLayout android:id="@+id/linLayout" ... > <ImageButton ... /> <TextView ... /> </LinearLayout> </LinearLayout>
And then in your java:
LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
If you add each ImageButton dynamically through java code, then it will still retain the same structure. Let me know if I need to add something.
source share