How to add Text to new Progrss bar in android programmatically - android

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.

Related

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

Android Activity with Transparent Layout

I wish to create a activity that look like a popup window
That is with transparent background for activity als i wish to show it in custom position on scree that is on right corner of device screen
what i did was inside onCreate of pop over like activty
Display display = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params = getWindow().getAttributes();
// params.x = -20;
params.height = (display.getHeight()) / 2;
params.width = (display.getWidth()) / 2;
// params.y = -10;
params.gravity = Gravity.RIGHT;
getWindow().setAttributes(params);
in maifest
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"></activity>
This is what my launcher activity looks like
So when i click on search on action bar i'm sending intent to my new activity .I want to make it look like pop just below the search icon
This what i obtained is
You can see search of previous activity is clicked and new activity is loaded(blue portion).
As you can see the new activity moves to centre of screen . but i want it just below the action bar icon.
i tried params.gravity = Gravity.TOP | Gravity.RIGHT;. then i got this
I want to place it just below the action bar of previous activity . I tried many ways to achieve it but failed. so can anyone suggest a methode
In your manifest you wrote
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar">
you should remove the NoActionBar part
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light">
I can think of two things that may help. First, you could try changing the Theme in your manifest to android:theme="#android:style/Theme.Dialog and this should give you the wanted look.
Edit
You can hide the title bar with
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just make sure to call it before calling setContentView()
Also, if it doesn't need to be a separate Activity you could use PopupWindow. With this you can use isAboveAnchor and use the ActionBar as the anchor and place it below or use one of the other associated methods. I don't know if either of these will fulfill what you need but they may work for you.
#edwin What you can do is after making
params.gravity = Gravity.TOP | Gravity.RIGHT;
you just provide some margin from top, like this:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourCustomPopOveractivity.setLayoutParams(params);
and i think after doing this you can get your needed position and everything you just did is right.
Please try then tell me
Try Gravity to Center rather than Top Right..

How to use the listView.setEmptyView()?

I want to add a progress bar in a listview when the list loads and if there are no results after the loading is finished I want to add a textVeiw saying the list is empty using the setEmptyView function.
I tried using this code to add a progress bar in the list but now luck:
#Override
protected void onPreExecute() {
ProgressBar progress = new ProgressBar(parent);
progress.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
((ListView) view).setEmptyView(progress);
super.onPreExecute();
}
how do I add a progress bar in the list pragmatically without using xml layout to inflate it from?
thaks
Try this way,I am not tried but got the reference from this Android – ListView => setEmptyView()
listView.setEmptyView(findViewById(R.id.progressBar));
and let me working or not.
Good Luck.

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

Android: Adding button to custom title bar

I have created a custom title bar as shown in this example
http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/
"A custom title bar" - half way down.
On some activities I would like to place a button on the right hand side of the titlebar (same as facebook app). I have attempted to add a button to the view as follows, but it doesn't appear.
Custom title bar is displayed as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.maintabhost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.headerbar_include);
Attempting to add button as follows. The button will eventually be an ImageButton and aligned to right of custom titlebar-if I get it working. (just realised I've too many layoutparams now, but this isnt affecting the button display)
LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);
Button searchButton = new Button(this);
searchButton.setText("info");
LayoutParams layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
searchButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(searchButton, layoutParams);
layout.invalidate();
I could just create another custom titlebar with the button already embedded, but a dynamic solution would be better.
Cheers
First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)
Starting with the xml file, add an id attribute:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<!-- Stuff -->
</LinearLayout>
and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):
LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
layout.addView(new Button(this));
and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):
Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.
Here's one approach to make sure the button(s) remain in the title bar:
How to Create Custom Window Title in Android
Essentially, wrap the android Activity class, and then extend from that new class.

Categories

Resources