한국어

스마트폰앱

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유

   ◎위챗 : speedseoul


  
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/



Android DialogFragment Tutorial

In this tutorial, you will learn how to implement a DialogFragment in your Android application. A DialogFragment is a fragment that displays a dialog window, floating on top of its activity’s window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment’s state. Alternatively, you can create an entirely custom dialog, such as an AlertDialog, with its own content. We will create buttons that will show a DialogFragment and a custom Alert DialogFragment.

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project DialogFragmentTutorial.

Application Name : DialogFragmentTutorial

Project Name : DialogFragmentTutorial

Package Name : com.androidbegin.dialogfragmenttutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.androidbegin.dialogfragmenttutorial;
 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends FragmentActivity {
 
    Button dfragbutton;
    Button alertdfragbutton;
    FragmentManager fm = getSupportFragmentManager();
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);
 
        // Locate the button in activity_main.xml
        dfragbutton = (Button) findViewById(R.id.dfragbutton);
        alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton);
 
        // Capture button clicks
        dfragbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                DFragment dFragment = new DFragment();
                // Show DialogFragment
                dFragment.show(fm, "Dialog Fragment");
            }
        });
 
        // Capture button clicks
        alertdfragbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                AlertDFragment alertdFragment = new AlertDFragment();
                // Show Alert DialogFragment
                alertdFragment.show(fm, "Alert Dialog Fragment");
            }
        });
    }
}

In this activity, we have created two buttons that will show different dialog fragments. The first button will show a simple DialogFragment and another with a custom Alert DialogFragment.

Next, create an XML graphical layout for the MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<RelativeLayout 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" >
 
    <Button
        android:id="@+id/dfragbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/dialogfragment" />
 
    <Button
        android:id="@+id/alertdfragbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dfragbutton"
        android:layout_centerInParent="true"
        android:text="@string/alertdialogfragment" />
 
</RelativeLayout>

Next, create a fragment. Go to File > New > Class and name it DFragment.java. Select your package named com.androidbegin.dialogfragmenttutorial and click Finish.

Open your DFragment.java and paste the following code.

DFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.androidbegin.dialogfragmenttutorial;
 
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class DFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialogfragment, container,
                false);
        getDialog().setTitle("DialogFragment Tutorial");       
        // Do something else
        return rootView;
    }
}

Next, create an XML graphical layout for the DialogFragment. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file dialogfragment.xml and paste the following code.

dialogfragment.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<RelativeLayout 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" >
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:text="@string/welcome" />
 
</RelativeLayout>

Next, create another fragment. Go to File > New > Class and name it AlertDFragment.java. Select your package named com.androidbegin.dialogfragmenttutorial and click Finish.

Open your AlertDFragment.java and paste the following code.

AlertDFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.androidbegin.dialogfragmenttutorial;
 
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
 
public class AlertDFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                // Set Dialog Icon
                .setIcon(R.drawable.androidhappy)
                // Set Dialog Title
                .setTitle("Alert DialogFragment")
                // Set Dialog Message
                .setMessage("Alert DialogFragment Tutorial")
 
                // Positive button
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something else
                    }
                })
 
                // Negative Button
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,    int which) {
                        // Do something else
                    }
                }).create();
    }
}

In this fragment, we have created custom alert DialogFragment that consist of two buttons, a positive and negative. We have prepared a sample alert dialog icon for this tutorial. Insert your downloaded sample alert dialog icon into your res > drawable-hdpi.

Sample Alert Dialog Icon

Androidhappy (1.2 KiB, 179 downloads)

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">DialogFragment Tutorial</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="welcome">This is a DialogFragment.</string>
    <string name="dialogfragment">Open DialogFragment</string>
    <string name="alertdialogfragment">Open Alert DialogFragment</string>
 
</resources>

No changes needed for the AndroidManifest.xml.

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.dialogfragmenttutorial"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Output:

DialogFragment ScreenShots

Source Code

조회 수 :
12944
등록일 :
2014.02.09
18:24:26 (*.251.139.148)
엮인글 :
http://webs.co.kr/index.php?document_srl=38610&act=trackback&key=4cc
게시글 주소 :
http://webs.co.kr/index.php?document_srl=38610
List of Articles
번호 제목 글쓴이 날짜 조회 수
66 mjsip sip sample code program admin 2019-09-07 53
65 Package org.linphone.core Description admin 2017-10-22 4769
64 Sqlite detail easy tutorial. admin 2017-09-09 4938
63 Change package name Linphone Android admin 2017-08-25 5104
62 Liblinphone - import Linphone library in Android Studio 2017 admin 2017-08-25 5457
61 I am able to build the limphone on mac by follow the steps describe here admin 2017-08-25 5014
60 To build liblinphone for Android, you must COMPILATION INSTRUCTIONS admin 2017-08-25 5332
59 sipdroid source code admin 2017-08-08 5110
58 안드로이드 주소록 전체가져오기 이름만가져오기 사진가져오기 코드 admin 2015-04-13 24859
57 the sipdroid Research Miscellaneous admin 2015-03-26 8386
56 HSS070 English Korean 무료 국제전화 미국 중국 카나다 무료 통화 제공 admin 2014-12-28 14441
55 Softphones admin 2014-09-20 10774
54 /xxxxx/gen already exists but is not a source folder. Convert to a source folder or rename it. admin 2014-09-03 13955
53 CSIPSimple building Rebuilding Detail on Eclips Good All is heare perfect admin 2014-08-15 11698
52 Improving QoE of SIP-based Automated Voice Interaction in Mobile Networks file admin 2014-03-20 12520
51 speex support in android admin 2014-03-19 11900
50 voxmobile Source and how to build admin 2014-02-18 13214
49 PJSIP hung the phone constantly corrected the problem admin 2014-02-09 13185
48 Csipsimple code rebuilding source sip 통신 Call Mechanism admin 2014-02-09 73399
47 Csipsimple code rebuilding source sip 통신 technical interviewer admin 2014-02-09 13208
46 Csipsimple code rebuilding source 주요인터페이스분석 admin 2014-02-09 177659
45 Csipsimple code rebuilding source 다운로드 구성 csipsimple 소스 프로젝트 admin 2014-02-09 15045
» Android DialogFragment Tutorial admin 2014-02-09 12944
43 Creating a fullscreen DialogFragment with a custom background admin 2014-02-09 16104
42 [Android API] 네트워크 상태 체크하기 admin 2013-12-19 18208
41 [안드로이드] 네트워크 연결 상태 체크 함수. admin 2013-12-19 14779
40 안드로이드 인터넷연결체크 ( 3g/lte/wifi ) 메서드 admin 2013-12-19 20760
39 Android Get Phone Contacts details with Contact Image admin 2013-12-15 13845
38 안드로이드 커스텀 타이틀바 How to add custom title bar to android application admin 2013-10-02 17026
37 How to create a custom title bar admin 2013-07-15 15455