안드로이드

안드로이드 커스텀 다이얼로그 custom dialog 만들기

알통몬_ 2017. 3. 25. 13:43
반응형


안녕하세요 알통몬입니다.

공감 및 댓글은 포스팅 하는데 아주아주 큰 힘이 됩니다!!

포스팅 내용이 찾아주신 분들께 도움이 되길 바라며

더 깔끔하고 좋은 포스팅을 만들어 나가겠습니다^^

 

이번 포스팅에서는 안드로이드에서 Dialog를 각자의 취향에 맞게

커스텀 다이얼로그를 만드는 방법에 대해 알아보겠습니다.


먼저 다이얼로그를 띄울려면 액티비티가 먼저 있어야겠죠?


필요한 파일은 4가지인데요.

MainActivity.java, CustomDialog.java, activity_main.xml, dialog_custom.xml입니다.


바로 코드를 보겠습니다.

activity_main.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:orientation="vertical">

            <Button

                android:id="@+id/openDialogBtn"

                android:layout_width="100dp"

                android:layout_height="50dp"

                android:text="OpenDialog"

                android:textSize="20sp"

                android:textColor="#000000" />

</LinearLayout>

-----------------------------------------------------------------------------------------------



MainActivity.java

------------------------------------------------------------------------------------------------

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final int LAYOUT = R.layout.activity_main;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(LAYOUT);

        Button openDialogBtn = (Button) findViewById(R.id.openDialogBtn);

       openDialogBtn.setOnClickListener(this);

    }


    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.openDialogBtn :

                CustomDialog dialog = new CustomDialog(this);

                dialog.setTitle("find password"); // 다이얼로그 제목.

                dialog.show();

                break;

        }

    }

}

------------------------------------------------------------------------------


위처럼 Main을 구성했으면 이제 커스텀 다이얼로그의 xml을 먼저 구성하고,
다이얼로그 안에서 실행될 코드들을 Dialog 클래스를 상속받은 CustomDialog에 작성하면 됩니다.
 저는 아래처럼 구성해봤습니다.




dialog_custom.xml
-------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:weightSum="10"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp">

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <EditText
        android:hint="이름"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.5"/>
    <EditText
        android:hint="전화번호"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.5"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:gravity="center">
        <TextView
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="#000000"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="아이디 찾기"/>
        <TextView
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="#000000"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="취소"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5"/>
</LinearLayout>
----------------------------------------------------------------------------------


CustomDialog.java

----------------------------------------------------------------------------

public class CustomDialog extends Dialog {


    private static final int LAYOUT = R.layout.dialog_custom;


    public CustomDialog(Context context) {

        super(context);

    }


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(LAYOUT);

    }

}

---------------------------------------------------------------------------------
*액티비티.java에 하는거 처럼 똑같이 기능을 구현하시면 됩니다~~~

커스텀 다이얼로그 만드는거 별로 어렵지않죠?

기능도 다양하게 넣을 수 있습니다.


다음 포스팅에서는 TabLayout + ViewPager를 구현하는 방법에 대해 공부하겠습니다.


반응형