MainActivity.java – show the button, set the items for the dropdown list, create the pop up window and then show it as dropdown when the button was touched.
  1. package com.example.showasdropdownexample;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.view.WindowManager;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Button;
  10. import android.widget.ListView;
  11. import android.widget.PopupWindow;
  12. import android.widget.TextView;
  13. import android.app.Activity;
  14. import android.graphics.Color;
  15. public class MainActivity extends Activity {
  16. String TAG = "MainActivity.java";
  17. String popUpContents[];
  18. PopupWindow popupWindowDogs;
  19. Button buttonShowDropDown;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. // initialize pop up window items list
  25. // add items on the array dynamically
  26. // format is DogName::DogID
  27. List<String> dogsList = new ArrayList<String>();
  28. dogsList.add("Akita Inu::1");
  29. dogsList.add("Alaskan Klee Kai::2");
  30. dogsList.add("Papillon::3");
  31. dogsList.add("Tibetan Spaniel::4");
  32. // convert to simple array
  33. popUpContents = new String[dogsList.size()];
  34. dogsList.toArray(popUpContents);
  35. // initialize pop up window
  36. popupWindowDogs = popupWindowDogs();
  37. // button on click listener
  38. View.OnClickListener handler = new View.OnClickListener() {
  39. public void onClick(View v) {
  40. switch (v.getId()) {
  41. case R.id.buttonShowDropDown:
  42. // show the list view as dropdown
  43. popupWindowDogs.showAsDropDown(v, -5, 0);
  44. break;
  45. }
  46. }
  47. };
  48. // our button
  49. buttonShowDropDown = (Button) findViewById(R.id.buttonShowDropDown);
  50. buttonShowDropDown.setOnClickListener(handler);
  51. }
  52. public PopupWindow popupWindowDogs() {
  53. // initialize a pop up window type
  54. PopupWindow popupWindow = new PopupWindow(this);
  55. // the drop down list is a list view
  56. ListView listViewDogs = new ListView(this);
  57. // set our adapter and pass our pop up window contents
  58. listViewDogs.setAdapter(dogsAdapter(popUpContents));
  59. // set the item click listener
  60. listViewDogs.setOnItemClickListener(new DogsDropdownOnItemClickListener());
  61. // some other visual settings
  62. popupWindow.setFocusable(true);
  63. popupWindow.setWidth(250);
  64. popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  65. // set the list view as pop up window content
  66. popupWindow.setContentView(listViewDogs);
  67. return popupWindow;
  68. }
  69. /*
  70. * adapter where the list values will be set
  71. */
  72. private ArrayAdapter<String> dogsAdapter(String dogsArray[]) {
  73. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dogsArray) {
  74. @Override
  75. public View getView(int position, View convertView, ViewGroup parent) {
  76. // setting the ID and text for every items in the list
  77. String item = getItem(position);
  78. String[] itemArr = item.split("::");
  79. String text = itemArr[0];
  80. String id = itemArr[1];
  81. // visual settings for the list item
  82. TextView listItem = new TextView(MainActivity.this);
  83. listItem.setText(text);
  84. listItem.setTag(id);
  85. listItem.setTextSize(22);
  86. listItem.setPadding(10, 10, 10, 10);
  87. listItem.setTextColor(Color.WHITE);
  88. return listItem;
  89. }
  90. };
  91. return adapter;
  92. }
  93. }
DogsDropdownOnItemClickListener.java – triggered when an item on the dropdown list was touched, it will change the text on the button and show a toast with the ID of the selected item.
  1. package com.example.showasdropdownexample;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.animation.Animation;
  5. import android.view.animation.AnimationUtils;
  6. import android.widget.AdapterView;
  7. import android.widget.Toast;
  8. import android.widget.AdapterView.OnItemClickListener;
  9. import android.widget.TextView;
  10. public class DogsDropdownOnItemClickListener implements OnItemClickListener {
  11. String TAG = "DogsDropdownOnItemClickListener.java";
  12. @Override
  13. public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
  14. // get the context and main activity to access variables
  15. Context mContext = v.getContext();
  16. MainActivity mainActivity = ((MainActivity) mContext);
  17. // add some animation when a list item was clicked
  18. Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in);
  19. fadeInAnimation.setDuration(10);
  20. v.startAnimation(fadeInAnimation);
  21. // dismiss the pop up
  22. mainActivity.popupWindowDogs.dismiss();
  23. // get the text and set it as the button text
  24. String selectedItemText = ((TextView) v).getText().toString();
  25. mainActivity.buttonShowDropDown.setText(selectedItemText);
  26. // get the id
  27. String selectedItemTag = ((TextView) v).getTag().toString();
  28. Toast.makeText(mContext, "Dog ID is: " + selectedItemTag, Toast.LENGTH_SHORT).show();
  29. }
  30. }
activity_main.xml – Here is the XML layout file we used for the user interface of our example.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <Button
  11. android:id="@+id/buttonShowDropDown"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_centerHorizontal="true"
  15. android:layout_centerVertical="true"
  16. android:text="Select your dog..." />
  17. </RelativeLayout>