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

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(...)

Related

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.

Showing two RelativeLayouts in a LinearLayout not working

I'm trying to display two RelativeLayouts in a LinearLayout, however the best result I'm getting is getting one of the two displayed.
final LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutAll = (RelativeLayout) inflate.inflate(
R.layout.activity_main, null);
final RelativeLayout menuLayout = (RelativeLayout)inflate.inflate(
R.layout.bottom_menu, null);
menuLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layoutMain.addView(layoutAll);
layoutMain.addView(menuLayout);
layoutAll is showing, and if I change the order of the addView lines, then menuLayout is showing. So far I've tried different layout parameters (MATCH_PARENT for layoutAll or menuLayout and even both). Any suggestion ? Thanks.

Open PopupWindow in PopupWindow

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.

How to make clickable overlay activity android? [duplicate]

I'm developing lock screen. And I need my lock screen activity be over all apps.
So, I have this:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.lock_screen, null); // lock_screen is .xml file
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
It works, but not at all. Everything is frozen, the navigation baris working, but touch screen is not. How can I make only my activity be over everything and can't quit it?
This is because you've only overlaid a view, not the activity.

Update result to TextView of AlertDialog Android

When I tried to set the result of my program to a textView in a dialog box , the app force closed. I made the dialog box by linking an xml which has a textview and it is this textview that I tried to update.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
resultOne=(TextView)findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultOne.setText("hello"); //this code is making the app close
final View textEntryView = factory.inflate(R.layout.dialog, null);
alert.setView(textEntryView);
alert.show();
Change the order so that you acess the View's children after inflating it. You will also need to use textEntryView to find the id, like so:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultOne.setText("hello");
alert.setView(textEntryView);
alert.show();

Categories