CaptainNemo CaptainNemo - 1 month ago 29
Android Question

Floating Action Button does not raise OnClick event

I have the Following layout:

<?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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/rlMap">
<org.mapsforge.map.android.view.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/windArrow"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_gravity="top|right|end"
android:src="@drawable/arrow"
android:layout_alignStart="@+id/fab" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/settings"
android:clickable="true"/>
</RelativeLayout>


And the following code in the OnCreate method:

...
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG,"FAB Setting Clicked");
Intent i = new Intent(getApplicationContext(), AppPreferences.class);
startActivity(i);
}
});
...


But when I press the button nothing happens, The intent does not run, and the log is not written.

The button animation is working and every time I press the button the following log message appears :


D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN


So there seems to be some reaction.

What am I doing wrong?

Answer

The solution suggested by @Tasos worked:

add a click event inside the xml e.g

android:onClick="runThis"

and then in the Activity add

public void runThis(View v) { ..... }
Comments