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
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.
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
on your button click use :
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
ProgressDialog mProgressDialog;
dialog_xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal"
android:background="#color/transparent">
<FrameLayout
android:id="#+id/contentView"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!-- you will add content here -->
</FrameLayout>
<ImageView
android:id="#+id/ivCloseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/content_description"
app:layout_constraintBottom_toTopOf="#+id/contentView"
app:layout_constraintEnd_toEndOf="#+id/contentView"
app:layout_constraintStart_toEndOf="#+id/contentView"
app:layout_constraintTop_toTopOf="#+id/contentView"
app:srcCompat="#android:drawable/ic_menu_close_clear_cancel" />
</android.support.constraint.ConstraintLayout>
style:
<style name="TransparentDialog" parent="#style/Base.Theme.AppCompat.Light.Dialog">
<item name="colorPrimaryDark">#android:color/black</item> <!-- you can change here -->
<item name="android:windowBackground">#android:color/transparent</item> <!-- this is important -->
<item name="android:windowIsFloating">false</item> <!-- this is important -->
<item name="android:windowNoTitle">true</item> <!-- this is important -->
</style>
Related
My motive is to have a base-card with an overlapping cardview icon view. I tried to achieve the same using Constraint Layout as a root and creating a DialogFragment out of it. However, the preview on IDE vs Phone is quite different.
I want to achieve:
DialogFragment with overlapping Icon
And The Background of ConstraintLayout should be transparent.
Here is comparison
user_profile_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/coins_count"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="#+id/cardView"
app:layout_constraintStart_toStartOf="#id/textView2"
app:layout_constraintEnd_toEndOf="#id/textView2"
app:layout_constraintTop_toBottomOf="#id/textView2">
<TextView
style="#style/cardBodyText"
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200" />
<TextView
style="#style/cardBodyText"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"/>
</LinearLayout>
<TextView
style="#style/viewParent.headerText"
android:id="#+id/textView2"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aman Sharma"
app:layout_constraintRight_toRightOf="#id/cardView"
app:layout_constraintLeft_toLeftOf="#id/cardView"
app:layout_constraintTop_toBottomOf="#+id/materialCardView2" />
<androidx.cardview.widget.CardView
android:translationY="-50dp"
android:translationZ="-3dp"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:clipChildren="true"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="#id/cardView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_user" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.button.MaterialButton
style="#style/myMaterialButton"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add as Friend"
app:layout_constraintStart_toStartOf="#id/flexbox"
app:layout_constraintEnd_toEndOf="#id/flexbox"
app:layout_constraintTop_toBottomOf="#id/flexbox"
app:layout_constraintBottom_toBottomOf="#id/cardView"/>
<com.google.android.flexbox.FlexboxLayout
android:id="#+id/flexbox"
android:layout_marginTop="20dp"
android:minHeight="60dp"
app:alignItems="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/coins_count"
app:layout_constraintEnd_toEndOf="#id/coins_count"
app:layout_constraintTop_toBottomOf="#id/coins_count">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user"/>
</com.google.android.flexbox.FlexboxLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my DialogFragmentClass
UserProfileDialogjava
public class UserProfileDialog extends DialogFragment {
public UserProfileDialog() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
DialogUserProfileBinding binding=DialogUserProfileBinding.inflate(inflater,container,false);
return binding.getRoot();
}
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params=getDialog().getWindow().getAttributes();
params.width= ViewGroup.LayoutParams.MATCH_PARENT;
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);
}
}
Okay, here is what I did. First of all, I deleted your lines from onResume inside DialogFragment class. Then, inside your XML I added my picture for the user.
Inside the activity where you call for UserProfileDialog I used this:
FragmentManager fm = getSupportFragmentManager();
UserProfileDialog upd = new UserProfileDialog();
upd.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NoActionBarDialog);
upd.show(fm, "upd");
As you can see, I used setStyle() to set a custom style for your Dialog which is defined in themes.xml like this:
<!-- Dialog Theme -->
<style name="NoActionBarDialog" parent="Theme.MaterialComponents.Light.Dialog.Bridge">
<item name="windowActionBar">false</item>
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
<item name="windowNoTitle">true</item>
<item name="android:colorBackground">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
And then, you get this:
You can now play more with some other attributes to make it as you want it to be. Sorry for spacing and colors inside the view but this is my temp_project just for things like this and themes are chaos. So, it should be working fine when you implement this in your code.
This is my dialog,
public class TestDialog extends DialogFragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_dialog, container, false);
}
}
When I am using RelativeLayout for my design like below,
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textTitle"
android:layout_width="match_parent"
android:layout_height="#dimen/dp48"
android:layout_alignParentTop="true"
android:fontFamily="#font/trebuchet"
android:gravity="center"
android:text="Test Dialog"
android:textColor="?attr/colorAccent"
android:textSize="18sp" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:src="#drawable/ic_baseline_public_24px"
android:tint="#color/black" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/icon"
android:fontFamily="#font/trebuchet"
android:text="This is very long text, This is very long text, This is very long text" />
<com.google.android.material.button.MaterialButton
android:id="#+id/imageView_close"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:layout_centerHorizontal="true"
android:contentDescription="#string/close"
android:src="#drawable/ic_baseline_close_24dx"
android:text="#string/cancel" />
</RelativeLayout>
I am getting this output.
But When I am using ConstraintLayout for my design like below,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textTitle"
android:layout_width="0dp"
android:layout_height="#dimen/dp48"
android:fontFamily="#font/trebuchet"
android:gravity="center"
android:text="Test Dialog"
android:textColor="?attr/colorAccent"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:src="#drawable/ic_baseline_public_24px"
android:tint="#color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textTitle" />
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="#font/trebuchet"
android:text="This is very long text, This is very long text, This is very long text"
app:layout_constraintStart_toEndOf="#+id/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textTitle" />
<com.google.android.material.button.MaterialButton
android:id="#+id/imageView_close"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/close"
android:src="#drawable/ic_baseline_close_24dx"
android:text="#string/cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am getting following output.
I don't know why this strange behavior. Only in dialog I am getting this behavior.
If anything I'm missing in constraint layout, please let me know friends, I'll correct myself.
all you have to do is set Theme to your dialog.
#Override
public int getTheme() {
return R.style.dialog;
}
style
<style name="dialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">#drawable/radius</item>
<item name="android:windowMinWidthMajor">80%</item>
<item name="android:windowMinWidthMinor">85%</item>
<item name="android:windowNoTitle">true</item>
</style>
radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:radius="10dp" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
I think the problem is android:layout_width="match_parent" in your ConstraintLayout tag. Since a Dialog has no fixed width, using match_parent won't work. Maybe you could set a fixed width, or a minimum width.
First, it's not the ConstraintLayout but how you are implementing the Dialog itself. It's a suggestion and according to Dialog Documentation:
The Dialog class is the base class for dialogs, but you should avoid
instantiating Dialog directly. Instead, use one of the following
subclasses:
AlertDialog
A dialog that can show a title, up to three buttons, a
list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user
to select a date or time.
So basically you'll be using setView api:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
// Setting my layout here
builder.setView(R.layout.my_layout);
builder.setPositiveButton(/* My Methods */)
.setNegativeButton(/* My Methods */);
return builder.create();
}
Read about it in Creating a Custom Layout documentation. If you want more customization then create a custom theme within values > styles.xml which can be used throughout the app or just by setting it along with the builder, like:
new AlertDialog.Builder(requireActivity(), R.style.My_Alert_Dialog_Theme);
I create my Dialog like this:
The layout dialog_app_rating:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingEnd="#dimen/spacing_large"
android:paddingStart="#dimen/spacing_large"
tools:ignore="MissingPrefix">
<TextView
fontPath="#string/font_roboto_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_normal"
android:text="#string/rating_remind_title"
android:textColor="#color/text_blue"
android:textSize="#dimen/font_large"
tools:text="Rate my app" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_small"
android:text="#string/rating_remind_msg"
android:textSize="#dimen/font_normal"
tools:text="Rate if it is useful" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/app_rating_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/spacing_normal"
app:srb_borderColor="#color/grey"
app:srb_fillColor="#color/pink"
app:srb_numberOfStars="5"
app:srb_rating="5"
app:srb_starBorderWidth="1"
app:srb_starSize="#dimen/spacing_larger"
app:srb_starsSeparation="#dimen/spacing_small"
app:srb_stepSize="1" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_normal">
<android.support.v7.widget.AppCompatButton
android:id="#+id/not_show_again_btn"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="#dimen/text_button_height"
android:text="#string/rating_remind_dont_show_again"
android:textColor="#color/text_blue" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/send_btn"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="#dimen/text_button_height"
android:layout_gravity="end"
android:text="#string/send"
android:textColor="#color/text_blue" />
</FrameLayout>
</LinearLayout>
The code for RatingDialog:
class RatingDialog extends Dialog {
#BindView(R.id.app_rating_rb) SimpleRatingBar mAppRatingBar;
#BindView(R.id.not_show_again_btn) View mNotAgainBtn;
#BindView(R.id.send_btn) View mSendBtn;
RatingDialog(#NonNull Context context) {
super(context, R.style.DialogTheme);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_app_rating);
ButterKnife.bind(this, this);
mNotAgainBtn.setOnClickListener(this);
mSendBtn.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
// Other implementation...
}
My DialogTheme in styles.xml:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowSoftInputMode">stateUnchanged|adjustPan</item>
<item name="colorAccent">#color/pink</item>
</style>
It simply has some text, a rating bar, and 2 buttons. The buttons have Ripple effect on touch by default.
The problem is the Ripple effect not smooth when using in Dialog. It dims the background, shows a ripple and delays a bit then finish like the frame is being skipped. I've tried the effect on other dialogs as well but the problem is the same. Is that Android set low-priority for animation for Dialog only or something wrong with my config, can I fix it?
Thanks in advance
I have an Android layout that's used for showing an "about app" alert dialog with a "Close" button. It has a title in bold centered, the app version and the developer name. Everything looks fine except that there's a very noticeable margin/padding above the title and I can't remove that empty space.
Here's the layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:orientation="vertical" android:scrollbars="none" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="6.0dp" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center">
<TextView android:textSize="16.0dip" android:textStyle="bold" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginRight="6.0dip" android:layout_marginBottom="10.0dip" android:text="#string/app_name" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="#string/version_text" />
<TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="#string/app_version" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:text="#string/about_developer" />
</LinearLayout>
<LinearLayout android:gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="#id/close_btn" android:layout_width="110.0dip" android:layout_height="44.0dip" android:layout_marginTop="4.0dip" android:layout_marginBottom="4.0dip" android:text="#string/about_close" />
</LinearLayout>
</LinearLayout>
</ScrollView>
And here's what it shows:
I have tried adding android:layout_marginTop="-10.0dip" attribute in many tags and searched SO for a similar issue but I can't find it. I'd be thankful if someone shows me how solve this problem.
You could try to make your own Dialog with a custom View by using a DialogFragment and set this
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Or maybe just try to use a simple Dialog, not an AlertDialog like this
Dialog dialog = new Dialog(theContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(yourCustomView);
dialog.show();
Create style for the AlertDialog:
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:background">#ffffff</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12sp</item>
</style>
Define this style while creating AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(container.getContext(), R.style.AlertDialogCustom);
Use a RelativeLayout instead (the code below needs to be adjusted to fit your needs, but it gives the general layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="App Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/textView" />
<TextView
android:text="Version 1.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:id="#+id/textView2" />
<TextView
android:text="Developed by Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:id="#+id/textView3" />
<Button
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginTop="42dp"
android:id="#+id/button2" />
</RelativeLayout>
You can also use a Dialog instead like others have mentioned.
EDIT:
You can try using a custom Dialog instead then using a DialogFragment. To call the Dialog, use this: new CustomDialogFragment().show(getSupportFragmentManager(), "mydialog");
Then create a new class extending DialogFragment, and use something similar to this:
public class DonateDialogFragment extends DialogFragment implements View.OnClickListener
{
private AlertDialog dialog;
public interface CustomDialogListener {
void buttonClicked();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof CustomDialogListener)) {
throw new ClassCastException(activity.toString() + " must implement CustomDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder customDialogMain = new AlertDialog.Builder(getActivity());
donateDialogMain.setCancelable(true);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.--YOUR-CUSTOM-DIALOG-LAYOUT-HERE--, null);
view.findViewById(R.id.close_btn).setOnClickListener(this);
customDialogMain.setView(view);
dialog = customDialogMain.show();
return dialog;
}
public void onClick(View v) {
// does something very interesting
switch (v.getId())
{
case R.id.close_btn:
((CustomDialogListener) getActivity()).buttonClicked();
dialog.dismiss();
break;
}
}
}
you can use this style direct to style.xml
<style name="Theme.UserDialog.AppCompat" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="android:minWidth">600dp</item>
</style>
Apply this style to the activity class which you wants to set as alert dialog
android:theme="#style/Theme.UserDialog.AppCompat"
and start activity using Intent like
Intent activity= new Intent(this, AlertActivity.class);
startActivity(activity);
I have an android activity that implements a custom dialog.The application is running ok but the dialog is too small,i want to display a bigger dialog.How can i achieve this?
Here is my layout xml
<?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/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/view_more_borders">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/share_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Company Name:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/nice_blue"
android:typeface="sans" />
<TextView
android:id="#+id/textViewCompanyName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.80"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Price per share:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/nice_blue" />
<TextView
android:id="#+id/textViewprice_pershare"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignBaseline="#+id/Yesterday"
android:layout_alignBottom="#+id/Yesterday"
android:layout_marginLeft="38dp"
android:fontFamily="sans-serif"
android:layout_toRightOf="#+id/company"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Total cost:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/nice_blue" />
<TextView
android:id="#+id/textview_totalcost"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignBaseline="#+id/Yesterday"
android:layout_alignBottom="#+id/Yesterday"
android:layout_marginLeft="38dp"
android:fontFamily="sans-serif"
android:layout_toRightOf="#+id/company"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Number of shares:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/nice_blue" />
<EditText
android:id="#+id/shareNumber"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:inputType="number"
android:fontFamily="sans-serif"
android:textSize="12sp"
>
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Payment method:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/nice_blue" />
<Spinner
android:id="#+id/spinner_paymentmode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button_buy_shares"
style="#style/ButtonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_button"
android:text="Buy" />
And here is my output.I want to increase the width and height of the dialog.
And my java file
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment() {
}
private EditText mEditText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialogfragment, container);
// mEditText = (EditText) view.findViewById(R.id.txt_your_name);
// getDialog().setTitle("Hello");
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Change your dialog dimension on runtime:
yourDialog.show();
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);
You can do that for both dimension, in my example i only changed the width.
Hope it helps!
EDIT
I forgot to mention where i took width:
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
EDIT 2
Try this code:
Dialog yourDialog = dialogFragment.getDialog();
yourDialog.getWindow().setLayout((6 * width)/7, (4 * height)/5);
Setting android:minWidth and android:minHeight in the custom layout does the trick for me.
put below line in onCreate
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
original answer
Dialogs are sized by their content. You can add padding and margins on the outer layout to consume more space, but that will not redistribute the views inside.
<style name="full_screen_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:minWidth" type="dimen">600dp</item>
</style>
Use this in style.xml file and use this style in dialog class
Add android:minWidth / android:minHeight to your root view. See example below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="300dp"
android:minHeight="500dp">
The right solution is to override the onCreateDialog method rather than onCreateView, and create your dialog with a AlertDialog.Builder, as explained in the doc: http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
The trick worked for me
override fun onStart() {
super.onStart()
val metrics: DisplayMetrics = resources.displayMetrics
val width: Int = metrics.widthPixels
dialog?.window?.setLayout(width - 58,
ViewGroup.LayoutParams.WRAP_CONTENT)
}
Try this
For setting dialog width and height as per device screen size
Display display;
int DisplayWidth, DisplayHeight, DialogWidth, DialogHeight;
Dialog dialog;
display =((WindowManager)activity_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayWidth = display.getWidth();
DisplayHeight = display.getHeight();
if(DisplayHeight > DisplayWidth)
{
DialogWidth = (6 * DisplayWidth) / 7 ;
DialogHeight = (4 * DisplayHeight) / 5 ;
}
else
{
DialogWidth = (6 * DisplayWidth) / 9 ;
DialogHeight = (4 * DisplayHeight) / 5 ;
}
dialog = new Dialog(activity_context);
// Set your dialog width and height dynamically as per your screen.
Window window = dialog.getWindow();
window.setLayout(DialogWidth,DialogHeight);
window.setGravity(Gravity.CENTER);
dialog.show();
try to set android:layout_height to some value for base LinearLayout
for example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!-- right here -->
android:layout_height="100dp"
android:background="#color/white"
android:orientation="vertical" >
I configured my project so that the default dialog theme for DialogFragments has a minimum width.
set the app's theme to your custom theme in the AndroidManifest.xml....alternatively, set the theme of the Activity hosting the DialogFragment to your app's custom theme.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"> <!-- set the app's theme to your custom theme -->
.....
</application>
</manifest>
define your custom theme in values/styles.xml, and override the default theme used for dialog fragments with one you define yourself.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<!-- override the default theme for DialogFragments -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
</style>
<!--
configure your custom theme for DialogFragments...
use a theme that has MinWidth, so that the dialog is not "too small"
-->
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">
<!-- override the default theme for DialogFragments spawned by this DialogFragment -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
<!--
OPTIONAL: override the background for the dialog...i am using a dark theme,
and for some reason, there is no themes for dialogs with dark backgrounds,
so, i made my own.
-->
<item name="android:windowBackground">#drawable/dialog__window_background</item>
<!--
add the title to the dialog's theme. you can remove it later by using
DialogFragment.setStyle()
-->
<item name="android:windowNoTitle">false</item>
<item name="windowNoTitle">?android:windowNoTitle</item>
</style>
.....
</resources>
if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="?dialogCornerRadius" />
<solid android:color="?android:colorBackground" />
</shape>
</inset>