Resize EditText (any view) shown in alertBox?

I want to show alertBox for user input. So I added EditText View to alertBox. code of my alertBox:

final EditText input = new EditText(this);
            //input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            input.setX(10);
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
            builder.setMessage("Title")
                    .setCancelable(false)
                    .setView(input)
                    .setTitle("Create new Playlist")
                    .setPositiveButton("CREATE",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // finish the current activity
                                    // AlertBoxAdvance.this.finish();
                                    playlistName = input.getText().toString();
                                    dialog.cancel();


                                }
                            })
                    .setNegativeButton("CANCEL",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // cancel the dialog box
                                    dialog.cancel();
                                }
                            });
            android.app.AlertDialog alert = builder.create();
            alert.show();

This gives the following alert dialog:

enter image description here

But EditText does not start from where the Header and Message header starts (indentation is not correct). Therefore, I want the next type of EditText to have the correct indentation in the alert dialog.

enter image description here

How to do it? How to change the position of views in the notification dialog? Or how to resize a view?

+4
source share
2 answers

Try the following:

XML layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editTextField"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

</LinearLayout>

Java Code:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
    LayoutInflater inflater = mActivity.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog, null);
    dialogBuilder.setView(dialogView);

final EditText mEditText = (EditText) dialogView.findViewById(R.id.editTextField);

dialogBuilder.setTitle("Title");
dialogBuilder.setMessage("Type your message here");

dialogBuilder.setPositiveButton("Yes", null);
dialogBuilder.setNegativeButton("No", null);

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String enteredTextString= mEditText.getText().toString();
                //To whatever with the text entered
            }
        });
        Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        negativeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

    }
});
alertDialog.show();
+1
source

... EditText , , lay.xml

builder.setMessage("Title")
                .setCancelable(false)
                .setContentView(R.layout.lay);
+1

Source: https://habr.com/ru/post/1660147/


All Articles