Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText in AlertDialog seems too wide

It seems like the EditText in the image below is too wide. I assume that I have misused the SDK in some way and until convinced otherwise I am not looking for a way to specify some number of margin/padding pixels on the sides of the EditText.

enter image description here

This one looks more appropriate.

enter image description here

Here's my code (that creates the first, 'Create Tag', dialog):

final Dao<Tag, Integer> tagDao = getHelper().getTagDao();

final EditText input = new EditText(this);
input.setSingleLine(true);
input.setHint(R.string.create_tag_dialog_hint);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setTitle(getString(R.string.create_tag_dialog_title));
builder.setPositiveButton(
    getString(R.string.create_tag_dialog_positive),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            Tag tag = new Tag(value);
            try {
                    tagDao.create(tag);
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
        }
    });
builder.setNegativeButton(
    getString(R.string.create_tag_dialog_negative), null);
builder.show();

Sorry for the length of the post and thanks for any helpful comments.

like image 882
altendky Avatar asked Feb 07 '11 22:02

altendky


People also ask

How do I set the width and height of an AlertDialog?

show(); alertDialog. getWindow(). setLayout(600, 400); //Controlling width and height. Or you can do it in my way.

How do I make alert dialog fill 90% of screen size?

Builder adb = new AlertDialog. Builder(this); Dialog d = adb. setView(new View(this)). create(); // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.


Video Answer


1 Answers

Just sorted this myself. Using an instance of AlertDialog, you can specify setView and pass in spacing parameters. This will work.

final EditText input = new EditText(this);

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setView(input, 10, 0, 10, 0); // 10 spacing, left and right
alertDialog.setButton("OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Clicked
    }
});
alertDialog.show();

Edit: I'm aware this question is old, but no solution was provided.

like image 56
Ricky Avatar answered Oct 09 '22 09:10

Ricky