Android FloatingActionButton onclick is just on background and not main button - android

Hey all this is driving me crazy. Been at this for hours without any luck.
What I am trying to do is detect when the user clicks on the main floatingActionButton to open it and of course when they click on it to close.
My XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/theVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:orientation="horizontal">
[More XML here]
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/floatingBtnMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:paddingStart="890dp"
android:paddingLeft="890dp"
android:paddingRight="50dp"
android:paddingBottom="30dp"
android:elevation="3dp"
app:elevation="3dp"
fab:menu_backgroundColor="#ccffffff"
fab:menu_labels_ellipsize="end"
fab:menu_labels_position="left"
fab:menu_labels_singleLine="true">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingBtnCloseVid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_label="Close Video"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingBtnPausePlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
app:backgroundTint="#3BFD6A"
app:fab_colorNormal="#3BFD6A"
fab:fab_colorRipple="#faff99"
fab:fab_label="Pause Video"
fab:fab_shadowColor="#66000000"
fab:fab_showShadow="true"
fab:fab_size="normal"
fab:menu_labels_colorRipple="#faff99"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="true" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingBtnVolumeUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_label="Skip Forward"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingBtnVolumeDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_label="Skip Back"
fab:fab_size="normal" />
</com.github.clans.fab.FloatingActionMenu>
[More XML here]
</RelativeLayout>
And my Java code:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout._fragvideoplay, container, false);
vidView = (VideoView)rootView.findViewById(R.id.videoView);
String path = "android.resource://" + BuildConfig.APPLICATION_ID + "/raw/" + passMP4Name;
vidView.setVideoURI(Uri.parse(path));
sBar = (SeekBar) rootView.findViewById(R.id.seekBar);
TextView tw = (TextView) rootView.findViewById(R.id.timeLeft);
final FloatingActionMenu floatingBtnMenu = (FloatingActionMenu) rootView.findViewById(R.id.floatingBtnMain);
final FloatingActionButton closeVid = (FloatingActionButton) rootView.findViewById(R.id.floatingBtnCloseVid);
final FloatingActionButton pausePlayVid = (FloatingActionButton) rootView.findViewById(R.id.floatingBtnPausePlay);
final FloatingActionButton floatingBtnVolumeUp = (FloatingActionButton) rootView.findViewById(R.id.floatingBtnVolumeUp);
final FloatingActionButton floatingBtnVolumeDown = (FloatingActionButton) rootView.findViewById(R.id.floatingBtnVolumeDown);
try {
pausePlayVid.setImageDrawable(Drawable.createFromStream(
getResources().getAssets().open("icons/pause.png"),
null
));
closeVid.setImageDrawable(Drawable.createFromStream(getResources().getAssets()
.open("icons/closeVid.png"),null
));
} catch (IOException e) {
e.printStackTrace();
}
floatingBtnMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This only fires if I click anywhere on the screen
Log.d("tesing", "was clicked");
}
});
closeVid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_stop();
floatingBtnMenu.toggle(true);
}
});
[more code here]
return rootView;
}
As my comment in my code says, the only time the onClick() fires off is when I click on any part of the screen. When I just click on the main floatingActionButton it never fires off that onClick()). The same goes with clicking it again to close it.
So what am I missing? Why does the onClick() only fire on the whole fragment and not just on the floatingActionButton itself?

The reason this is happening I believe is because of how your XML is set up, let's take a look at this part of the code here.
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/floatingBtnMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:paddingStart="890dp"
android:paddingLeft="890dp"
android:paddingRight="50dp"
android:paddingBottom="30dp"
android:elevation="3dp"
app:elevation="3dp"
fab:menu_backgroundColor="#ccffffff"
fab:menu_labels_ellipsize="end"
fab:menu_labels_position="left"
fab:menu_labels_singleLine="true">
If you focus here, on how the width and height is setup:
android:layout_width="match_parent"
android:layout_height="match_parent"
As you can see your width and height is set to match_parent which in turns means it is covering the full screen.
That is when you click on the full screen, it acts up. I suggest you take another look at this and maybe change it to wrap_content instead or however you want to set it up.

Related

Bottom Sheet dialog landscape width issue

bottom_sheet_image_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="62dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/llAddDriverPic"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/baseline_add_photo_alternate_24"
android:layout_margin="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/add_picture"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/llRemoveDriverPic"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/baseline_no_photography_24"
android:layout_margin="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/remove_picture"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
</LinearLayout>
Code implementation:
BottomSheetDialog bsDialog = new BottomSheetDialog(getContext());
bsDialog.setContentView(R.layout.bottom_sheet_image_dialog);
bsDialog.setOnShowListener(dialog -> {
BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
});
Following issue which occurs: The width, as seen in the video won't appear fully.
If I start the bottomsheet in the beginning in landscape mode the width also looks different as the left and right side are not covered:
What may the issue(s) be?
Is it necessary to predefine the width before showing the dialog?
-> There's no problem with your xml layout, It can be solved in Code. I am providing an example below which solved this issue.
=> Bottom Sheet Class Code
public class BottomSheet extends BottomSheetDialogFragment {
private View view;
private BottomSheetBehavior mBottomBehavior;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
//Do your logic code here
return view;
}
#Override
public void onStart() {
super.onStart();
mBottomBehavior = BottomSheetBehavior.from((View) view.getParent());
mBottomBehavior.setMaxWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mBottomBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
=> Activity Class Code
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
//To open the bottom sheet, NB. make sure 'BottomSheet' is the name you declared Bottom sheet class
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
});

How to grow/shrink a DialogFragment when dynamically showing/hiding an EditText?

I have a very simple Dialog:
On click of the button I would like to toggle visibility of the second EditText:
public class SomeDialog extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.some_dialog, container, false);
EditText editSecond = root.findViewById(R.id.text_second);
Button buttonOk = root.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(v -> {
editSecond.setVisibility(editSecond.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
});
return root;
}
}
I would like it to be animated smoothly, so I set "animateLayoutChanges" to "true":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true"
>
<TextView
android:id="#+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:padding="8dp"
android:text="Title"
android:textAppearance="#style/TextAppearance.AppCompat.Large.Inverse"
android:textSize="18sp"
/>
<EditText
android:id="#+id/text_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First"
/>
<EditText
android:id="#+id/text_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Second"
/>
<Button
android:id="#+id/button_ok"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:text="Ok"
/>
</LinearLayout>
The dialog gets animated, but unfortunately not in the correct order.
What happens:
the TextEdit vanishes
the Dialog height snaps (instantly) to the smaller height
the Button moves up (animated)
So between 2) and 3) the button is not visible and slowly slides into the (already smaller) dialog from offscreen (bottom).
What can be done about this?
(Code Sample works and can be copy/pasted into Android Studio as is)

FloatingActionButton inside framelayout not working

I want to display text instead of icon on FloatingActionButton. Hence, I made use of FrameLayout to achieve this. However, none of the onClickListerners are working. I have setandroid:clickable="true" still no change.
Below is my sample layout file:
<FrameLayout
android:id="#+id/frame"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="bottom|end"
android:clickable="true"
android:layout_margin="#dimen/activity_horizontal_margin">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/text"
android:layout_width="30dp"
android:layout_height="match_parent"
android:elevation="7dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:maxLines="3"
android:text="My Text"
android:clickable="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textSize="8sp"
android:textStyle="bold" />
</FrameLayout>
I tried using both methods:
1. setting android:onClick="myMethod"
2. setting onClickListener programmatically on all the three elements i.e. FrameLayout, FAB and TextView inside Activity class
However, i use FAB normally, i.e. without using FrameLayout; it is working fine.
Please provide some solution.
EDIT : Activity.java
fab = (FloatingActionButton) findViewById(R.id.fab);
text= (TextView) findViewById(R.id.text);
frame = (FrameLayout) findViewById(R.id.frame);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMethod();
}
});
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMethod();
}
});
frame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMethod();
}
});
Logger implementation present inside myMethod(). It seems onClick event is not getting triggered, as I cannot see any log in the console.
Update::
The layout file also contains RecyclerView at the same level that of FrameLayout. Does the problem occur due to presence of RecyclerView?
The problem is resloved. I set android:layout_anchor on FrameLayout to recycler view id. Now the button is clickable.

Android Button Losing onClickListener When Keyboard Opens

I have a EditText field and a "Save" Button in my Android layout.
When the Fragment is created, I assign an onClickListener to the Button. The Button works correctly before the keyboard opens and after the keyboard closes, but seems to lose its onClickListener while I am editing the EditText.
Is there somewhere I need to reassign the onClickListener so it is available while the keyboard is open?
Here is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/add_comment_details"
android:text="#string/done"
android:id="#+id/done_button"
android:textColor="#color/red_1"
android:layout_alignParentBottom="true"
android:textSize="#dimen/text_size_10"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_field2"
android:layout_above="#id/done_button"
android:hint="#string/enterText2"
android:gravity="start"
android:background="#drawable/add_comment_entertext"
android:padding="20dp"
/>
</RelativeLayout>
Here's the code, in a Fragment where I'm adding the onClickListener
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_addcommentfragment, null);
text = (EditText) view.findViewById(R.id.text_field2);
done = (Button) view.findViewById(R.id.done_button);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("MyFragment","onClick");
doneListener.onClick(saveAndClose);
}
});
return view;
}
I am also using android:windowSoftInputMode="adjustResize" in the Manifest so the EditText and the Button both stay on the screen when the keyboard opens.
Some more weirdness, if I switch the layout to this, the button works, but the button takes up most of the screen:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_field2"
android:hint="#string/enterText2"
android:gravity="start"
android:background="#drawable/add_comment_entertext"
android:padding="20dp"
/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/add_comment_details"
android:text="#string/done"
android:id="#+id/done_button"
android:textColor="#color/red_1"
android:layout_below="#id/text_field2"
android:layout_alignParentBottom="true"
android:textSize="#dimen/text_size_10"/>

ImageView onClickListener is not working

I have two ImageViews, one text field and one WebView in a LinearLayout. My ImageView listener is not working. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="back"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/back_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="forward"
android:src="#drawable/back_icon"/>
<EditText
android:id="#+id/adressBar"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ems="10" >
</EditText>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is my Java code. I should point out that the Log.d statement is not firing, so it is definitely not getting called:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState == null) {
L.d(">>onCreateView", "savedInstanceState null");
}
View v = inflater.inflate(R.layout.browser,null);
mWebView = (WebView) v.findViewById(R.id.webView);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
return v;
}
try this
v = inflater.inflate(R.layout.browser, container, false);
ImageView back = (ImageView)findViewById(R.id.imageVIew1);
back.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
try this-
back.setOnClickListener(this);
and implement onclicklistener..
You need to inflate the view like
v = inflater.inflate(R.layout.browser, container, false);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new View.OnClickListener())
{
..
}
set to focusable false for all the views in your xml file. or setOnClickListener for imageview in onActivityCreated() method in your fragment.
I had a similar issue and fixed it by adding android:layout_width="44dp" to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="#+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="#id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/icn_edit_small_white"/>

Categories

Resources