Dialog using DialogFragment not showing up properly - android

I am using DialogFragment to display a dialog.But the dialog was displayed with the title.When i used no title my dialog was not being displayed properly.
Dialog with title
Dialog without title
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/share_background"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>
</LinearLayout>
DialogFragment Code
public class ShareDialogFragment extends DialogFragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, null, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Why this is happening????

Try this:
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
Hope this helps.

Fix layout height and width of your parent linear layout to a fixed value.
Example:
android:layout_width="200dp"
android:layout_height="wrap_content"

Try this:
inflater.inflate(R.layout.share_dialog, container, false);

Related

View is not added when using addView()

I am making a window to open an empty dialog and write comments.
The dialog initially has an empty item where i can write one comment.
Comments can be up to one line.
Pressing the Enter key dynamically creates an item that allows you to write the next comment.
I was wondering whether to use RecyclerView or addView() for this function.
Since this dialog was also opened from the recycler view item of the parent, it would be complicated when using RecyclerView again, so I used addView().
I don't know if this is the right choice.
This is because data management seems to be difficult because the created comment must be saved in the parent's recycler view item.
Anyway, using addView() does not add the view.
There is no such thing as an error, but it cannot be added.
What is the cause?
writeing_comment_item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/comment_edit" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
app:layout_constraintLeft_toRightOf="#+id/start_point"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_writing_comment_dialog
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
WritingCommentDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
editText = view.findViewById(R.id.comment_edit);
mContainer = view.findViewById(R.id.container);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override // IME actionNone (Enter Key)
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
View inflater1 = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
mContainer.addView(inflater1);
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
setDialogSize();
}
private void setDialogSize() {
getDialog().getWindow().setLayout(1000, 1000);
}
}
your problem is that you added a horizontal LinearLayout and after you added an EditText with android: layout_width = "match_parent" that's why your Comment is added on your container view but is not visible because of the EditText.
if I understood correctly you must change
android: orientation = "horizontal" by android: orientation = "vertical" in your fragment_writing_comment_dialog
so that the commentary will be displayed above the EditText
And also you need to change the orientation on your Container LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Layout Params not working in Custom Dialog Fragment

I am trying to set my custom dialog fragment to use the full width of the screen. The one solution is to set LayoutParams on view. But they are not working.
Things I Tried
Changing parent Layout
using getLayoutParams().getClass() which returns null
Layout Validator shows correct layout so no prob here, here is my Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
style="#style/MaterialAlertDialog.MaterialComponents.Title.Text.CenterStacked"
android:id="#+id/ask_hint_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/puppy_vw"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<pl.droidsonroids.gif.GifImageView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:src="#drawable/solving"
android:adjustViewBounds="true">
</pl.droidsonroids.gif.GifImageView>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MaterialAlertDialog.MaterialComponents.Title.Text.CenterStacked"
android:text="#string/ad_confirm_text"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_below="#id/puppy_vw"
android:id="#+id/ad_confm_btns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_centerInParent="true">
<com.google.android.material.button.MaterialButton
android:id="#+id/yes_hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#color/white"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:text="Yes">
</com.google.android.material.button.MaterialButton>
<!--
<com.google.android.material.button.MaterialButton
android:id="#+id/no_hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:text="No">
</com.google.android.material.button.MaterialButton>
-->
<com.google.android.material.button.MaterialButton
android:id="#+id/why_hint"
android:layout_margin="5dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
android:text="Why Ads?">
</com.google.android.material.button.MaterialButton>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
Here is my DialogFragment class
public class AdLoadFragment extends DialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
AskHintDialogBinding binding=AskHintDialogBinding.inflate(inflater,container,false);
View view=binding.getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MyUtilsApp.showLog(String.valueOf(view.getClass()));
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
}
}

Modal Bottom Sheet isnt transparent althought background set too transparent

All my modal BottomSheets have a white background above them and are not transparent as intended.
I tried opening the view via a separate Fragment like this:
ContextMenuPlaylistFragment contextMenuPlaylistFragment = new ContextMenuPlaylistFragment();
contextMenuPlaylistFragment.show(fragmentManager,"contextmenu playlists");
and like that:
View modelBottomSheet = LayoutInflater.from(mContext).inflate(R.layout.context_menu_playlist, null);
BottomSheetDialog dialog = new BottomSheetDialog(mContext);
dialog.setContentView(modelBottomSheet);
dialog.show();
This is my BottomSheetFragment:
public class ContextMenuPlaylistFragment extends BottomSheetDialogFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.context_menu_playlist, container, false);
}
}
And this is my BottomSheet:
<?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="#color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_search"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_search_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_rename"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_edit_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rename_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_delete"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_delete_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/delete_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I put it in two LinearLayouts to try and make the background transparent.
I also tried it with one LinearLayout that doesn't change the background and has height "wrap_content"
Please check if you have set the android:background attribute in the "AppTheme" style to white. If so, delete it or set it to transparent will solve the problem.

Android: custom layout DialogFragment doesn't respect the width size given

I've created this class:
public class AlertDialogFragment extends DialogFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_alert, container);
...
My dialog_alert.xml layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dialog_background">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingLeft="#dimen/keyline_4"
android:paddingRight="#dimen/keyline_4"
android:paddingTop="#dimen/keyline_4">
<TextView
android:id="#+id/title"
style="#style/DialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:text="#string/visible_by"
android:visibility="gone"/>
<TextView
android:id="#+id/content"
style="#style/DialogContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vos données de connexion sont incorrectes." />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="#dimen/keyline_4"
android:paddingRight="#dimen/keyline_1"
android:paddingBottom="#dimen/keyline_1"
android:layout_gravity="right">
<TextView
android:id="#+id/negative_button"
style="#style/DialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/keyline_0"
android:text="negative"
android:visibility="gone" />
<TextView
android:id="#+id/positive_button"
style="#style/DialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="positive" />
</LinearLayout>
</LinearLayout>
And the result is:
I've defined "match_parent" attribute with the padding equal at 24dp (=#dimen/keyline_4).
Thanks for your help guys!

How to remove main activities buttons from the fragment with the codes in android?

How to remove main activities buttons from the fragment with the codes in android?
There are a few buttons in activity_main.xml. I want to access and remove a button from the fragment class. Is this posssible and how?
main_activ.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/paper"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal" >
<Button
android:id="#+id/butBack"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butMain"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text="..."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butNext"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My fragment
public class Frag_Menu extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_menu, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) getActivity().findViewById(R.id.butBack).getParent();
if(null!=layout)
layout.removeView(layout);
};
public static android.support.v4.app.Fragment newInstance(String string) {
return null;
}
}
You can use getActivity().findViewById() to get your Button from the fragment class in onActivityCreated().
View button = getActivity().findViewById(R.id.butBack);
if (button != null && button.getParent() != null) {
((ViewGroup)button.getParent()).removeView(button);
}

Categories

Resources