Make your Dialog more concise

Keywords: Android github

GitHub address

Rebuild a dialog with Builder mode. There are two kinds of Builder in the case, CommonBuilder and MDBuilder. If you want to implement other general dialog, you can inherit from FRBaseDialogBuilder.

1. Usage:

1.1 ordinary Dialog

private void showCommonDialog() {
    final FRDialog dialog = new FRDialog.CommonBuilder(this)
            .setContentView(R.layout.dialog_common)
            .setText(R.id.dcu_tv_cancel, "no")
            .setText(R.id.dcu_tv_confirm, "yes")
            .setText(R.id.dcu_tv_title, "Reminder")
            .setText(R.id.dcu_tv_content, "1.Text text I am text text text I am text text text I am text!\n2.Text text text text\n3.Text text text text")
            .setDefaultAnim()
            .show();

    dialog.setText(R.id.dcu_tv_confirm, "Determine");

    dialog.setOnClickListener(R.id.dcu_tv_cancel, new FRDialogClickListener() {
        @Override
        public boolean onDialogClick(View view) {
            Toast.makeText(MainActivity.this, "Click No.", Toast.LENGTH_SHORT).show();
            return true;
        }
    });

    dialog.setOnClickListener(R.id.dcu_tv_confirm, new FRDialogClickListener() {
        @Override
        public boolean onDialogClick(View v) {
            Toast.makeText(MainActivity.this, "Click Yes.", Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

1.2,MaterialDesign Dialog

private void showMDDialog() {
    FRDialog dialog = new FRDialog.MDBuilder(this)
            .setMessage("1.Text text I am text text text I am text text text I am text!\n2.Text text text text\n3.Text text text text")
            .setTitle("Reminder")
            .setNegativeAndPositive("no", "yes")
            .setPositiveListener(new FRDialogClickListener() {
                @Override
                public boolean onDialogClick(View view) {
                    return true;
                }
            }).show();
}

2. Special settings:

Inherit all dialog settings, and customize the following settings

//Set width full screen
dialog.setFullWidth()

//Settings pop up from bottom
dialog.setFromBottom()

//Animating pop ups
dialog.setAnimation(int anim)

Updated on May 24, 2018

Change mWidth to mWidthOffset. Instead of setting a specific width, let the user set a width proportion. Then set the width and height of dialog by changing the LayoutParams of window:

WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int) (baseBuilder.mContext.getResources().getDisplayMetrics().widthPixels * baseBuilder.mWidthOffset);
lp.height = baseBuilder.mHeight;
window.setAttributes(lp);

The usage is the same as before:

dialog.setWidthOffset(0-1) is 0.9 by default

Updated on July 19, 2018

New click the non EditText area of dialog to hide the soft keyboard

Rewrite dispatchTouchEvent Methods to intercept:

/**
 * Click the area other than EditText in the dialog to hide the soft keyboard
 *
 * @param ev
 * @return
 */
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    FRInputMethodManager.autoHideSoftInput(this, ev);
    return super.dispatchTouchEvent(ev);
}

//Core method
public static boolean isAutoHideSoftInput(View view, MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return false;
    }

    if (!(view instanceof EditText)) {
        return false;
    }

    float x = event.getX();
    float y = event.getY();

    int[] location = {0, 0};
    view.getLocationInWindow(location);
    int left = location[0];
    int top = location[1];
    int bottom = top + view.getHeight();
    int right = left + view.getWidth();
    if (left <= x && x < right && top <= y && y < bottom) {
        // Click event in EditText area
        return false;
    }

    return true;
}

Usage is the same.

Updated on September 11, 2018

New recyclerview style dialogBuilder

Specific functions include:

  • To set a list layout, just pass an adapter
  • You can add headers and bottoms to the list
  • You can add headers and bottoms to the dialog

Usage:

    private void showRecyclerViewDialog() {
        List<Object> mDataList = new ArrayList<>();
        mDataList.add(new TestDataBean("Zhang San", "2018-09-11 14:00"));
        mDataList.add(new TestDataBean("Li Si", "2018-09-11 11:00"));
        mDataList.add(new TestDataBean("Wang Wu", "2018-09-11 12:00"));
        mDataList.add(new TestDataBean("Li Si", "2018-09-11 13:00"));
        mDataList.add(new TestDataBean("Zhang San", "2018-09-11 16:00"));
        mDataList.add(new TestDataBean("Wang Wu", "2018-09-11 15:00"));


        final FRDialog dialog = new FRDialog.RecyclerViewBuilder(this)
                .setLayoutManager(new LinearLayoutManager(MainActivity.this))
                .setAdapter(new FRBaseDialogAdapter<TestDataBean>(MainActivity.this) {

                    @Override
                    protected int getLayoutRes() {
                        return R.layout.item_test;
                    }

                    @Override
                    protected void convert(FRBaseDialogViewHolder holder, TestDataBean dataBean, int position, List<Object> payloads) {
                        holder.setImageResource(R.id.it_iv_image, R.mipmap.ic_launcher_round);
                        holder.setText(R.id.it_tv_title, dataBean.getName());
                        holder.setText(R.id.it_tv_time, dataBean.getTime());
                    }
                }).setDataList(mDataList)
                .setHeightOffset(0.5)
                .addRecyclerViewHeader(R.layout.layout_header)
                .addDialogFooter(R.layout.layout_footer)
                .setOnClickListener(R.id.lf_tv_cancel, view -> true)
                .setOnClickListener(R.id.lf_tv_confirm, view -> {
                    Toast.makeText(MainActivity.this, "Click OK", Toast.LENGTH_SHORT).show();
                    return false;
                })
                .show();
    }

Refer to in the demo for details.

Posted by gingerboy101 on Sat, 07 Dec 2019 16:13:16 -0800