简体   繁体   中英

How to make an AppCompat Activity as a Dialog?

I need to use my AppCompat Activity as a Dialog.For this I tried so my solution that answered in StackOverflow. But nothing worked.Please answer me. I am getting activity as dialog. But it shows very narrow both in height & width.

I used the following Theme:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

在此输入图像描述

You can use DialogFragment and customize the layout accordingly.

public class CustomDialogFrag extends DialogFragment{
static FragmentManager fragmentManager;
    public static CustomDialogFrag showDialog(FragmentManager fm){
        CustomDialogFrag customDialogFrag=new CustomDialogFrag();
        fragmentManager=fm;
        return customDialogFrag;
    }
@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null);
        alertDialogBuilder.setView(view);
        setupUI(view);
        alertDialogBuilder.setTitle("Notification Message");
        alertDialogBuilder.setIcon(R.drawable.notificationicon);
        alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return alertDialogBuilder.create();
    }
void setupUI(View view){
        TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias);
        TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime);
        TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage);
        textViewOne.setText("Text 1");
        textViewTwo.setText("Text 2");
        textViewThree.setText("Text 3");
    }
}


And the dialogfrag_layout.xml will be

<?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:padding="@dimen/margin_10"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtEventAlias"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtTime"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtLogMessage"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
</LinearLayout>

For invoking this Dialog from a Fragment :

DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager());
dialogFragment.show(getActivity().getFragmentManager(), "tag");

In your activity's onCreate put the following lines:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your);

    // Make the window's width full sized
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    Window window = getWindow();

    layoutParams.copyFrom(window.getAttributes());
    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

    window.setAttributes(layoutParams);
}

Tested and works. You can set to both width and height to WRAP_CONTENT if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM