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.
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>
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
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
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.
3 Comments
Thank you for this article…
It helps me so much, especially at the describe of the “View” object by using => ” layout.findViewById “..
It’s so clear and so important but newbite always may miss…
I’m very very appreciate…
Nice
Thank you,
Nice tutorial!
Thanks
I ve popwindow containing 3 edit text and my main layout contains a list view I have to store the EditText values in Array and display in ListView Can You Help me.Thanks in advance