Open PopupWindow in PopupWindow - java

I want to open a PopUpWindow in another PopUpWindow. I have an ImageButtons in my MainActivity. When I click on it a PopUpWindow appears. I use it as a kind of submenu in my app. In my first PopupWindow is another ImageButton. If I click on it a second PopupWindow should appear and overlay the first one.
Opening the first PopupWindow works just fine. When I click on the button in it to open the second one, the app crashes. How can I make the second PopupWindow work?
Thanks for your help.
I tried it likes this:
final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.button_name);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_fertig, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(btnOpenPopup, Gravity.TOP | Gravity.RIGHT, 0, 0);
Button btn_2 = (Button) popupView.findViewById(R.id.button_2);
btn_2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater_2
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView_2 = layoutInflater.inflate(R.layout.popup_2, null);
final PopupWindow popupWindow_2 = new PopupWindow(
popupView_2,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow_2.showAtLocation(btn_2, Gravity.TOP | Gravity.RIGHT, 0, 0);
}
}
});
}

I'm also trying to do what you are doing but was not successful however I did figure out a workaround for what you want to do.
Inside your popupView xml layout, you would have to create a framelayout as the parent layout and then put both your popupView and popupView 2 layout within the parent layout. You would then switch on and off the visibility for each of the two layouts when a button is pressed in the popupView.
It actually works quite nicely, the popupView resizes itself appropriately according to the content it holds with smooth animations.

Related

Multiple denpendent Spinner in Android

In my app there is a Spinner for colors and a Button for the user to add additional spinners in different colors. An existing spinner is coupled with a delete button for removal upon creation. It looks like this -
I'm trying to accomplish the following:
The user clicks "ADD PAINTYPE"
A Spinner is created, given it is not already present in the List
If the user deletes the item, it is available to insert again.
I´m pretty new to Android App development, and this logic is difficult for me to work through. Any suggestions on how to approach this would be appreciated.
Edit: Blow down is the function, which called after user click the button "ADD PAINTYPE". The arrayAdapter of the newly created spinner is as same as the fisrt one(which in the left side of the "ADD PAINTYPE" button). Right now all Spinners are sharing the sam adapter. What I want to achieve is, When I choose a Option in one spinner, then dynamiclly the option will not be listed in other spinners.(e.g. The "ANFALLSTARTIGER" would not be listed in the second and the third spinners). Is this possible?
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected void addItem(Context context,View view) {
if(clickedTimes < 16) {
clickedTimes++;
final LinearLayout linearLayout = new LinearLayout(context);
Spinner spinner = new Spinner(context);
spinner.setPopupBackgroundResource(R.color.white);
spinner.setBackgroundColor(getResources().getColor(R.color.white));
final Button button = new Button(context);
final Button button_delete = new Button(context);
button_delete.setText("DELETE");
button_delete.setTextColor(getResources().getColor(R.color.white));
button_delete.setBackground(context.getDrawable(R.drawable.mybutton));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(6);
LinearLayout.LayoutParams params_0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params_0);
LinearLayout.LayoutParams params_1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4.0f);
spinner.setLayoutParams(params_1);
params_1.setMargins(2, 2, 2, 2);
LinearLayout.LayoutParams params_2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
params_2.setMargins(2, 2, 2, 2);
button.setLayoutParams(params_2);
button_delete.setLayoutParams(params_2);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.all_paintype);
layout.addView(linearLayout);
linearLayout.addView(spinner);
linearLayout.addView(button);
linearLayout.addView(button_delete);
button_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layoutAll = (LinearLayout) view.findViewById(R.id.all_paintype);
layoutAll.removeView(linearLayout);
spinners.remove(spinner);
clickedTimes--;
}
});
dropBoxButton(spinner, button, clickedTimes);
spinners.add(spinner);
}else {
Toast toast = Toast.makeText(getContext(),"Maximal Pain Type",Toast.LENGTH_SHORT);
toast.show();
}
}
protected void dropBoxButton(Spinner spinner, final Button button, int clickedTimes) {
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(clickedTimes);
spinner.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string_1 = ((TextView) view).getText().toString();
setButtonColor(button, string_1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

textView.setText does not update text but also gives no errors

I have a PopupWindow that has a TextView in it. I am trying to change the text in this TextView dynamically but can't seem to get it to work. The code executes, but it does nothing and gives no errors, making it hard to understand what's happening (as there is nothing that shows up in the debug logs). It's important to note that this TextView is not in the same layout as the layout given to setContentView - and that menu is called when a button is pushed in the main layout. Does anybody have any idea why this could be happening?
Here is my code:
public void popupView(View view, int viewPointer){//viewPointer is R.id.popup_layout
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(viewPointer, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height,true);//add another parameter here (true) to make it so it goes away when clicked outside the menu itself
popupWindow.setElevation(20);
// show the popup window
// which view you pass in doesn't matter
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
and
public void menu(View view){
textView.setText("this won't change the text but will execute with zero errors...");
popupView(view, R.layout.popup_layout);
}
and in onCreate:
LayoutInflater layoutInflater = getLayoutInflater();
View text_view = layoutInflater.inflate(R.layout.popup_layout, null);
textView = text_view.findViewById(R.id.textViewID);

Android: Is it possible to set parent of PopupWindow?

I displayed PopupWindow in fragment and it works well except when side menu appears. Side menu is displayed behind the PopupWindow which I don't want.
So I think it would be good to set parent of PopupWindow or maybe zOrder.
LinearLayout contentView = (LinearLayout) inflate(getContext(), R.layout.callout_merge_destination, null);
PopupWindow mCallout = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mCallout.showAsDropDown(view3, x, y);
Please let me know the best solution.

Android popup on activity create

I am using the following code to create a popup which shows as the activity loads.
I call loadingPopup(); in my oncreate method.
The popup works the problem is when ever i try to place onclick listener for the dismiss button the whole thing crashes on load. If i were to remove the DissMissButton onclick listener code it works fine. Any help would be appreciated
private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.popup_welcome, null);
final Button DismissButton = (Button) findViewById(R.id.button_welcomepopup_dismiss);
final PopupWindow Welcome_popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
Welcome_popupWindow.setFocusable(false);
Welcome_popupWindow.setTouchable(true);
Welcome_popupWindow.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
Welcome_popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
DismissButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Welcome_popupWindow.dismiss();
}
});
}
});
You call find view on your activity view, while your button is in the popup, so of course the DismissButton is null and app force close.
Change the find view code to this:
final Button DismissButton = (Button) layout.findViewById(R.id.button_welcomepopup_dismiss);

How do I create a floating window using onClick attribute in Android?

So I'm working on an Android app (in Eclipse) and I've hit a wall. In my app I have a drawer that slides out with a list of options. I would like the user to be able to click one of the options and to bring up a floating window with a form in it. I'm trying to do this using the onClick attribute on the buttons rather than using a onClickListener. Is this possible without having to use a onClickListener or am I trying to avoid the inevitable? The button's onClick attribute in my layout has a value of "newWindow".
My MainActivity class
public void newWindow(View v){
Intent intent = new Intent(){
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
};
startActivity(intent);
}
I see my popupView variable is unused but I'm not sure where to place or if I'm even headed in the right direction. Thanks in advance for the help!
I can't for the life of me figure out why you are trying to start an Activity with an Intent here. Your code shold be:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(...); // or showAsDropdown(...)

Categories