Reputation: 11608
I use an EditText
inside an AlertDialog
like:
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
final EditText input = new EditText(getActivity());
alert.setView(input);
Now I'd like to set margins for this EditText
so it doesn't take the full width of the dialog. How can I do this? (I do not consider the option of inflating a custom Layout
at this point)
Upvotes: 3
Views: 3433
Reputation: 307
if you don't want to inflate a custom Layout, Try this
AlertDialog.Builder dialog = new AlertDialog.Builder(Login.this);
final EditText email_input = new EditText(Login.this);
LinearLayout linearLayout = new LinearLayout(Login.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
email_input.setHint("Email Address");
email_input.setLayoutParams(layoutParams);
linearLayout.addView(email_input);
linearLayout.setPadding(60, 0, 60, 0);
dialog.setTitle("Forgot Password?");
dialog.setMessage("Enter Your Email Address");
dialog.setView(linearLayout);
dialog.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Some Code
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
dialog.show();
Upvotes: 7
Reputation: 4954
You can pass spacing parameter in setView
method
alert.setView(view ,left_space , top_space , right_space , bottom_space);
Upvotes: 5
Reputation: 5425
For this kind of stuff, you will have to have a custom dialog box:
Here's how you can do this:
Step 1: Create a CustomDialog.java
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
public class CustomDialogClass extends Dialog{
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.customdialog);
}
}
Step 2: Create a custom 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"
android:background="#F3F3F4"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2sp"
android:background="#000000" >
</LinearLayout>
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="@string/hello"
android:textColor="#00BFFF"
android:textSize="35sp"
android:textStyle="bold" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2sp"
android:background="#000000" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F3F3F4"
android:orientation="horizontal" >
<TextView
android:id="@+id/navigationid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10sp"
android:text="@string/navigation"
android:textSize="22sp"
android:textStyle="normal"
android:textColor="#00BFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2sp"
android:background="#000000" >
</LinearLayout>
</LinearLayout>
Step 3 : In you main activity in the onCreate(); just call this:
final CustomDialogClass dialog = new CustomDialogClass(TutorialActivity.this);
dialog.show();
//This part is optional. Just to close the dialog after few seconds.
final Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
dialog.dismiss();
}
}, 5000);
That's it..So simple .. :)
Upvotes: 0
Reputation: 6709
Try using the setPadding (int left, int top, int right, int bottom)
method on your EditText
before setting is as a view in the AlertDialog
.
You can use negative padding to emulate margins in some circumstances.
Upvotes: 1