Android. How to show popup window directly above button - java

I need to show popup window directly above button. My button is placed inside scroll view, and popup always shown below button. Here is my code:
private void showPopup(View view, String text) {
if (infoPopup == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View popupView = layoutInflater.inflate(R.layout.popup_credit_request_passport, null);
TextView tvPopupText = popupView.findViewById(R.id.tv_popup_text);
tvPopupText.setMovementMethod(new ScrollingMovementMethod());
tvPopupText.setText(text);
FrameLayout flBackground = popupView.findViewById(R.id.fl_background);
flBackground.setBackground(new BubbleDrawable(getContext(), R.color.azure, 16, 16, 8));
infoPopup = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
infoPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
infoPopup.setOutsideTouchable(true);
infoPopup.showAtLocation(view, Gravity.TOP, (int) view.getX(), (int) view.getY());
} else {
dismissInfoPopup();
}
}
But it is not work. Please hep me.

Change Gravity to Top and reset your coordinates according to your requirement.
infoPopup.showAtLocation(view, Gravity.TOP, 0, (int) view.getY());

When you have to show the popup, find out the location of the button in the screen, then use the showAtLocation() method as Prachi Said using Gravity.TOP and the x,y coordinates of the button on screen...

Related

Make Layout Non Clickable But Children Clickable

I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do this? Here is my code so far
final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
//this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.height = 2560;
params.y = 0;
floaterLayout.setBackgroundColor(Color.RED);
floaterLayout.setAlpha(0.5f);
floaterLayout.setLayoutParams(params);
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
ImageView view = new ImageView(getApplicationContext());
Util.setImageDrawable(view, R.drawable.floater_dots);
floaterLayout.addView(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
#Nicolas you are creating a relative layout that's not added to your parent layout.Simply create a xml with a relativeLayout and an imageView. Set that imageView's onClickListener in java code. Through this the parent layout will be nonclickable and only imageview will be clickable

How to add an ImageView over a Button

I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}

How to get ImageView ID from inflater in Android

I'm using an inflater to show a popup setting menu made by me. It's composed by some images and buttons. After complete XML layout I'm started to code it, it opens correctly with this code:
public void Settings_button(View view)
{
if (p != null)
showPopup(Main_activity.this, p);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
ImageButton setting_button = (ImageButton) findViewById(R.id.settings_button);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
setting_button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Resources r = getResources();
int popupWidth = 500;
int popupHeight = 300;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.settings_popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 150;
int OFFSET_Y = 30;
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
But now I need to get ImageViews IDs and Buttons IDs, I've used:
//In onCreate of Main_Activity
ImageView popup_setting_1_icon;
popup_setting_1_icon = (ImageView) findViewById(R.id.setting_1_icon);
Button popup_setting_1_button;
popup_setting_1_button = (Button) findViewById(R.id.setting_1_button);
But when I'm starting to use this Views for example popup_setting_1_icon.setImageResource(R.id.setting_done_icon); I get application crash with NullPointerException.
I've read something like I should get pointers from inflater but I've tried to do so inside showPopup method but nothing. How I can solve this problem?
Initialize views in show popup method:
ImageView popup_setting_1_icon;
popup_setting_1_icon = (ImageView)layout. findViewById(R.id.setting_1_icon);
Button popup_setting_1_button;
popup_setting_1_button = (Button)layout. findViewById(R.id.setting_1_button);
I had a very similar problem with programmatically inflating a container and populating it. This is what worked for me:
LayoutInflater inflator = LayoutInflater.from(getContext());
LinearLayout viewGroup = (LinearLayout) getActivity().findViewById(
R.id.popup);
View layout = inflator.inflate(R.layout.settings_popup_layout, null);
Button button = (Button) layout.findViewById(R.id.setting_1_button);
ImageView image = (ImageView) layout.findViewById(R.id.setting_1_icon);
//bind data to view/buttons
viewGroup.addView(layout);
This is assuming your settings button and image views are inside the settings popup layout.

How do I get buttons not to sit on top of each other in RelativeLayout

I'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all

Show/Hide ImageView on Button click

I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.
This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?
private ImageView picview;
//*snip* loads of other code
//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(picview.getVisibility() == View.INVISIBLE)
{
picview.setVisibility(View.VISIBLE);
}
else if (picview.getVisibility() == View.VISIBLE)
{
picview.setVisibility(View.INVISIBLE);
}
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);
//Imageview loaded from drawable
ImageView picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);
this.setContentView(rl);
You are accidentally creating two copies of picview. Shorten this line:
ImageView picview = new ImageView(this);
To:
picview = new ImageView(this);
(Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException...)
change
show_pic.setOnClickListener(new OnClickListener() to
show_pic.setOnClickListener(new View.OnClickListener()

Categories

Resources