0

any ideas why my ImageButton isn't working in my Fragment? The ImageButton contains a Drawable and is transparent. When I place it in my RelativeLayout below, its working...

<RelativeLayout
android:id="@+id/rl_newsdet_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_newsdet_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:layout_marginTop="7dp"
    android:textSize="20sp"
    android:layout_centerHorizontal="true"
    />

<ImageButton
    android:id="@+id/btn_newsdet_back"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/bt_back"
    />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    android:background="@color/white"
    android:gravity="left"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_newsdet_name"
        android:textSize="18sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:textColor="@color/cyan"
        android:background="@color/grey"
        android:paddingLeft="1dp"
        android:paddingRight="1dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_newsdet_time"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:layout_toRightOf="@+id/tv_newsdet_name"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:background="@color/grey"
        android:layout_marginTop="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/tv_newsdet_text"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="1dp"
    android:layout_below="@+id/tv_newsdet_name"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

My onClickListener with AndroidAnnotations

 @ViewById
 ImageButton btn_newsdet_back;


    @Click(R.id.btn_newsdet_back)
    void back() {
       Log.i(TAG,"back");
       mActivity.popFragments();
    }

EDIT onClick is working (called), when its below the TextView. Can anybody explain why?

enter image description here

Bobbelinio
  • 134
  • 1
  • 8
  • 2
    what do you mean by "not working" ? – mithrop Nov 06 '14 at 09:03
  • As I wrote in the title, onClick isn't working. For me, it seems the button is not reachable but I don't know why. – Bobbelinio Nov 06 '14 at 09:14
  • again "is not working" is not really clear. The Log is wrote, or not ? It is just the "popFragments()" that is not executed ? We cannot know what behavior you expect and what actually happened. – mithrop Nov 06 '14 at 09:21
  • onClick isn't working : onClick isn't called. Might the transparent ActionBar be a possible reason? – Bobbelinio Nov 06 '14 at 09:27
  • I asked two questions, you didn't answer even one. – mithrop Nov 06 '14 at 09:30
  • for me, I answer both - sorry. When onClick isnt called, whether the Log is written nor the fragment is popped. – Bobbelinio Nov 06 '14 at 09:33
  • check this link it will give some idea about your queries http://stackoverflow.com/questions/21903654/android-imagebutton-worked-well-in-activity-it-doesnt-work-in-fragment –  Nov 06 '14 at 10:18

5 Answers5

0

If, as you said, everything works when you put the button inside the RelativeLayout below the reason should be that the second RelativeLayout has its parameters declared as:

android:layout_width="match_parent"
android:layout_height="match_parent"

Which means it is "filling" its parent, that is the original RelativeLayout containing the button. It's like you're creating a layer that covers the button and keeps any interaction with it from working. When you click on the button, you're actually clicking on the second RelativeLayout.

I didn't actually tried this, but you should consider setting:

android:layout_below="@id/btn_newsdet_back"

On the RelativeLayout below the button.

0

You can try this code:

This is code of Fragment: TestFragment.java

public class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.fragment_one, container, false);

    ImageButton button = (ImageButton) view.findViewById(R.id.img2);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "Click on fragment ",
                    Toast.LENGTH_LONG).show();
        }
    });
    return view;
}

}

This is layout of 'TestFragment`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageButton
    android:id="@+id/img2"
    android:layout_width="100dp"
    android:layout_height="80dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

This is code of MainActivity.java

public class MainActivity extends FragmentActivity {

ImageButton imgButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    FragmentManager fragmentManager = getSupportFragmentManager();

    TestFragment fragment = (TestFragment) fragmentManager
            .findFragmentById(R.id.fragment);

    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    // fragmentTransaction.add(fragment, "");
    fragmentTransaction.commit();

}

}

This is layout of MainActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/fragment"
    android:name="com.alarmdemo.TestFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

Here ImageButton is placed in a Fragment and it is working fine.

Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • thx for your answer. As you can see at my EDIT, onClick works when its below the TextView. So in general its working in a fragment. There seems to be an overlay I don't understand. – Bobbelinio Nov 06 '14 at 11:48
  • You have two `RelativeLayout` and both having match parent in width and height. Try to change the width and height of inner `RelativeLayout` – Ashish Tiwari Nov 06 '14 at 11:54
  • setting width and height to wrap content at the inner layout has no effect. – Bobbelinio Nov 06 '14 at 11:58
0

No need to add clickable in image button .Try following:

<ImageButton
    android:id="@+id/btn_newsdet_back"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="mymethod"
    android:src="@drawable/bt_back"
    />

add in java

public void mymethod(View v)
{
  //your method
}
0

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_sample,
            container, false);
    addListenerOnButton();

}

   private void addListenerOnButton() {

    imageButton = (ImageButton) rootview.findViewById(R.id.imageButton1);

    imageButton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

           Toast.makeText(MainActivity.this,
            "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

        }



    });     
}
user1722880
  • 71
  • 2
  • 12
0

I solved it. The problem was I was using a transparent Actionbar which was the overlay I was looking for.

The following line in my onCreate() solved this:

getActivity().getActionBar().hide();
Bobbelinio
  • 134
  • 1
  • 8