Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add bigger margin to EditText in Android AlertDialog

I have an EditText inside an AlertDialog. It looks like this.

enter image description here

See where it says tddjdjck and how it is indented quite a lot. That is what I want (I used setPadding with left and right set to 50), but I also want the blue line under it to be indented too. How do I do that?

The code I am using is below:

            final AlertDialog.Builder alert = new AlertDialog.Builder(thisActivity);             final EditText input = new EditText(thisActivity);             input.setSingleLine();             input.setPadding(50, 0, 50, 0);              alert.setTitle("by...");             alert.setMessage("enter the name of the person who did:");             alert.setView(input);             alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int whichButton) {                     String value = input.getText().toString().trim();                  }             });              alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int whichButton) {                     dialog.cancel();                 }             });             alert.show();   

Thank you

like image 958
b85411 Avatar asked Jan 05 '15 06:01

b85411


People also ask

How can I customize AlertDialog in Android?

Step 1: Create a XML file: custom_layout. Add the below code in custom_layout. xml. This code defines the alertdialog box dimensions and add a edittext in it.

Is AlertDialog deprecated?

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

How do I change the font in AlertDialog?

If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog . <style name="MyTheme" parent="Theme. AppCompat.


2 Answers

final AlertDialog.Builder alert = new AlertDialog.Builder(thisActivity); final EditText input = new EditText(thisActivity); input.setSingleLine(); FrameLayout container = new FrameLayout(thisActivity); FrameLayout.LayoutParams params = new  FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.leftMargin = getResources().getDimensionPixelSize(R.dimen.dialog_margin); input.setLayoutParams(params); container.addView(input); alert.setTitle("by..."); alert.setMessage("test message"); alert.setView(container); 

Make sure you add another line to your dimens.xml resource file, such as

<dimen name="dialog_margin">20dp</dimen> 
like image 171
Ashton Engberg Avatar answered Oct 15 '22 04:10

Ashton Engberg


You can pass spacing parameter in setView method

alert.setView(view ,left_space , top_space , right_space , bottom_space); 

So,in your case you can try this

alert.setView(input , 50 ,0, 50 , 0); 
like image 36
Bhargav Thanki Avatar answered Oct 15 '22 05:10

Bhargav Thanki