Simply customize a Dialog with rounded corners

Nowadays, more and more apps are using Dialog. Why is it that when writing Dialog, it always fails to meet people’s expectations and displays different things to us? In the recent company project appeared this east east, the following pit I met to tell you, I hope to help.

start

(Although this is the first time to write a blog, but I still understand the rules, ha ha) :

This is a very, very simple home page with only one Button in it;



This is followed by clicking the button to pop up a dialog box



This is the cancel button on the pop-up dialog box



This is the ok button on the pop-up dialog box

The distance between the border and the screen border can not be determined, there is no rounded corner, listening can not be added…

Code to also

package cn.ddh.administrator.mydialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/** * this is my main screen */
public class MainActivity extends AppCompatActivity {
    private Button button;
    Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.btn_dialog);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { showdialog(); }}); }/** * A dialog box is displayed */
    private void showdialog() {
     View view=View.inflate(this,R.layout.dialog_cancel_indent,null);
    dialog = new MyDialog(this.200.100, view, R.style.dialog);
        dialog.show();
  final TextView cancel = 
                 (TextView) view.findViewById(R.id.cancel);
  final TextView confirm =        
                 (TextView)view.findViewById(R.id.confirm);
    cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this."Hit the cancel button.", Toast.LENGTH_SHORT).show();
                Log.d("ddh"."onClick: "+ cancel); dialog.dismiss(); }}); confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this."Hit the ok button", Toast.LENGTH_SHORT).show();
                Log.d("ddh"."onClick: "+ confirm); dialog.dismiss(); }}); }}Copy the code

Here you will be surprised to find a red, don’t worry… A little MyDialog, two layout files, a Style, wait a minute

This is a custom MyDialog class. I don’t know, I just know that if I don’t write it, my rounded corners won’t work.

package cn.ddh.administrator.mydialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/** * Created by yellow on 2017/4/8. ** Created by Yellow on 2017/4/8

public class MyDialog extends Dialog{
    private static int default_width = 300; // Default width

    private static int default_height = 200;// Default height



    public MyDialog(Context context, View layout, int style) {

        this(context, default_width, default_height, layout, style);

    }



    public MyDialog(Context context, int width, int height, View layout, int style) {

        super(context, style); setContentView(layout); Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.gravity = Gravity.CENTER; window.setAttributes(params); }}Copy the code

This is obviously a style, inherited from the style/Theme.Dialog style. This is to set the various colors of the screen, and the properties can be changed according to their own needs.

dialog

<!"> < span style =" max-width: 100%; clear: both;
    <style name="dialog" parent="@android:style/Theme.Dialog">

        <item name="android:windowFrame">@null</item>

        <item name="android:windowIsFloating">true</item>

        <item name="android:windowIsTranslucent">true</item>

        <item name="android:windowNoTitle">true</item>

        <item name="android:background">@android:color/transparent</item>

        <item name="android:windowBackground">@android:color/transparent</item>

        <item name="android:backgroundDimEnabled">true</item>

        <item name="android:backgroundDimAmount">0.6</item>



    </style>Copy the code

The end of style is the most important custom layout

dialog_cancel_indent


       
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:background="@drawable/dialog">
<! The padding is added to expand the width of the dialog.
        <TextView
            android:gravity="center"
            android:layout_marginTop="50dp"
            android:id="@+id/textView10"
            android:textColor="#7b7b7b"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Confirm cancellation of this order"/>

        <TextView
            android:layout_gravity="center_horizontal"
            android:layout_width="300dp"
            android:layout_height="1dp"
            android:background="#ebebeb"


            />

        <LinearLayout
            android:layout_gravity="center_horizontal"
            android:gravity="center_vertical"
            android:layout_width="300dp"
            android:orientation="horizontal"
            android:layout_height="50dp">
            <TextView
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:id="@+id/cancel"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#fd7e2a"
                android:text="Cancel"
                />
            <TextView
                android:layout_gravity="center_vertical"
                android:layout_width="1dp"
                android:background="#ebebeb"
                android:layout_height="30dp"/>

            <TextView
                android:layout_weight="1"
                android:textColor="#fd7e2a"
                android:layout_gravity="center_vertical"
                android:id="@+id/confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Sure"
                />

        </LinearLayout>





</LinearLayout>Copy the code

The outer LinearLayout has a background

dialog


       
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <! - the rounded - >
    <corners
        android:radius="9dp"
        /><! -- Set fillet radius -->
    <! - fill - >
    <solid
        android:color="@android:color/white"/><! -- Fill color -->
</shape>Copy the code

This shape clearly controls the rounded corners, so that’s where the code ends.

The end of the

A few words (concluding remarks)

The whole piece of code, the most important thing is three sentences

    View view;
    view=View.inflate(this,R.layout.dialog_cancel_indent,null);
    dialog = new MyDialog(this, 200.100, view, R.style.dialog);
        dialog.show(a);Copy the code

So my Dialog has been called the simplest Dialog ever, doesn’t it feel like that; Note: when initializing the button in the dialog box, it should

 final TextView cancel = 
                 (TextView) view.findViewById(R.id.cancel);
  final TextView confirm =        
                 (TextView)view.findViewById(R.id.confirm);Copy the code

The width of my Dialog is not as wide as I want it to be. Look at the layout. Add the padding inside the outer LinearLayout so that the margins are stretched too much. Once stretched, the dark gray dividing line in the middle of me will be broken. Our project needs that dividing line to fill the Dialog. So I did some work on that dividing line:

Just like I fixed his width when making the dividing line, there are many minutes method, you like it

android:layout_width="300dp"Copy the code

Note: I use the resolution of 720×1280, because it is the first time to write blog, there may be a lot of deficiencies, but also please correct more children, xiao Huang here thank you;