MainActivity.java
Shows the button, set the items for the drop down list, creates the pop up window and then show it as drop down when the button was touched
DogsDropdownOnItemClickListener.java
Triggered when an item on the drop down list was touched, it will change the text on the button and show a toast with the ID of the selected item.
- package com.example.showasdropdownexample;
-
- import android.content.Context;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.TextView;
-
- public class DogsDropdownOnItemClickListener implements OnItemClickListener {
-
- String TAG = "DogsDropdownOnItemClickListener.java";
-
- @Override
- public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
-
-
- Context mContext = v.getContext();
- MainActivity mainActivity = ((MainActivity) mContext);
-
-
- Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in);
- fadeInAnimation.setDuration(10);
- v.startAnimation(fadeInAnimation);
-
-
- mainActivity.popupWindowDogs.dismiss();
-
-
- String selectedItemText = ((TextView) v).getText().toString();
- mainActivity.buttonShowDropDown.setText(selectedItemText);
-
-
- String selectedItemTag = ((TextView) v).getTag().toString();
- Toast.makeText(mContext, "Dog ID is: " + selectedItemTag, Toast.LENGTH_SHORT).show();
-
- }
-
- }
Activity_main.xml – Here is the XML layout file we used for the user interface of our example.
- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <Button
- android:id="@+id/buttonShowDropDown"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="Select your dog..." />
-
- </RelativeLayout>