S
S
Sekorashka2014-04-04 14:15:01
Java
Sekorashka, 2014-04-04 14:15:01

Why does the Dialog appear as if it has Gravity.TOP when it needs to be centered?

I turn to you again for help.
It is necessary to show the AlertDialog with a non-standard background (in this case, a blurry background, but generally speaking, a dynamic image depending on what is on the screen).
The xml code for the dialog specified by android:layout_gravity="center":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android">

<SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_gravity="center_horizontal"
        android:max="255"/>
</RelativeLayout>

Actually, the DialogFragmenta code:
public class CustomDialog extends DialogFragment implements  DialogInterface.OnClickListener {

   public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
               .setPositiveButton("yes", this)
               .setNegativeButton("no", this);
    LayoutInflater inflater = getActivity().getLayoutInflater();
    adb.setView(inflater.inflate(R.layout.slider_item, null));
    Dialog dialog = adb.create();
    Bitmap map=takeScreenShot(getActivity());
        Bitmap fast= FastBlur.doBlur(map, 10, true);
        final Drawable draw=new BitmapDrawable(getActivity().getResources(),fast);
        dialog.getWindow().setBackgroundDrawable(draw);
    // dialog.getWindow().getAttributes().gravity = Gravity.CENTER_VERTICAL;
//       WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
//       wmlp.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
      
//		Window window = dialog.getWindow();
//		window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
//		window.setGravity(Gravity.CENTER);
    return dialog;
   }
    private static Bitmap takeScreenShot(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
        view.setDrawingCacheEnabled(false);
        return b;
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

    }
}

but why o shows as in the picture.
8ab3fd38e95746baad69bae7ca59a027.png
As you can see, I tried several options for setting LayoutParams - commented out, none of them help...
Without dialog.getWindow().setBackgroundDrawable(draw); shows a standard dialog, but I just need to customize my background from the image.
Tell me how you can solve the problem and what is the problem itself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sekorashka, 2014-04-06
@Sekorashka

In general, the solution turned out to be rather crooked, but still a solution.
Everything is really simple. We make a dialog in the DialogFragment from onCreateView():
1) Set the layout with the imageView as the background, blur the screenshot of the screen and paste it into the imageView as the background
2) Create any alertDialog and show it, do not forget to put the fragment closure on the buttons in the dialog to close the entire dialog, otherwise the background will remain
3) Then we call the dialog as a normal fragment, but we serve the entire screen as a container.
Problems:
1) a purely Blurish problem with screen rotation - do not take a screenshot, during rotation - nothing (for me it's not a problem - many applications are rigidly tied to orientation)
2) my ignorance - how to take the view as a container along with the actionBar - I know about android.R.id.content, but it only takes the container itself, not including the actionbar + id placement for the view (I just never did it)
I guess it’s possible combine android.R.id.content with android.R.id.action_bar_container and its additional buttons, but I don't know how, can anyone help?
who is interested in the code below:
DialogFragment (with a couple of crutches related just to the problem of taking the activity screen):

public class CustomAlertDialog extends DialogFragment implements  DialogInterface.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.alert_custom_dialog, container, false);
    ImageView background = (ImageView) rootView.findViewById(R.id.image);
        Rect frame = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, statusBarHeight, 0, 0);
        background.setLayoutParams(params);
        Bitmap map=takeScreenShot(getActivity());
        Bitmap fast= FastBlur.doBlur(map, 10, true);
        final Drawable draw=new BitmapDrawable(getActivity().getResources(),fast);
        background.setImageDrawable(draw);
        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
                .setPositiveButton("yes", this)
                .setNegativeButton("no", this);
        LayoutInflater dialogInflater = getActivity().getLayoutInflater();
        adb.setView(dialogInflater.inflate(R.layout.slider_item, null)).show();
        return rootView;
    }

    private static Bitmap takeScreenShot(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
        view.setDrawingCacheEnabled(false);
        return b;
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
    }
}

well, a call from another fragment (maybe just here tell me how to take the whole screen with the actionbar?)
public void showDialogFragment() {
        View v = getActivity().getWindow().getDecorView();
        v.setId(1);
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        DialogFragment newFragment = new CustomAlertDialog();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.add(1, newFragment)
                    .addToBackStack(null).commit();
    }

xml is the same
and yes a picture to attract attention)
6cf21919baca46b18cc05f3dbd07b5ff.png

A
Andrey M, 2014-04-07
@Collosteam

I usually do this

<!--Обязательно в корневом Layout делаем wrap_content по width и height-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:layout_gravity="center">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@color/cp_yellow">
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textColor="@android:color/white"
                android:textSize="22sp" />
        </RelativeLayout>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center">
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center">
                <ProgressBar
                    android:id="@android:id/progress"
                    style="?android:attr/progressBarStyleLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center" />
                <com.RichEditText.colorpicker.ColorPickerPalette
                    android:id="@id/color_picker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:visibility="gone" />
            </FrameLayout>
        </ScrollView>
    </LinearLayout>
</FrameLayout>

in FragmentDialog override onCreateView
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);  // Находим наш Layout
        this.mProgress = ((ProgressBar) view.findViewById(android.R.id.progress));
        this.mPalette = ((ColorPickerPalette) view.findViewById(R.id.color_picker));
        this.mPalette.init(this.mSize, this.mColumns, this);
        if (this.mColors != null)
            showPaletteView();
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);  //Прячем стандартный Title
        return view;
    }

One problem is that you need to redraw the confirmation and cancel buttons yourself, but this template code can be taken from the sources.
It was not possible to customize AlertDialog in another way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question