Currently, I have a button, within a Linear Layout
, like so:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/open_popup"
android:onClick="createPopup"/>
</LinearLayout>
I also have a createPopup extension (which does indeed execute when the button is clicked).
public void createPopup(View view) {
}
I have tried to create a PopupWindow
with a TextView
within it. I then called showAtLocation(parent, gravity, x, y)
where parent
is the root LinearLayout
. gravity
was Gravity.BOTTOM
, x
and y
were both set to 10
.
When I clicked the button, I received an IllegalStateException
error where I called showAtLocation()
. Here is the createPopup()
function:
PopupWindow popUp;
LinearLayout layout;
LinearLayout main;
TextView value;
LayoutParams params;
boolean click = true;
public void createPopup(View view) {
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
main = (LinearLayout) findViewById(R.id.parent);
value = new TextView(this);
if(click) {
popUp.showAtLocation(main, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
value.setText(R.string.message);
layout.addView(value, params);
popUp.setContentView(layout);
setContentView(main);
TextView status = (TextView) findViewById(R.id.status);
status.setTextSize(28);
status.setText("Status of Popup: Don't worry, you're never going to get this.");
}
What I would like to know is:
*I'd like to be able to create a function where it takes at least one parameter (which would be a String
), and then create's a popup window that contains that String
, as well as a button to close the popup window.
EDIT: Not sure if this is connected to the popup issue, but when I run the app, I get this error:
07-13 19:51:48.448 133-133/? E/[EGL-ERROR]: egl_image* _egl_create_image_ANDROID_native_buffer(egl_display*, egl_context*, EGLClientBuffer, EGLint*, void*):593: CHUN try create image with crop 0,0,0,0
[ 07-13 19:51:48.448 133: 133 E/ ]
CHUN map external image rgb 1024 x 600
SECOND EDIT: I've added relevant code above.
If you are looking at how to implement a Dialog, what I normally do is create a separate layout file, class file, and call it from where it needs to be shown. Dialogs show over the other activity in the center, and as far as I know, they cannot be moved.
When you want to show the dialog, you can call:
private void openMyDialog() {
MyDialog dialog = new MyDialog(this);
dialog.show();
}
MyDialog.java needs to extend Dialog
and implement android.view.View.OnClickListener
. It will contain any code necessary to save or control the elements on your layout.
MyDialog.java:
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
public class MyDialog extends Dialog implements android.view.View.OnClickListener {
public Activity activity;
public MyDialog() {
super(null);
}
public MyDialog(Activity activity) {
super(activity);
this.activity = activity;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
}
dismiss();
}
}
my_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1C1C1C">
<!-- Whatever your layout will consist of can go here -->
</RelativeLayout>
I hope this answers your question. If it does not, feel free to comment on this post. I do not know how to fix your error (as it is not the full error) but I do know how to implement custom Dialogs (or Popups) to go over your other activity.