Adding a close button to a DialogFragment

Adding a close button to a DialogFragment



I've been trying to make a DialogFragment have a close button in the top left, as seen in the photo. What the final result should look like (Sorry about the text, it's in romanian) Could anyone tell me how I could go about doing that?



Here's the layout for the fragment:


<android.support.constraint.ConstraintLayout
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">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="-50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"/>
<!---add your views here-->
</LinearLayout>

<ImageView
android:id="@+id/imageView_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:clickable="true"
android:src="@drawable/ic_close_dialog" />





The way the fragment looks now: A small box where the image and text are overlayed




3 Answers
3



You can create a custom layout and use


dialog.setContentView(R.layout.custom_view);



Refer custom dialog with close button



If it still comes like it does in the picture, a possibility is that the container view for this constraint layout has wrap_content as its width and height.





I was looking at that post earlier, attempted the verified answer. Unfortunately, the button wouldn't move to the top, whatever I did.
– Boni Daniel
Aug 27 at 14:03





can you post the code for the layout
– Venkata Narayana Malireddy
Aug 27 at 14:04





(full) layout has been posted
– Boni Daniel
Aug 27 at 14:09






Please post the complete XML file.Seems like some of it is missing
– Venkata Narayana Malireddy
Aug 27 at 14:10





my bad. updated
– Boni Daniel
Aug 27 at 14:11



Create a custom dialog class that extends dialog and then use an xml file to define your layout like you would for an activity.


public class CustomDialogClass extends Dialog

public CustomDialogClass(Activity a)
super(a);


@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);




And have your custom_dialog.xml layout file define the layout


custom_dialog.xml



on your button click use :


if (mProgressDialog != null)
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
mProgressDialog = null;




ProgressDialog mProgressDialog;





How would a ProgressDialog help in this case if what I need is a DialogFragment?
– Boni Daniel
Aug 27 at 14:08






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)