2

I have a gridview with items consisting of different views, among which an ImageButton which may or may not be present (depending on the case). I implemented onItemClick for each item inside the grid, but I also need to have a toast showing up when the info button is present (and pressed). Everything worked fine, except the fact that the items which contain the info button will not respond to onItemClick anymore, they only respond to the info icon being pressed. How can I make those items respond accordingly both to click on the info button and on other areas?

Here is the code for onItemClick():

@Override
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
        getDayStatus((Date) _monthVectorList.get(0).get(arg2));
        _listenerDateSelect.onDispatchDateSelect((Date) _monthVectorList.get(0).get(arg2)); 
}

and here is the imageButton for Info:

<ImageButton
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:id="@+id/tv_day_info"
    android:layout_gravity="center"
    android:gravity="right|center"
    android:layout_marginLeft="1dp"
    android:onClick="infoToast"
    android:focusable="false"
    />

Also, here is the code for info toast:

public void infoToast(View view){
        Date date = (Date) view.getTag();
    List<String> dayDescription = getDayStatus(date);
    Context context = (Context) getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    CharSequence text = (String) dayDescription.get(2);

    Toast.makeText(context, text, duration).show();
    }

I tried setting the focus off to the imageButton, but it still does not work. I attach an image to get a better depiction of the issue: http://oi42.tinypic.com/2rcxrv6.jpg

3 Answers3

4

add this to root layout of ImageButton

android:descendantFocusability="blocksDescendants"
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
  • I have two buttons in a gridview. I want to focus one by one. Can you please help me? – Bebin T.N Oct 27 '15 at 07:21
  • @BebinT.N what's your exact problem ? Also I would suggest you to ask separate question. – Ketan Ahir Oct 27 '15 at 07:23
  • when i give android:descendantFocusability="blocksDescendants". Its focusing all buttons in the root layout. but I want one by one focus button. – Bebin T.N Oct 27 '15 at 07:28
0

try with:

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

inside imagebutton

codareee
  • 570
  • 2
  • 8
  • 19
0

That because your "info Button" has gained focus form your gridview item, you can only click on the button, in this case is 'info Button'. However, you could also click on your gridview item by set the "info Button" NOT focusable. Try this:

<Button
        android:id="@+id/your_info_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="info Button"
        android:focusable="false"
        android:focusableInTouchMode="false" />

Hope this helps.

user2652394
  • 1,686
  • 1
  • 13
  • 15
  • It makes sense what you are saying. As you have seen, I tried setting the focus to false. At your suggestion, I now tried adding the line with focusableInTouchMode. I was really hoping this would work, but unfortunately, it does not. Any other suggestions, please? – Elena Creangă Aug 09 '13 at 08:54