Created
June 7, 2014 09:26
-
-
Save Phonbopit/eaf6262dcc2c30c0d1d3 to your computer and use it in GitHub Desktop.
Android Dialog Example , post on : http://phonbopit.com/how-to-show-android-dialog/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="16dp"> | |
<TextView | |
android:text="Android Dialog Example" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/text_title" | |
android:textSize="22sp" | |
android:textColor="#D74B4C" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="8dp"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_simple" | |
android:text="Simple Dialog" | |
android:gravity="center" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_marginTop="16dp" | |
android:layout_below="@+id/text_title" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_list" | |
android:text="List Dialog" | |
android:gravity="center" | |
android:layout_marginTop="16dp" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_below="@+id/button_dialog_simple" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_single_choice" | |
android:text="Single Choice Dialog" | |
android:gravity="center" | |
android:layout_marginTop="16dp" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_below="@+id/button_dialog_list" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_multi_choice" | |
android:text="Multi Choice Dialog" | |
android:gravity="center" | |
android:layout_marginTop="16dp" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_below="@+id/button_dialog_single_choice"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_custom" | |
android:text="Custom Dialog 1" | |
android:gravity="center" | |
android:layout_marginTop="16dp" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_below="@+id/button_dialog_multi_choice" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button_dialog_custom2" | |
android:text="Custom Dialog 2" | |
android:gravity="center" | |
android:layout_marginTop="16dp" | |
android:padding="16dp" | |
android:textSize="20sp" | |
android:textColor="#FFFFFF" | |
android:background="#D74B4C" | |
android:layout_below="@+id/button_dialog_custom" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="64dp" | |
android:text="Login" | |
android:gravity="center" | |
android:textSize="32sp" /> | |
<EditText | |
android:id="@+id/username" | |
android:inputType="textEmailAddress" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Username" /> | |
<EditText | |
android:id="@+id/password" | |
android:inputType="textPassword" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Password"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<EditText | |
android:id="@+id/username" | |
android:inputType="textEmailAddress" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:layout_marginLeft="4dp" | |
android:layout_marginRight="4dp" | |
android:layout_marginBottom="4dp" | |
android:hint="Username" /> | |
<EditText | |
android:id="@+id/password" | |
android:inputType="textPassword" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="4dp" | |
android:layout_marginLeft="4dp" | |
android:layout_marginRight="4dp" | |
android:layout_marginBottom="16dp" | |
android:hint="Password"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:weightSum="1"> | |
<Button | |
android:id="@+id/button_cancel" | |
android:layout_weight="0.5" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:padding="16dp" | |
android:text="Cancel"/> | |
<Button | |
android:id="@+id/button_login" | |
android:layout_weight="0.5" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:padding="16dp" | |
android:text="Login"/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.phonbopit.sample.dialog; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class MainActivity extends ActionBarActivity { | |
private Button mSimpleDialog; | |
private Button mListDialog; | |
private Button mSingleChoiceDialog; | |
private Button mMultiChoiceDialog; | |
private Button mCustomDialog1; | |
private Button mCustomDialog2; | |
private static final String[] NAMES = {"KitKat", "Jelly Bean", | |
"Ice Cream Sandwich", "Honeycomb", "Gingerbread", "Froyo"}; | |
private int mIndexSelected; | |
private ArrayList<Integer> mMultiSelected; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Link to all View that declare in activity_main.xml | |
mSimpleDialog = (Button) findViewById(R.id.button_dialog_simple); | |
mListDialog = (Button) findViewById(R.id.button_dialog_list); | |
mSingleChoiceDialog = | |
(Button) findViewById(R.id.button_dialog_single_choice); | |
mMultiChoiceDialog = | |
(Button) findViewById(R.id.button_dialog_multi_choice); | |
mCustomDialog1 = (Button) findViewById(R.id.button_dialog_custom); | |
mCustomDialog2 = (Button) findViewById(R.id.button_dialog_custom2); | |
// Set OnClickListener each button with difference dialog | |
mSimpleDialog.setOnClickListener(SimpleDialogOnClick); | |
mListDialog.setOnClickListener(ListDialogOnClick); | |
mSingleChoiceDialog.setOnClickListener(SingleDialogOnClick); | |
mMultiChoiceDialog.setOnClickListener(MultiDialogOnClick); | |
mCustomDialog1.setOnClickListener(CustomDialogOnClick); | |
mCustomDialog2.setOnClickListener(CustomDialog2OnClick); | |
} | |
View.OnClickListener SimpleDialogOnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
AlertDialog.Builder builder = | |
new AlertDialog.Builder(MainActivity.this); | |
builder.setMessage("Are you ready?"); | |
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
Toast.makeText(getApplicationContext(), | |
"OK, Let's go!", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
builder.setNegativeButton("No, Thanks", null); | |
builder.show(); | |
} | |
}; | |
View.OnClickListener ListDialogOnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
AlertDialog.Builder builder = | |
new AlertDialog.Builder(MainActivity.this); | |
builder.setTitle("Select Your Favorite Name?"); | |
builder.setItems(NAMES, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
String selected = NAMES[which]; | |
Toast.makeText(getApplicationContext(), "Select " + | |
selected, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
builder.setNegativeButton("No, Thanks", null); | |
builder.show(); | |
} | |
}; | |
View.OnClickListener SingleDialogOnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
AlertDialog.Builder builder = | |
new AlertDialog.Builder(MainActivity.this); | |
builder.setTitle("Select Your Favorite Name?"); | |
//You need to set default item checked when open Dialog by use mIndexSelected. | |
builder.setSingleChoiceItems(NAMES, mIndexSelected, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
// store index with you choose in mIndexSelected. | |
mIndexSelected = which; | |
} | |
}); | |
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Toast.makeText(getApplicationContext(), "Select: " + | |
NAMES[mIndexSelected], Toast.LENGTH_SHORT).show(); | |
dialog.dismiss(); | |
} | |
}); | |
builder.setNegativeButton("No", null); | |
builder.show(); | |
} | |
}; | |
View.OnClickListener MultiDialogOnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mMultiSelected = new ArrayList<Integer>(); | |
AlertDialog.Builder builder = | |
new AlertDialog.Builder(MainActivity.this); | |
builder.setTitle("Select Your Favorite Name?"); | |
builder.setMultiChoiceItems(NAMES, null, new DialogInterface.OnMultiChoiceClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which, boolean isChecked) { | |
if (isChecked) { | |
mMultiSelected.add(which); | |
} else if (mMultiSelected.contains(which)) { | |
mMultiSelected.remove(Integer.valueOf(which)); | |
} | |
} | |
}); | |
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
StringBuffer buffer = new StringBuffer(); | |
for (Integer team : mMultiSelected) { | |
buffer.append(" "); | |
buffer.append(NAMES[team]); | |
} | |
Toast.makeText(getApplicationContext(), "Select:" + | |
buffer.toString(), Toast.LENGTH_SHORT).show(); | |
dialog.dismiss(); | |
} | |
}); | |
builder.setNegativeButton("No, Thanks", null); | |
builder.show(); | |
} | |
}; | |
View.OnClickListener CustomDialogOnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
AlertDialog.Builder builder = | |
new AlertDialog.Builder(MainActivity.this); | |
LayoutInflater inflater = getLayoutInflater(); | |
View view = inflater.inflate(R.layout.custom_dialog, null); | |
builder.setView(view); | |
final EditText username = (EditText) view.findViewById(R.id.username); | |
final EditText password = (EditText) view.findViewById(R.id.password); | |
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
// Check username password | |
if (username.getText().equals("[email protected]") && | |
password.getText().equals("demo")) { | |
Toast.makeText(getApplicationContext(), "Login success!", | |
Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(getApplicationContext(), "Login Failed!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}); | |
builder.show(); | |
} | |
}; | |
View.OnClickListener CustomDialog2OnClick = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
final Dialog dialog = new Dialog(MainActivity.this); | |
dialog.setTitle("Phonbopit Login System"); | |
dialog.setContentView(R.layout.custom_dialog2); | |
final EditText username = (EditText) dialog.findViewById(R.id.username); | |
final EditText password = (EditText) dialog.findViewById(R.id.password); | |
Button buttonCancel = (Button) dialog.findViewById(R.id.button_cancel); | |
Button buttonLogin = (Button) dialog.findViewById(R.id.button_login); | |
buttonCancel.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
dialog.dismiss(); | |
} | |
}); | |
buttonLogin.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Check username password | |
if (username.getText().equals("[email protected]") && | |
password.getText().equals("demo")) { | |
Toast.makeText(getApplicationContext(), "Login success!", | |
Toast.LENGTH_SHORT).show(); | |
dialog.dismiss(); | |
} else { | |
Toast.makeText(getApplicationContext(), "Login Failed!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
dialog.show(); | |
} | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment