PopupWindow

Create the PopupWindow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PopupWindow popup;
 
 
 
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 
     LayoutInflater inflater = (LayoutInflater)
                  getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     popup= new PopupWindow(inflater.inflate(R.layout.popup_layout,null,false));
     popup.setFocusable(true);
     popup.setOutsideTouchable(true);
     //If you want it to work correctly, be sure to add a background.
     popup.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.popup_background));
     popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
PopupWindow popup;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     LayoutInflater inflater = (LayoutInflater)
			      getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     popup= new PopupWindow(inflater.inflate(R.layout.popup_layout,null,false));
     popup.setFocusable(true);
     popup.setOutsideTouchable(true);
     //If you want it to work correctly, be sure to add a background.
     popup.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.popup_background));
     popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

Making a change to the PopupWindow:

1
2
TextView text = ((TextView) popup.getContentView().findViewById(R.id.popupText));
text.setText("PopupWindow Example");
TextView text = ((TextView) popup.getContentView().findViewById(R.id.popupText));
text.setText("PopupWindow Example");

Showing the PopupWindow:

1
2
if(!popup.isShowing())
     popup.showAsDropDown(getActivity().findViewById(R.id.display_below_me), 5, 5);
if(!popup.isShowing())
     popup.showAsDropDown(getActivity().findViewById(R.id.display_below_me), 5, 5);

popup_layout.xml

1
2
3
4
5
6
7
8
9
10
< ?xml version="1.0" encoding="utf-8"?>
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:baselineAligned="false"
     android:orientation="vertical" android:layout_width="wrap_content"
     android:layout_height="wrap_content">
 
     <textview android:id="@+id/popupText" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 
</linearlayout>
< ?xml version="1.0" encoding="utf-8"?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:baselineAligned="false"
     android:orientation="vertical" android:layout_width="wrap_content"
     android:layout_height="wrap_content">

     <textview android:id="@+id/popupText" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</linearlayout>

Common Intents

Open Browser

1
2
3
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
     * Creates an intent to send some text to another app.
     */
    public void SendText() {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        // emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new
        // String[]{"webmaster@website.com"});
        // emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT,
        // mySubject);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, mEditArea
                .getText().toString());
        startActivity(Intent.createChooser(emailIntent, "Send text..."));
 
    }
/**
	 * Creates an intent to send some text to another app.
	 */
	public void SendText() {
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		emailIntent.setType("plain/text");
		// emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new
		// String[]{"webmaster@website.com"});
		// emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT,
		// mySubject);
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, mEditArea
				.getText().toString());
		startActivity(Intent.createChooser(emailIntent, "Send text..."));

	}
1
2
3
4
5
6
7
8
9
/**
     * Sends text as a text message.
     */
    public void SMSText() {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", mEditArea.getText().toString());
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);
    }
/**
	 * Sends text as a text message.
	 */
	public void SMSText() {
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		sendIntent.putExtra("sms_body", mEditArea.getText().toString());
		sendIntent.setType("vnd.android-dir/mms-sms");
		startActivity(sendIntent);
	}

Simple DialogFragment

This class can handle multiple dialogs by specifying the “type” argument, and adding any additional arguments you may need.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class MultiDialog extends DialogFragment {
 
//This is how we will know which dialog to display.
private int DialogType = 0;
 
//Our ID for a dialog that displays a file's properties. This
//dialog requires an additional argument with key "filename"
public static final int DIALOG_PROPERTIES = 0;
 
DialogCallbacks mDialogCallbacks;
 
public void setDialogCallback(DialogCallbacks callback){
mDialogCallbacks = callback;
}
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
DialogType = getArguments().getInt("type");
 
}
 
@Override
public void onStart(){
super.onStart();
 
switch (DialogType) {
case DIALOG_PROPERTIES: {
 
String f = getArguments().getString("filename");
 
if(f!=null){
 
File mFile = new File(f);
 
// get size units i.e. B,KB,MB
double size = mFile.length();
int count;
for (count = 0; size > 1024; count++)
size = size / 1024;
 
// set the message text
((AlertDialog) getDialog()).setMessage(getString(
R.string.file_properties_text, mFile.getName(), "charset",
FileOps.getPathOnly(mFile.getAbsolutePath()), String.format(
"%1$.1f%2$s", size, FileOps.FileSizeUnits[count])));
 
}}
 
}
 
}
 
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
 
switch(DialogType){
case DIALOG_PROPERTIES: {
 
return new AlertDialog.Builder(getActivity())
.setTitle("File Properties")
.setMessage("Unsaved file.")
.setCancelable(true)
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();// close dialog
}
}).create();
 
}
 
default:
return null;
}
 
}
 
}
public class MultiDialog extends DialogFragment {

//This is how we will know which dialog to display.
private int DialogType = 0;

//Our ID for a dialog that displays a file's properties. This
//dialog requires an additional argument with key "filename"
public static final int DIALOG_PROPERTIES = 0;

DialogCallbacks mDialogCallbacks;

public void setDialogCallback(DialogCallbacks callback){
mDialogCallbacks = callback;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DialogType = getArguments().getInt("type");

}

@Override
public void onStart(){
super.onStart();

switch (DialogType) {
case DIALOG_PROPERTIES: {

String f = getArguments().getString("filename");

if(f!=null){

File mFile = new File(f);

// get size units i.e. B,KB,MB
double size = mFile.length();
int count;
for (count = 0; size > 1024; count++)
size = size / 1024;

// set the message text
((AlertDialog) getDialog()).setMessage(getString(
R.string.file_properties_text, mFile.getName(), "charset",
FileOps.getPathOnly(mFile.getAbsolutePath()), String.format(
"%1$.1f%2$s", size, FileOps.FileSizeUnits[count])));

}}

}

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

switch(DialogType){
case DIALOG_PROPERTIES: {

return new AlertDialog.Builder(getActivity())
.setTitle("File Properties")
.setMessage("Unsaved file.")
.setCancelable(true)
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();// close dialog
}
}).create();

}

default:
return null;
}

}

}