How to create a Custom Dialog box in android?

后端 未结 22 2762
囚心锁ツ
囚心锁ツ 2020-11-21 07:06

I want to create a custom dialog box like below

\"enter

I have tried the foll

相关标签:
22条回答
  • 2020-11-21 07:45

    Add the below theme in values -> style.xml

    <style name="Theme_Dialog" parent="android:Theme.Light">
         <item name="android:windowNoTitle">true</item>
         <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    

    Use this theme in your onCreateDialog method like this:

    Dialog dialog = new Dialog(FlightBookActivity.this,R.style.Theme_Dialog);
    

    Define your dialog layout including title bar in the xml file and set that xml file like this:

    dialog.setContentView(R.layout.your_dialog_layout);
    
    0 讨论(0)
  • 2020-11-21 07:46

    Here is a very simple way to create a custom dialog.

    dialog.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="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <!--  Put your layout content  -->
    </LinearLayout>
    

    MainActivity.java

    ShowPopup(){
    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.dialog, null);
    android.app.AlertDialog.Builder alertDialogBuilder = new 
    android.app.AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptsView);
    alertDialogBuilder.setCancelable(true);
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    }
    
    0 讨论(0)
  • 2020-11-21 07:47

    I found this as the easiest way for showing custom dialog.

    You have layout your_layout.xml

    public void showCustomDialog(final Context context) {
        Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.your_layout, null, false);
        findByIds(view);  /*HERE YOU CAN FIND YOU IDS AND SET TEXTS OR BUTTONS*/
        ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        dialog.setContentView(view);
        final Window window = dialog.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawableResource(R.color.colorTransparent);
        window.setGravity(Gravity.CENTER);
        dialog.show();
    }
    
    0 讨论(0)
  • 2020-11-21 07:48

    Import custom alert :

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.dse_location_list_filter, null);
    final Dialog dialog = new Dialog(Acitvity_name.this);
    dialog.setContentView(view);
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);
    dialog.show();
    
    0 讨论(0)
  • 2020-11-21 07:49

    Simplest way to change the background color and text style is to make custom theme for android alert dialog as below :-

    : Just put below code to styles.xml :

        <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#999999</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:typeface">monospace</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:textSize">@dimen/abc_text_size_medium_material</item>
        <item name="android:background">#80ff00ff</item>
    </style>
    

    : Now customization thing is done , now just apply to your alertBuilder object :

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.AlertDialogCustom);
    

    Hope , this will help you !

    0 讨论(0)
  • 2020-11-21 07:50

    Another easy way to do this.

    step 1) create a layout with proper id's.

    step 2) use the following code wherever you desire.

    LayoutInflater factory = LayoutInflater.from(this);
    final View deleteDialogView = factory.inflate(R.layout.mylayout, null);
    final AlertDialog deleteDialog = new AlertDialog.Builder(this).create();
    deleteDialog.setView(deleteDialogView);
    deleteDialogView.findViewById(R.id.yes).setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
            //your business logic 
            deleteDialog.dismiss();
        }
    });
    deleteDialogView.findViewById(R.id.no).setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
            deleteDialog.dismiss();    
        }
    });
    
    deleteDialog.show();
    
    0 讨论(0)
提交回复
热议问题