안드로이드

안드로이드 다이얼로그 생성방법

톰이야요 2023. 7. 3. 08:00

안드로이드 앱에서 다이얼로그를 띄우는 방법입니다.

메인액티비티에서 다이얼로그를 표시하는 방법은 아래와 같습니다.

 

 

다이얼로그 레이아웃을 만듭니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/textview_score_name4"
                style="@style/sub_subtitle_text"
                />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_score_graph_linechart"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/chart_score_graph"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                style="@style/sub_chart"/>
        </LinearLayout>
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

위는 차트를 표시하는 다이얼로그 레이아웃입니다. 간단히 텍스트만 표시하는 걸로 테스트 해보셔도 됩니다.

 

 

다이얼로그 클래스를 생성합니다.

 public GraphDialog(Context context, List<DataControl> datalist) {
        super(context);
        setContentView(R.layout.graph_dialog);

        m_nScore_For_Graph = new ArrayList<DataControl>(datalist);

        layout_linechart = findViewById(R.id.layout_score_graph_linechart);

        DrawScoreGraph();
    }

위 다이얼로그 클래스는 생성과 동시에 데이터를 받기 위해 리스트를 전달받도록 했습니다.

 

 

메인액티비티에서 다이얼로그를 호출합니다.

// 가점변화 그래프 다이얼로그 //////////////////////////
        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                graphDialog = new GraphDialog(MainActivity.this, m_nScore_For_Graph);                
                graphDialog.show();
            }
        });

m_nScore_For_Graph 변수를 다이얼로그 클래스에 전달하면서 다이얼로그 클래스를 생성합니다.

 

 

 

다이얼로그를 띄우는 간단한 방법입니다!