How to get a spinner into an alertbox/dialog? - android

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

Related

How to add Text to new Progrss bar in android programmatically

Hi I am trying to show a new progress bar on my android app since the progress dialog is deprecated i want to use new progress bar. Right now my code does bring up progress bar but i want to add a text to it.
See my code below
RelativeLayout layout= view.findViewById(R.id.upload_layout);
pDialog =new ProgressBar(container.getContext(),null,android.R.attr.progressBarStyleSmall);
pDialog.setIndeterminate(false);
pDialog.setBackgroundColor(Color.argb(240,255,255,255));
pDialog.setVisibility(View.GONE);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(pDialog,params);
Edit:
Sorry I guess the question wasn't clear please see the image. I have no errors.
Progress bar on the screen to which i want add text like "Uploading..."
Any help would be appreciated. Thanks in advance
I guess you want to add text "Uploading..." below the pDialog,you can add code in your code's end
TextView up = new TextView(pDialog.getContext());
up.setText("Uploading...");
RelativeLayout.LayoutParams paramUp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
paramUp.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(up,paramUp);
but I suggest you build progress bar in xml.

having trouble to make the layout of a dialog box

I wanted to make a Dialog box for simple signup page like this image
but I can't figure out how can I do this. I am basically struggling to make the cross icon to the right corner of the Dialog box.
Is there any library to make this?
Try this:
final Dialog exitBayDialog = new Dialog(getActivity());
exitBayDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
exitBayDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
exitBayDialog.setContentView(R.layout.your_dialog_layout);
final EditText etSearch = (EditText) exitBayDialog.findViewById(R.id.etVehicleNumber);
exitBayDialog.show();

Android Transparent Dialog Still Shows Black Background And Doesn't Wrap Around Content Completely

In this code piece i've built a dialog, a textView and put the textView inside the Dialog.
TextView progressHolder = new TextView(activity);
progressHolder.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
progressHolder.setText("la la la3");
progressHolder.setBackgroundColor(Color.TRANSPARENT);
mProgressDialog = new Dialog(activity);
mProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(R.color.transparent));
mProgressDialog.setContentView(progressHolder);
mProgressDialog.show();
Displaying this dialog is supposed to show the words "la la la3" on screen with the activity showing behind them.
What actually happens is that most of the activity is revealed but the words described appear in a small black box that only wraps around the words width but has a big top margin.
For question sake, i'm not trying to show a textView but the bug still happens with this exact code.
Help anyone?
This this:
mProgressDialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
Try This code..
yourDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
To remove black background colour use
getWindow().setBackgroundDrawableResource(android.R.color.transparent);

Adding table layout to AlertDialog

I want to display an AlertDialog (button OK) with the content being a TableLayout.
However, I would like the TableLayout creation to be programatically, by Java, as I need to add rows depeding on some variables.
Any Idea how to do that?
Specifically:
1- Can an AlertDialog have TableLayout view, or does it only expect text so I should go with dialog?
2- How do I create TableLayout programmatically and add rows to it.
I have done it in xml but not sure in java
Help is appreciated
Thanks
Why don't you create a custom Dialog?
1 - You can instantiate a Dialog as in Dialog dialog = new Dialog(context, theme)
2 - Then you can make a dialog.setContentView(layout), find your tablelayout by id (dialog.findViewById)
3 - Then do what you need to do. I'd recommend putting this all in a separated method.
You should be able to set whatever layout you desire to the dialog using it 'setContentView' method.
You can inflate ui from an xml file. Like
LayoutInflater mInflater = LayoutInflater.from(context);
View yourView = mInflater.inflate(R.layout.your_layout, null);
alertDialog.setContentView(yourView);
The above code should work.

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