Android
Android - Dialog 내의 EditText Padding, Margin 조절하기
TechNote.kr
2016. 2. 1. 23:40
728x90
환경 정보
Android API 23 (Android 6.0) Android Studio 1.5.1. |
이 글의 목표는 아래와 같이 유난히 긴 EditText를 상위 Text와 같이 align을 맞춰 줄여주는 것이다.
. EditText의 길이가 짧아졌다.
[Before] |
[After] |
|
|
Dialog에 EditText를 추가하는 코드를 구해서 코드에 추가하였다.
AlertDialog.Builder alert = new AlertDialog.Builder(CategoryActivity.this); alert.setTitle("Add Box"); // Set an EditText view to get user input final EditText input = new EditText(CategoryActivity.this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); //value.toString(); helper_category.insert(value); // Do something with value! refreshCategory(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert.show();
위와 같이 코드를 추가하니 아래와 같이 Dialog UI가 나타났다.
위에서 보듯이 EditText 의 칸이 양옆으로 끝까지 뻗어 있어 예뻐 보이지 않았다.
그래서 EditText에 margin 혹은 padding 을 주어서 좀 칸안으로 줄여 보고자 하였다.
구글링을 해보면 여러가지 방법이 있었지만 나 같은 경우는 동작하지 않았고, 결국은 Dialog UI를 XML로 분리하고 LinearLayout을 이용해 구현하였다.
1. Dialog XML 을 선언해준다
XML내의 EditText에서 layout_marginLeft, layout_marginRight를 20dp로 정의해 주었다.
res\layout\addboxdialog.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="match_parent"
android:paddingRight="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/addboxdialog"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2. Java Code는 다음과 같다.
EditText를 Code상에서 호출하여 setView를 하지 않고 R.layout.addboxdialog (xml)을 setView로 호출하였다.
AlertDialog.Builder alert = new AlertDialog.Builder(CategoryActivity.this); alert.setTitle("Add Box"); alert.setView(R.layout.addboxdialog); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Dialog f = (Dialog) dialog; EditText input = (EditText) f.findViewById(R.id.addboxdialog); String value = input.getText().toString(); //value.toString(); helper_category.insert(value); // Do something with value! refreshCategory(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); AlertDialog dialog = alert.create(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); alert.show();
728x90