Spinner with Icon and Text like Phone App on Android (with Bluetooth) - android

I want to re-create the following:
Specifically, note the Bluetooth icon, clicking on it brings up what looks like a spinner? Or is it a dialog somehow located correctly? I couldn't find the phone app code anywhere, and am at a loss for how to best implement this.

I actually figured out the solution shortly after posting the question.
The correct solution is to use a PopupWindow. Here is pretty much all you need to do:
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.bluetooth_popup, null);
popupView.findViewById(R.id.bluetooth).setOnClickListener(this);
popupView.findViewById(R.id.speakerphone).setOnClickListener(this);
popupView.findViewById(R.id.earpiece).setOnClickListener(this);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Together these two allow for the popupWindow to be dismissed when touch occurs outside
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
and then to show it anchored to a view:
popupWindow.showAsDropDown(speaker, 0, (int) (-160));
Hope this helps you out.

Related

PopupWindow.setFocusable() not working on Lollipop

I'm trying to hide a PopupWindow, when it is clicked outside, and it works perfectly when the setFocusable(true) is set for the PopupWindow. Howewer this solutions seems to be not working on Lollipop devices.
I know there is an other method, when we set a background drawable to the PopupWindow like this:
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
This works as expected, but the background hides the elevation of the PopupWindow.
Is there a proper way to accomplish the following behavior on Lollipop: Make the PopupWindow dismissable on outside click, while keeping it's shadow generated by the setElevation() call?
This is the complete code:
PopupWindow popupWindow = new PopupWindow(binding.getRoot(), UiUtil.dpToPx(context, POPUP_WIDTH_IN_DP),
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setElevation(UiUtil.dpToPx(context, POPUP_SHADOW));
PopupWindowCompat.setOverlapAnchor(popupWindow, true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
int[] position = new int[2];
viewGroup.getLocationInWindow(position);
popupWindow.showAtLocation(viewGroup, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, position[1]);

Is there a way to move the calendar of a datePicker in spinner mode?

I'm looking for something like this. I have plenty of vertical screen real estate, and I like how the calendar fits in with the app, but I want the user to have a spinner for the year as well. I just want to know if there's a way to move the calendar (if that's possible) so that the year spinner would be able to be seen.
Just add the pickerMode to your DatePicker in your XML file:
<DatePicker
...
android:datePickerMode="spinner" />
Or you can add a popup dialog which is more customizable. If you use a popupdialog, then you have to handle more cases compared to spinner.
And in onclick event of the view, attach popupwindow
PopupWindow yourDatePopup = datePopupWindow();
//this line will give a spinner effect
yourDatePopup.showAsDropDown(clickableView, -7, 0);
public PopupWindow datePopupWindow() {
PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
//Define your dateview
popupWindow.setContentView(yourDateView);
popupWindow.setOutsideTouchable(true);
return popupWindow;
}
Note:- Just to help, not exact code

Move centered PopupView when Keyboard opens

I'm programmatically adding a PopupView which contains an EditText field to my Activity, which is vertically and horizontally centered on the screen. When the keyboard opens, I want the PopupView to move up, so it is still centered on the visible screen/activity part.
My code:
EditText e = new EditText(super.getContext());
PopupWindow popup = new PopupWindow(e, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(this, Gravity.CENTER, 0, 0);
I've tried many things with windowSoftInputMode for the Activity; I've tried to setSoftInputMode(mode) on the popup - but none of my approaches have worked. Neither my layout nor the Popup change their position when the keyboard opens. (I only want my popup but not the layout to change, though, just pointing it out).
Also the code is placed in a LinearLayout class, in case you are wondering why I'm using this as a View.
Easier to get Android to do all the heavy lifting for you.
Just use:
popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
So after much research, I finally found a way to accomplish that.
The code for creating the PopupWindow and making it being displayed in the vertical and horizontal center stays the same:
PopupWindow popup = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);
Then the only thing you need is a Listener for the Keyboard (or more general: For Window Height changes). This was actually easier than I thought - and it didn't require any special access like an Activity-object or similar. Even in my independent View-class which only knows the Context (which I didn't want to cast), I was able to accomplish that. Everything you need is only one View-object which has already been added to the layout.
// You can call this method on any view that is added to the layout:
final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
root.getWindowVisibleDisplayFrame(r);
// Calculate the difference between the original height and the new height
int heightDiff = r.height() - root.getHeight();
// Now update the Popup's position
// The first value is the x-axis, which stays the same.
// Second value is the y-axis. We still want it centered, so move it up by 50% of the height
// change
// The third and the fourth values are default values to keep the width/height
popup.update(0, heightDiff / 2, -1, -1);
}
});
For reference:
Listening to window height changes
Only downside:
This solution may not work when you add a PopupView while the Keyboard is already opened. But in my case, this isn't an expectable scenario anyway.

Display an image modally with PopupWindow without XML

How do I display a Popup Window without XML and show a custom image?
I have not found any working examples.
Tried this:
public void showMenu() {
PopupWindow myWindow=new PopupWindow(this);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
myWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.dashboard_instructions));
myWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
Idea in my mind is you can add custom image as a background of popup window, Not able to test right now, below is the idea hope it will get a clue for you.
PopupWindow myWindow=new PopupWindow(this);
myWindow.showAtLocation(ANY_VIEW_FOR_WINDOW_TOKEN, Gravity.CENTER, 0,0);
myWindow.setBackgroundDrawable(getDrawable(R.drawable.abc));
getDrawable() call requires Target API 21, Hope it will works.

Android: setText() for TextView in PopupWindow not working

Basically, I have a TextView in a layout which I use for a PopupWindow. I show this PopupWindow when a user clicks a button; I want to be able to dynamically change the text in the PopupWindow upon button click. However, findViewById(my_textview).setText() does not seem to do anything, and indeed causes the PopupWindow to no longer show when I click the button.
I can set text from the layout xml fine.
Anyone know what's up with this? Thanks-
I solved the problem. For whatever reason you need to call popup.getContentView().findViewById instead of just findViewById (where popup is your PopupWindow object). I wasn't getting a NullPointerException before so I'm not exactly sure why this fixed the issue but it did.
So the code goes something like:
PopupWindow pw = new PopupWindow(your layout and params here);
((TextView)pw.getContentView().findViewById(R.id.my_textview)).setText("hello there");
pw.showAtLocation(your params here);
You will be able to find the views with the "findViewById" only using the view you inflated the popupWindow before
like this
private View viewPopUp;
private PopupWindow windowPopUp;
//...
//form_popup is the template to the popup
viewPopUp = mContext.getLayoutInflater().inflate(R.layout.form_popup, null);
windowPopUp = new PopupWindow(viewPopUp, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//...
viewPopUp.findViewById(R.id.popupTopTitle);
viewPopUp.findViewById(R.id.popupMiddleMsg);
//...

Categories

Resources