Hello there I'm trying to remove the background of my dialog with
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
and it worked fine
But after implementing the shadow effect for my dialog this issue comes up again even if the background drawable is set to transparent
this is the line I have added
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
This is a screenshot of it
Full code
sort_image_dialog_fragment.java
public class sort_image_dialoge_fragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.dialog_rounded_background);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setDimAmount(0.8f);
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
return view;
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
}
sort_image.xml
<RelativeLayout 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">
<com.google.android.material.card.MaterialCardView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:backgroundTint="#color/grey"
android:elevation="12dp"
app:cardCornerRadius="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto"
android:text="Sort by"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/divider_line_sort_by"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/sort_textView"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:backgroundTint="#color/lite_grey"
app:cardCornerRadius="50dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/latest_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:fontFamily="#font/roboto"
android:text="Latest"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<TextView
android:id="#+id/most_popular_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/latest_sort_textView"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25sp"
android:fontFamily="#font/roboto"
android:text="Most Popular"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<RadioButton
android:id="#+id/latest_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp" />
<RadioButton
android:id="#+id/most_popular_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true" />
</RadioGroup>
</RelativeLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
Problem
The issue not related to the layout because whatever layout you assign to a dialog; the dialog applies it to its squared window; and the dialog window is white rectangle by default (i.e. doesn't have rounded corners).
The outside part of this rectangle layout is dimmed by default.
Therefore the white color you see on the corners is related to the dialog window background.
Solution
So, in order to make a rounded corners dialog; you have to change that on its window with below 2 steps:
Step 1: Create a theme in the themes.xml/styles.xml that removes the dialog window background:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#null</item>
</style>
Apply this theme by overriding getTheme() in your custom DialogFragment class:
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
Step 2:: Create a rounded drawable exactly with your card radius (20dp):
res\drawable\rounded_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
</shape>
Apply this to the dialogFragment view using view.setBackgroundResource() and remove other window calls that you provided in onCreateView():
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
UPDATE:
Make sure to use the lower case #null not #Null.
The sort_image_dialog_fragment.java should now look like:
public class sort_image_dialog_fragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
}
Result:
Side note: the name convention of classes should start with upper case, and follow camel case pattern; so the sort_image_dialog_fragment would be SortImageDialgFragment instead.
Related
This is the error I'm gettingI'm trying to use this library in fragment inside onViewCreated() method, but i'm getting this casting error: - java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView cannot be cast to de.hdodenhof.circleimageview.CircleImageView. I don't know how to solve this error. can anybody please help me?
FragmentProfile.java :
public class FragmentProfile extends Fragment implements View.OnClickListener {
Button sign_out;
TextView user_number;
private CircleImageView userImage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sign_out = getActivity().findViewById(R.id.user_logout);
userImage = getActivity().findViewById(R.id.user_display_name);
user_number = getActivity().findViewById(R.id.phone_number);
sign_out.setOnClickListener(this);
}
#Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getActivity(), Login.class));
getActivity().finish();
}
}
fragment_profile.xml
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/dp_180"
android:scaleType="fitXY"
android:src="#drawable/money" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="#dimen/dp_95"
android:layout_height="#dimen/dp_95"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_140"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher"
app:civ_border_color="#color/textColor"
app:civ_border_width="#dimen/dp_2" />
<TextView
android:id="#+id/user_display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_4"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="#string/username"
android:textColor="#color/border"
android:textSize="#dimen/sp_22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/user_logout"
android:layout_below="#id/user_display_name"
android:layout_margin="#dimen/dp_12"
android:orientation="vertical">
<TextView
android:id="#+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp_12"
android:layout_marginTop="#dimen/dp_30"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="+91-987654321"
android:textColor="#android:color/black"
android:textSize="#dimen/dp_18" />
</LinearLayout>
<Button
android:id="#+id/user_logout"
android:layout_width="#dimen/dp_200"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/dp_30"
android:background="#drawable/button_shape"
android:text="#string/sign_out"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textStyle="bold" />
</RelativeLayout>
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// ... rest of the codes.
// Comment out code below.
// userImage = getActivity().findViewById(R.id.user_display_name);
// Replace it with code below.
userImage = getActivity().findViewById(R.id.profile_image);
// ... rest of the codes.
}
Here you are parsing a textview id to the CircleImageView. You should change your userImage = getActivity().findViewById(R.id.user_display_name); to userImage = getActivity().findViewById(R.id.profile_image);. That's why you are getting java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView exception.
You issue is here:
private CircleImageView userImage;
userImage = getActivity().findViewById(R.id.user_display_name);
and in your layout:
<TextView
android:id="#+id/user_display_name"
You can not cast a TextView into a CircleImageView.
Here's my fragment_photo_gallery.xml file:
<?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">
<RelativeLayout
android:id="#+id/disconnected_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone">
<ImageView
android:id="#+id/disconnected_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_offline" />
<TextView
android:id="#+id/disconnected_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/disconnected_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/disconnected_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_title_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="#string/disconnected_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0" />
<Button
android:id="#+id/disconnected_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/disconnected_subtitle_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/disconnected_button_text" />
</RelativeLayout>
</RelativeLayout>
I bind all the stuff in the class which extends fragment:
public class PhotoGalleryFragment extends Fragment {
....
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
....
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
unbinder = ButterKnife.bind(this, v);
return v;
}
#Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
....
}
And get NPE on this method:
private void showDisconnectedView() {
disconnectedView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
My build gradle looks ok so I don't know why I get such an error
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
The guy who got similar problem figured out by changing annotationProcessor to apt but in my case, it just won't ever sync.
QUICK UPDATE
The problem still happened even if I replace
#BindView(R.id.disconnected_view)RelativeLayout disconnectedView;
with
disconnectedView = v.findViewById(R.id.disconnected_view); in my onCreateView method. So it must be not butterknife fault.
Use Butterknife without unbinder, You need to return already inflated view rather than creating new view & inflating it.
Also apply view specific code in onViewCreated() method.
public class PhotoGalleryFragment extends Fragment {
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
disconnectedView.setVisibility(View.GONE);
}
I am having trouble showing an image from the drawable folder on to a fragment.
When I try to show do it, it comes up with a java.lang.NullPointerException.
any help would be appreciated.
This is my code,
public class Fragment4 extends Fragment {
public Fragment4() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment4, container, false);
ImageView image = (ImageView)rootView.findViewById(R.id.image);
image.setImageResource(R.drawable.image123);
return rootView;
}
}
XML file
<?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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Your ImageView has no id. Set it as follows,
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
It is because you didn't add id attribute to the image in xml
Try this:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Hello i want to add a popup window to a fragment i tried a lot of things still nothing i have. If someone helps me it will be great.
Here is my Fragment code;
public class Info extends Fragment {
public Info() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info, container, false);
}
Here is my fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:src="#drawable/pozitif"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:text="dfdfdsfs"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
You can use a DialogFragment.
Instead of extending your Activity with Fragment, use DialogFragment
In on createView of the DailogFragment Activity inflate your layout
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Find your View elements
TextView Message = (TextView) v.findViewById(R.id.text_view1);
To call your DailogFragment use this
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
For more info on this you can check out this link.
https://developer.android.com/reference/android/app/DialogFragment.html
When using a dialogFragment I get a white space rectangle at the top of the fragment without knowing why.
What I should get
What I actually get
My DialogFragment:
public class SelectDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.select_frag, container, false);
return v;
}
}
select_frag.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/textView" />
</RelativeLayout>
You should set style without title:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
Also you can found more useful example here