Layout Params not working in Custom Dialog Fragment - android

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));
}
}

Related

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.

Add things to backdrop of Android DialogFragment

Want to add something to Dialog's backdrop like
I tried to read all the documents but can't find a way to add check in text to the backdrop.
I have done it like this, hope it will help
Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:calendar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/mainLayout"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/fifty_five_dp"
android:background="#android:color/transparent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/twenty_dp"
android:layout_marginTop="#dimen/one_twenty_dp"
android:layout_marginRight="#dimen/twenty_dp"
calendar:cardCornerRadius="#dimen/three_dp"
calendar:cardElevation="#dimen/two_dp"
calendar:cardPreventCornerOverlap="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_five_dp"
android:background="#color/dull_white"
android:padding="#dimen/thirteen_dp"
android:textSize="#dimen/sixteen_sp">
<TextView
android:id="#+id/leaveFromTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:fontFamily="#font/sf_pro_display_bold"
android:text="#string/select_leaves_placeholder"
android:textColor="#color/darkGreyDeepDeep"
android:textSize="#dimen/sixteen_sp" />
<TextView
android:id="#+id/leaveCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:fontFamily="#font/sf_pro_display_bold"
android:text="#string/days_placeholder"
android:textColor="#color/darkGrey"
android:textSize="#dimen/twelve_sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:calendar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/seven_dp"
android:layout_marginTop="#dimen/fifteen_dp"
android:background="#drawable/box"
android:padding="#dimen/ten_dp">
<RelativeLayout
android:id="#+id/okayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/thirteen_dp">
<ImageView
android:id="#+id/calenderDrawable"
android:layout_width="#dimen/seventeen_dp"
android:layout_height="#dimen/seventeen_dp"
android:layout_centerVertical="true"
android:src="#mipmap/calener" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="#dimen/ten_dp"
android:layout_toRightOf="#+id/calenderDrawable"
android:text="Calender"
android:textColor="#color/blue_light"
android:textSize="#dimen/fourteen_sp" />
<Button
android:id="#+id/btnDone"
android:layout_width="#dimen/seventy_dp"
android:layout_height="#dimen/twenty_six_dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="#dimen/three_dp"
android:background="#drawable/calender_button_background"
android:text="DONE"
android:textColor="#color/white"
android:textSize="#dimen/thirteen_sp" />
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/okayLayout"
android:layout_marginTop="#dimen/ten_dp"
android:background="#color/viewColor" />
<com.neomeric.nock.com.andexert.calendarlistview.library.DayPickerView
android:id="#+id/pickerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view"
android:layout_marginTop="#dimen/ten_dp"
android:background="#color/dull_white"
calendar:enablePreviousDay="false" />
</RelativeLayout>
</LinearLayout>
DatePickerPopup.java
public class DatePickerPopup extends DialogFragment implements DatePickerController {
private DayPickerView dayPickerView;
public DatePickerPopup() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static DatePickerPopup newInstance(OnDatesSelected datesSelected, Date first, Date last) {
DatePickerPopup frag = new DatePickerPopup();
Bundle args = new Bundle();
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.date_selection_layout, container);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return root;
}
public void setDateAndDaysCount(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays) {
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public int getMaxYear() {
return 2021;
}
#Override
public void onDayOfMonthSelected(int year, int month, int day) {
}
#Override
public void onStart() {
super.onStart();
// To make it match parent
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
#Override
public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays) {
}
}
How it looks
Make a linear layout with 2 internal layouts, where the upper one will have a transparent background and the other one will have the content.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ic_clock" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="10dp"
android:background="#000000"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>

Fragment is not appearing on the hosting activity

I am adding a fragment to an activity as shown below in the code. but at run time i found that the fragment is never shown.
i examined the fragment layout and the its viewgroup in the main layout.
please let me know why it is not showing.
MainAct:
I am adding a fragment to an activity as shown below in the code. but at run time i found that the fragment is never shown.
i examined the fragment layout and the its viewgroup in the main layout.
please let me know why it is not showing.
MainAct:
public class ActMain extends AppCompatActivity {
public static String TAG = ActMain.class.getSimpleName();
private FragmentButtons mFragButtons = null;
//private Fragment mFragmentButtons = null;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "#onCreate");
this.initViews();
}
}
frag
public class FragmentButtons extends Fragment {
private final static String TAG = FragmentButtons.class.getSimpleName();
public interface iActionHandler {
public void onButton00Clicked();
public void onButton01Clicked();
public void onButton02Clicked();
public void onButton03Clicked();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "#onCreateView");
View view = inflater.inflate(R.layout.frag_buttons, container, true);
return view;
}
}
mainLayout:
<?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:weightSum="2">
<fragment
android:id="#+id/actMain_FragButtons"
android:name="com.example.pc_amr.servicewithid_01.fragments.FragmentButtons"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!--<FrameLayout
android:id="#+id/actMain_FragContents00"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"/>-->
</LinearLayout>
fragLayout:
<?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:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn00"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn01"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn02"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn03"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn10"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn11"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn12"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn13"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn20"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn21"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn22"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn23"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
The reason that it is not showing it is because in your onCreate method, you have not done setContentView and that is why it is empty and not showing.
Add the following line:
setContentView(R.layout.mainLayout);
This should allow the fragment to be shown.
I used this way and it worked with me
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_1_layout, container, false);
return v;}

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!

Dialog using DialogFragment not showing up properly

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);

Categories

Resources