Android: setText() for TextView in PopupWindow not working - android

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);
//...

Related

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

Get the position of a view just after an addView()

Everything is in the title. I need to add the view at some point in my relativeLayout but not necessary at the OnCreate method. What I want to do is adding a view when the user click on the RelativeLayout, no problem for that. But I need to know the position of this view as soon as possible after I used addView method. Currently I'm using postDelayed but I need to put a high value to be sure that it is displayed before taking the position of the view. I tried using post but didn't work very well so that's why i use a delayed value. I also tried something with treeObserver some time ago but maybe i used it wrong I don't know because it did not work.
Edit:
Here I create the layout param.
RelativeLayout.LayoutParams laoutparam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT );
laoutparam.addRule(RelativeLayout.CENTER_IN_PARENT);
Here I create my custom view and add the layoutparam to it:
Button button = (Button)findViewById(R.id.lol);
CustView view = new Bubble(getApplicationContext(), "Press this button", button);
bubble.setLayoutParams(laoutparam);
And then in the constructor of my custom RelativeLayout I do an addView(view).
Thanks for your help.
I hope this helps you with getting view location: https://www.tutorialspoint.com/how-to-get-the-absolute-coordinates-of-a-view-in-android
Example:
int loc[]=new int[2];
yourView.getLocationOnScreen(loc);
int x=loc[0];
int y=loc[1];

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

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.

Place a button inside DialogFragment's title

I show a DialogFragment from another DialogFragment. I need to set title and a button just next to it. I don't want to reimplement the theme and create a custom view inside DialogFragment's content view instead of dialog title (because it's error-prone and time wasting). Is it even possible? I tried many API functions, AlertBuilder, ActionBar, this and that, still didn't found anything that fits my needs.
Try something like this:
// Title
final int titleId = getResources().getIdentifier("alertTitle","id","android");
TextView title = (TextView) popup.findViewById(titleId);
title.setText("My new title");
// Title's parent layout
ViewGroup viewGroup = (ViewGroup) title.getRootView();
// Button
Button button = new Button(this);
button.setText("A Button");
viewGroup.addView(button);
Note: You may need to adjust the button and title objects' LayoutParams.
An update. This can be achieved by calling setCustomTitle on AlertDialog.Builder class. Available since api level 1, and do not require a bunch of code.
Example:
AlertDialog.Builder(getActivity()).setCustomTitle(getActivity().getLayoutInflater().inflate(R.layout.my_custom_view, viewGroup)).create()

How to get a spinner into an alertbox/dialog?

I am trying to put a spinner into an alert box and would much appreciate it, if someone would point me in the direction of a tutorial or show some code on how this can be done.
Create an xml layout with a spinner
in your code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layoutname);
you can access the spinner like this
Spinner spin = (Spinner)dialog.findViewById(R.id.spinnerid);
If you're using an alert dialog, you can add a custom layout containing your Spinner to your existing dialog.
To see an example of this, look for the "DIALOG_TEXT_ENTRY" case in this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
You can do it like this:
// ProgressBar properties
RelativeLayout.LayoutParams progressParams = new RelativeLayout.LayoutParams(Patterns.PROGRESS_BAR_WIDTH, Patterns.PROGRESS_BAR_WIDTH);
progressParams.addRule(RelativeLayout.CENTER_VERTICAL);
progressParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mProgress = new ProgressBar(context);
mProgress.setIndeterminate(true);
rootLayout.addView(mProgress,progressParams);
mProgress.setVisibility(View.VISIBLE);
Where rootLayout is your Activity's layout where you want to put the spinning "box". The LayoutParams that I used is just to place the box in the layout's center. When your box is no longer necessary, you can dismiss it like this:
mProgress.setVisibility(View.GONE);
layoutBg.removeView(mProgress);

Categories

Resources