I'm trying to instantiate a Progress Bar from the main Activity and add it to the view dynamically. When I do this, I get a bar:
ProgressBar bar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(params);
bar.setId(mInt);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(bar);
I started a new project that only includes the above code, and the progress bar displays correctly.
If I add the following lines to the above code, the screen is blank for a second and then shows a half full bar. I would expect to see an empty bar for a second and then it goes to 50%.
try {
Thread.sleep(2000);
} catch ...{
}
bar.setProgress(50);
If I add some code like this, however, the bar and changes display correctly.
Button b = new Button(this);
b.setText("change bar");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
changeProg();
}
});
public void changeProg() {
switch (state) {
case 0: bar.setProgress(100); state = 1; break;
case 1: bar.setProgress(127); state = 2; break;
case 2: bar.setProgress(33); state = 0; break;
}
}
If I try to automate the process with a loop to call changeProg() every so often, the screen just stays blank - no widgets at all display, whether they are described by the XML or pro grammatically makes no difference.
I'm having trouble understanding why this behavior exists.
It seems that you call Thread.sleep(2000) from UI thread, which causes the problem, even create ANR. Try to create a new thread (separate thread) to change the progress value.
See the example below:
https://stackoverflow.com/a/17758416/3922207
ProgressBar pdialog = new ProgressBar(context,null, android.R.attr.yourcustomstyle);
and check your apptheme in style
SetProgress tip:
Set the current progress to the specified value. Does not do anything if the progress bar is in indeterminate mode.
So you have to call:
bar.setIndeterminate(false);
Have you tried looking at this site?
http://www.mkyong.com/android/android-progress-bar-example/
The correct way is use 1 thread to run your time consuming task and another thread to update the progress bar status
Hope this helps!
Related
I would hide navigation bar and action bar of my app after some second that display don't get touched, and expand current view to fullscreen.
Then if user touch the screen (or better if he swipe down), make visible both again. How to?
You can use a Handler to delay certain actions.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// DO DELAYED STUFF
getActionBar().hide();
}
}, delaytime); // e.g. 3000 milliseconds
The actions you take inside the run() method will be executed after
the delay-time you set.
If you are using Eclipse, just create a new project and select "Fullscreen Activity": this is a good example on how to do what you want.
I want to make toast click able or a UI element which will have clickable button but behaves like toast.
It should not run on UI thread like toast.
It should not halt or overlay current user activity, message should come like toast with clickable button and vanish but as toast user should be able to access background ongoing UI item.
If any one has any idea about how to achieve this pls share with me.
Little trick. Tested working Android 4.4
toast = new Toast(context);
try {
Class<?> clazz = Class.forName("android.widget.Toast");
Method method = clazz.getDeclaredMethod("getWindowParams");
WindowManager.LayoutParams param = (WindowManager.LayoutParams) method.invoke(toast);
param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
} catch (Exception e) {
e.printStackTrace();
}
The Gmail undo bar is the best suitable for you, its just like a toast with a button.
Here is a code implementation for it.
http://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java
I had a similar requirement that I solved using a PopupWindow. Basically, I had an about window with a clickable link that I wanted displayed like a toast. The popup window can accomplish this as follows:
In the parent class, I use the following flag:
private boolean durationExpired = false;
Then, when I invoke what would have been the toast, I do the following instead:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.about_hiittimer,
(ViewGroup) findViewById(R.id.about_hiittimer));
TextView url = (TextView) layout.findViewById(R.id.url);
url.setMovementMethod(LinkMovementMethod.getInstance());
final PopupWindow popupWindow = new PopupWindow(layout, 280, 160, false);
popupWindow.showAtLocation(layout, 17, 0, 0);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
final Handler popupHandler = new Handler();
runOnUiThread(new Runnable() {
#Override
public void run() {
if (!durationExpired) {
durationExpired = true;
popupHandler.postDelayed(this, 2000);
} else {
popupWindow.dismiss();
popupHandler.removeCallbacks(this);
durationExpired = false;
}
}
});
Put your main layout inside a FrameLayout. Write a layout for your toast, with the button and all, insert it into the FrameLayout (below your main layout) and set its visibility to GONE.
When you show it (setting visibility to VISIBLE) start a new thread that counts down the seconds till its dismissed. Set it back to invisible from the thread via a Handler (cause all UI elements can only be managed from the main thread).
cheers!
I have a null pointer on a button that I need to take me to a new layout when pushed. I have the code set as:
((Button) findViewById(R.id.analyzee)).setOnClickListener(btnClick);
inside a method that uses conditional statements.
It is a basic face detect app. If faces are not found, I do this:
if (facesFound < 1) {
mFlipper.setDisplayedChild(2);
mTheMessage = (TextView) findViewById(R.id.falsemessage);
mThePicture = (ImageView) findViewById(R.id.false_view);
mTheMessage.setText(R.string.noFaceOne);
mThePicture.setImageBitmap(bitmap565);
return;
if faces are found, I draw a box on the face, and do this:
mFlipper.setDisplayedChild(1);
mTheMessage.setText(R.string.noFaceFive);
mThePicture.setImageBitmap(bitmap565);
My issue lies here though, I use this method to run when buttons are clocked:
private final View.OnClickListener btnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_box:
openCamera();
break;
case R.id.crop_face:
final ProgressDialog dialog = ProgressDialog.show(Main.this, "",
"Cropping photo", true);
dialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
dialog.dismiss();
}
}, 3000);
cropFace();
break;
So, my issue lies in this:
In one of my layouts in the flipper, the button that rests on the layout will need to give the user the option to snap a new picture if there are no faces found. The other layout will need the button (upon click) to have the faces cropped and the results to be sent to another layout.
The issue I am facing is where the code:
((Button) findViewById(R.id.crop_face)).setOnClickListener(btnClick);
needs to be placed in order for the program to release the button has been clicked, it calls the case in my switch statement, and runs the crop face_method.
I try putting it in the if statement where I set the image View and text View, but I get a null pointer on that line I am declaring my button on.
The buttons I have on my main menu work fine, as they are in my onCreate method, but I don't know where to play this button command, and also where I need to place my reopen Camera command.
Thanks!
I simply was using the wrong button ID...sorry for the confusion =x
I need to display progress icon in button so that user can interact other GUI elements while background task is processing.
I have searched in Android developer site and found that we can use animated drawables but don't know how to use them. Please advise on the same.
The very simple way to do this without using the animated drawable is to use "PregressBar" component in the design layout xml. When u need to show it, just set it's visibility property to visible and when you need to hide it, u can set it's visibility property to GONE. But remember this is UI task so when u need to do this with non-UI thread, u need to use Handler to set the status of "ProgressBar" component at runtime.
Below id the component in the layout file.
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Below is the code written in java file
ProgressBar prg;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
prg=(ProgressBar)findViewById(R.id.ProgressBar1);
prg.setVisibility(ProgressBar.GONE);
}
public void start_background_process()
{
// starting the process
prg.setVisibility(ProgressBar.VISIBLE);
new Thread(new Runnable()
{ public void run()
{
// Do your background stuff here which takes indefinite time
mHandlerUpdateProgress.post(mUpdateUpdateProgress);
}
} ).start();
}
final Handler mHandlerUpdateProgress= new Handler();
final Runnable mUpdateUpdateProgress = new Runnable() {
public void run() {
// ending the process
prg.setVisibility(ProgressBar.GONE);
}
};
If the default progress indicator is good enough for you (i.e. the spinning wheel), then you can just use ProgressBar. To change it from a normal progress bar to a spinning wheel, use progressBar.setIndeterminate(true).
public class MessageDisplayDialog extends Dialog implements OnClickListener
{
public MessageDisplayDialog(Context context, String title, String message)
{
super(context);
setTitle(title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.color.default_text_color);
Log.v(getClass().getSimpleName(), "MessageDisplayDialog");
LinearLayout objLinearLayout = new LinearLayout(context);
LinearLayout objButtonLayout = new LinearLayout(context);
TextView objMesaageView = new TextView(context);
objMesaageView.setText(message);
objMesaageView.setTextColor(Color.WHITE);
objMesaageView.setGravity(Gravity.CENTER_HORIZONTAL);
objMesaageView.setPadding(0, 0, 0, 10);
Button okButton = new Button(context);
okButton.setText(" OK ");
okButton.setOnClickListener(this);
okButton.setWidth(100);
objButtonLayout.addView(okButton);
objButtonLayout.setGravity(Gravity.CENTER_HORIZONTAL);
objButtonLayout.setPadding(0, 5, 0, 0);
objButtonLayout.setBackgroundColor(Color.LTGRAY);
objLinearLayout.setOrientation(LinearLayout.VERTICAL);
objLinearLayout.addView(objMesaageView);
objLinearLayout.addView(objButtonLayout);
setContentView(objLinearLayout);
//LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//this.addContentView(objLinearLayout, param);
}
public void onClick(View v)
{
this.dismiss();
}
}
But the Dialog is not showing bar below the Title, how to crack it.
I think your question has already been answered in this thread
Android - change custom title view at run time
please do some searching and accept answers before asking questions.
I think the horizontal border between the title and the message in the built in dialogs is part of AlertDialog, not the base Dialog class, although I could be totally wrong about that. Regardless, whenever I attempt to do something similar to what you are doing, that horizontal line disappears and I've never been able to get it back.
I ended up just writing my own dialog layout XML file and creating my own horizontal line using a <shape> drawable. It's actually fairly painless to create your own completely custom Dialog layouts like this, and gives you more control over the look of your dialogs.
This is old I know, but FYI the solution of drawing your own line on a custom dialog is not compatible with ICS.
It will display both your line and the line that ICS is now including by default. So you'd get two lines on the screen.
use two lines of code to remove dialoge title
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);