// DialogDemo.java

 

 package com.example.hellow;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DialogDemo extends Activity{
 Button list, radio, check, dialog1, login, progress, datePickerFragment;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_demo);

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

  alert.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    new AlertDialog.Builder(DialogDemo.this)
    .setIcon(R.drawable.ic_launcher)
    .setMessage(R.string.dialog_message)
    .setTitle(R.string.dialog_title)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(DialogDemo.this, "OK Button Click", Toast.LENGTH_SHORT).show();
     }
    })
    .setNegativeButton("NO", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(DialogDemo.this, "NO Button Click", Toast.LENGTH_SHORT).show();
     }
    })
    //.create()
    .show();    
   }
  });

 


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

  list.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    new AlertDialog.Builder(DialogDemo.this)
    .setTitle(R.string.pick_color)
    .setItems(R.array.color_array, new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {

      switch(which){
      case 0 : list.setBackgroundColor(Color.RED); break;
      case 1 : list.setBackgroundColor(Color.GREEN); break;
      case 2 : list.setBackgroundColor(Color.BLUE); break;
      }
     }
    })
    .show();    
   }
  });

 

 

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

  radio.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    new AlertDialog.Builder(DialogDemo.this)
    .setTitle(R.string.pick_color)
    .setSingleChoiceItems(R.array.color_array, -1, new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {

      switch(which){
      case 0 : radio.setBackgroundColor(Color.RED); break;
      case 1 : radio.setBackgroundColor(Color.GREEN); break;
      case 2 : radio.setBackgroundColor(Color.BLUE); break;
      }
     }
    })
    .setNeutralButton("Close", null)
    .show();    
   }
  });

 


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

  check.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    new AlertDialog.Builder(DialogDemo.this)
    .setTitle(R.string.pick_color)
    .setMultiChoiceItems(R.array.color_array, null,
      new DialogInterface.OnMultiChoiceClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
      String color="";
      if(isChecked){
       if(which == 0){
        check.setBackgroundColor(Color.RED);
        color = "red";
       } else if(which == 1){
        check.setBackgroundColor(Color.GREEN);
        color = "green";
       } else if(which == 2){
        check.setBackgroundColor(Color.BLUE);
        color = "blue";
       }

       Toast.makeText(DialogDemo.this, "color is " + color + "!!!", Toast.LENGTH_SHORT).show();
      } else {
       if(which <= 2){
        check.setBackgroundColor(Color.GRAY);
       }

       Toast.makeText(DialogDemo.this, "color unselected!!!", Toast.LENGTH_SHORT).show();
      }
     }
    })
    .setNeutralButton("Close", null)
    .show();    
   }
  });


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

  dialog1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    FireMissilesDialogFragment fragment = new FireMissilesDialogFragment();
    Bundle bundle = new Bundle();
    fragment.setContext(DialogDemo.this);
    fragment.onCreateDialog(bundle).show();    
   }
  });


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

  login.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    showAlertDialog();
   }
  });
  
  
  
  progress = (Button)findViewById(R.id.progress);

  progress.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    ProgressDialog dialog = new ProgressDialog(DialogDemo.this);
    dialog.setTitle("Indeterminage");
    dialog.setMessage("Please wait while loading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    dialog.show();
   }
  });
  
  
  
  datePickerFragment = (Button)findViewById(R.id.datePickerFragment);

  datePickerFragment.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    DatePickerFragment fragment = new DatePickerFragment();
    Bundle bundle = new Bundle();
    fragment.setContext(DialogDemo.this);
    fragment.onCreateDialog(bundle).show();    
   }
  });
 }


 private void showAlertDialog(){
  LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  LinearLayout loginLayout = (LinearLayout) vi.inflate(R.layout.logindialog, null);

  final EditText id = (EditText) loginLayout.findViewById(R.id.id);
  final EditText pw = (EditText) loginLayout.findViewById(R.id.pw);

  new AlertDialog.Builder(this).setTitle("로그인").setView(loginLayout)
  .setNeutralButton("확인", new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(DialogDemo.this, "ID : " + id.getText().toString() + "\nPW : " + pw.getText().toString(), Toast.LENGTH_SHORT).show();
   }
  }).show();
 }
}

 

 

// DatePickerFragment.java

 

 package com.example.hellow;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
 Context ctx;
 
 public void setContext(Context ctx){
  this.ctx = ctx;
 }

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
  // Use the current date as the default date in the picker
  final Calendar c = Calendar.getInstance();
  int year = c.get(Calendar.YEAR);
  int month = c.get(Calendar.MONTH);
  int day = c.get(Calendar.DAY_OF_MONTH);

  // Create a new instance of DatePickerDialog and return it
  return new DatePickerDialog(ctx, this, year, month, day);
 }

 public void onDateSet(DatePicker view, int year, int month, int day) {
  Toast.makeText(ctx, year+"/"+month+"/"+day, Toast.LENGTH_SHORT).show();
 }
}

 

 

// FireMissilesDialogFragment.java

 

 package com.example.hellow;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

public class FireMissilesDialogFragment extends DialogFragment {

 Context ctx;

 public void setContext(Context ctx){
  this.ctx = ctx;

 }

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
  AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
  builder.setMessage(R.string.dialog_fire_missiles)

  .setPositiveButton("Fire", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
    //
   }
  })
  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
    //
   }
  });
  return builder.create();
 }
}

 

 

// dialog_demo.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/alert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="simple alert dialog" />
   
    <Button
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="simple list dialog" />
   
    <Button
        android:id="@+id/radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="simple radio dialog" />
   
    <Button
        android:id="@+id/check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="simple check dialog" />
   
    <Button
        android:id="@+id/dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_label5" />

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_label6" />
   
    <Button
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_label7" />
   
    <Button
        android:id="@+id/datePickerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_label8" />
</LinearLayout>

 

// logindialog.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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="아이디" />

        <EditText
            android:id="@+id/id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="비밀번호" />

        <EditText
            android:id="@+id/pw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"/>
    </LinearLayout>

</LinearLayout>

 

+ Recent posts