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)
Related
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");
});
I have dialogs in other places in the app that work just fine(keyboad doesn't hide the view) , but in this particular new dialog I wrote it doesn't move the view up when the keyboard is on. I can't figure out why..
My dialog's layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:background="#drawable/dialog_background"
android:clickable="true"
android:orientation="vertical"
custom:maxHeight="#dimen/dialog_max_height"
custom:maxWidth="#dimen/dialog_max_width" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="something" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:id="#+id/SomeEditText"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:minHeight="100dp"
android:layout_margin="15dp"
android:background="#ffffff"
android:gravity="right|top"
android:inputType="textMultiLine"
android:singleLine="false"
android:textColor="#color/edit_text_text_color"
android:textSize="16dp" />
</LinearLayout>
<Button
android:id="#+id/SomeButton"
android:layout_width="match_parent"
android:layout_height="#dimen/dialog_button_height"
android:layout_margin="15dp"
android:text="text" />
</LinearLayout>
I have
android:windowSoftInputMode="adjustPan" >
set in my manifest.
Found the solution :
I put : getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
in the onCreateDialog callback
I also faced the same issue and resolved by keeping getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
inside oncreateview
Since SOFT_INPUT_ADJUST_RESIZE is deprecated starting android 30, you should use Window.setDecorFitsSystemWindows(boolean decorFitsSystemWindows)
Here is example for DialogFragment
class MyDialog extends DialogFragment {
#Override
public void onDestroyView() {
super.onDestroyView();
getDialog().getWindow().setDecorFitsSystemWindows(true);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_dialog, container, false);
v.setOnApplyWindowInsetsListener((v1, insets) -> {
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
v1.setPadding(0, 0, 0, imeInsets.bottom);
return insets;
});
getDialog().getWindow().setDecorFitsSystemWindows(false);
return v;
}
}
I have a viewpager with a fragment that contains a button. Here is the onCreateView of the fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment, container, false);
refreshButton = (Button) v.findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
refreshButton_Click();
}
});
return v;
}
I can verify that refreshButton_Click is not being called when I click on the refreshButton (which is clearly visible). I am guessing that the fragment just isn't getting focus or that it isn't allowing child elements to have focus.
Here is the layout for the fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.me.philip.firehydrant.FeedFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffededed">
<TextView
android:id="#+id/headerTextView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="header"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:layout_margin="2dp"
android:text="refresh"
android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"/>
</LinearLayout>
<ExpandableListView
android:id="#+id/postList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></ExpandableListView>
</LinearLayout>
EDIT: I forgot to mention: the button doesn't do the usual blue lighting up that buttons do when they are clicked, so I'm pretty sure it isn't the setOnClickListener that isn't working, but rather that the button is unable to get focus
Well, I believe I solved it. I had the ViewPager nested inside a DrawerLayout. I guess the similarity of their swiping gestures make them unworkable together.
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"/>
Been trying to figure this out for a while now:
I'm trying to create a custom dialog using the standard approach shown on the Dev site.
public class CustomDialog extends DialogFragment {
private OnDialogResultListener mOnDialogResultListener = null;
public void setOnDialogResultListener(OnDialogResultListener dialogResultListener) {
mOnDialogResultListener = dialogResultListener;
}
public static CustomDialog newInstance(OnDialogResultListener dialogResultListener) {
CustomDialog frag = new CustomDialog();
frag.setOnDialogResultListener(dialogResultListener);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle(getString(R.string.Dialog_CustomDialog_Title));
View v = inflater.inflate(R.layout.customdialog, container, false);
return v;
}
}
With the XML being:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
<EditText android:hint="#string/Dialog.CustomDialog.OldPassword" android:layout_marginBottom="#dimen/normalMargin" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText02" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.NewPassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText01" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.RetypePassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/editText1" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:text="#string/save" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_save"></Button>
<Button android:layout_height="wrap_content" android:text="#string/cancel" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_cancel"></Button>
</LinearLayout>
</LinearLayout>
And after creating and showing the dialog, I'm left with:
The white background has been applied to emphasize the unexpected and unwanted behavior.
Any ideas? I've tried changing width/heights, using weights in a horizontal LinearLayout, setting width/height programatically - all to no avail.
I found this tricks:
getDialog().getWindow().setBackgroundDrawableResource(R.color.white);
Looks like better solution.
I did come up with a cheap fix, which I would rather not have to use.
Adding a 0px height, 2000dp (some huge number) width view anywhere in the layout causes it to fill the dialog frame.
With the DialogFragment the width and height are null. Create an extra sub view group with fill parent and it should work for you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
.....
If you set attachToRoot = true during inflater.inflate it should show dialog correctly.
View v = inflater.inflate(R.layout.customdialog, container, true);
I used a relativeLayout as the root view in my case and it actually filled out the dialog windows.