Reputation: 179
It is possible using Dialog class but I do not want it that way. Instead I want it to be done by using PopupWindow class which gets popped up on startup and display some message on the popup. I am helpless, just can not getting this after spending many days behind it. Hope I get it here. Please and Thanks. Also look at below snippet if you didn't get what I want..
public class PopupActivity extends Activity implements OnClickListener {
LinearLayout ll = new LinearLayout(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(ll);
}
protected void onStart() {
super.onStart();
final PopupWindow pw;
Button button = new Button(this);
button.setText("Hello");
pw = new PopupWindow(button, 245, 284, true);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
pw.dismiss();
}
});
pw.showAsDropDown(ll, 10, -50);
}
}
Above code gives me FORCE CLOSE :/ Help guys..
Upvotes: 2
Views: 7995
Reputation: 68167
this is how you may show a popup window.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/layoutTemp">
</LinearLayout>
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up" />
</LinearLayout>
main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Handler().postDelayed(new Runnable() {
public void run() {
showPopup();
}
}, 100);
}
public void showPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
}
The idea is that your popup will be displayed once your activity is loaded, otherwise it will produce exception Unable to add window -- token null is not valid; is your activity running?. Unless you show popup on a button click, that is the reason I'm showing it after a delay of 100 milliseconds (which is almost unnoticeable).
Upvotes: 3