You may face certain scenario where you want to input single value from user and that value is not simple text. let’s say you want to display a custom widget. In such cases, instead of creating a new activity or dialog, you may show a popup window containing a widget. This article explains how you can show a popup containing a widget to user and then get value of the displayed widget.
Step 1: Creating the popup window XML file:
First you need to create an XML file in your res/layout directory that will contain all you widgets that you want to display in the popup window. Be sure to assign id to the first container element in your XML file as we need to reference it later. A sample XML file looks like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PopupLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#777" >
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnOK"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/timePicker1"
android:layout_marginLeft="61dp"
android:layout_marginTop="20dp"
android:text="Done" />
</RelativeLayout>
Step 2: Displaying the popup window:
The next step is to display this popup window. In your activity class, Invoke the popup window by inflating the layout XML, and assign the appropriate "parent view" to the pop-up. Sample code to perform these step
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.PopupLayout1)); final PopupWindow pw = new PopupWindow(layout,400,300,true); pw.showAtLocation(findViewById(R.id.LinearLayout1), Gravity.CENTER, 0, 0);
The code above assumes that
- the name of your XML layout file is "popup"
- the id of first container element in your popup layout is PopupLayout1
- you want to display popup of 400×300 size
- the id of first container element in your main activity is LinearLayout1
Step 3: Getting value from a widget in popup:
Now to get the value from widget, you have to handle some event. For this sample application we have added a button in the popup window and when user clicks that button, we are displaying the value from widget to a text view in main activity. Every thing else is simple, just one thing to remember here. When we get the widget using findViewById, we normally do not specify view object as we want to call method of current view, but in this case be sure to specify view object. Sample code
Button btnOk = (Button) layout.findViewById(R.id.btnOK);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimePicker tp = (TimePicker) layout.findViewById(R.id.timePicker1);
Time tm = new Time(tp.getCurrentHour(), tp.getCurrentMinute(), 0);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText( tm.toString());
pw.dismiss();
}
});
The code above assumes that
- you have a button with id btnOk in your popup window
- you have a time picker with id timePicker1 in your popup window
- you have a text view with id textView1 in your main activity
Final Activity Class:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen = (Button) findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayPopup();
}
});
}
private void displayPopup()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.PopupLayout1));
final PopupWindow pw = new PopupWindow(layout,400,300,true);
pw.showAtLocation(findViewById(R.id.LinearLayout1), Gravity.CENTER, 0, 0);
Button btnOk = (Button) layout.findViewById(R.id.btnOK);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimePicker tp = (TimePicker) layout.findViewById(R.id.timePicker1);
Time tm = new Time(tp.getCurrentHour(), tp.getCurrentMinute(), 0);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText( tm.toString());
pw.dismiss();
}
});
}
}
Hope this helps.









